-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_feed.go
107 lines (96 loc) · 2.92 KB
/
handler_feed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/haroonalbar/rss-aggregator/internal/database"
)
func (apiCfg *apiConfig) handlerCreateFeed(w http.ResponseWriter, r *http.Request, user database.User) {
type Parameters struct {
Name string `json:"name"`
Url string `json:"url"`
}
params := Parameters{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(¶ms)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error decoding: %v", err))
return
}
feed, err := apiCfg.DB.CreateFeed(r.Context(), database.CreateFeedParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
Name: params.Name,
Url: params.Url,
UserID: user.ID,
})
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error creating feed: %v", err))
return
}
respondWithJSON(w, 201, databaseFeedtoFeed(feed))
}
func (apiCfg *apiConfig) handlerUpdateFeed(w http.ResponseWriter, r *http.Request, user database.User) {
type Parameters struct {
Name string `json:"name"`
Url string `json:"url"`
}
feedIDStr := chi.URLParam(r, "feedID")
feedID, err := uuid.Parse(feedIDStr)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error parsing feed id from url: %v", err))
return
}
params := Parameters{}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(¶ms)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error decoding: %v", err))
return
}
feed, err := apiCfg.DB.UpdateFeed(r.Context(), database.UpdateFeedParams{
ID: feedID,
UserID: user.ID,
Name: params.Name,
Url: params.Url,
})
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error updating feed: %v", err))
return
}
respondWithJSON(w, 200, databaseFeedtoFeed(feed))
}
func (apiCfg *apiConfig) handlerGetFeeds(w http.ResponseWriter, r *http.Request) {
feeds, err := apiCfg.DB.GetFeeds(r.Context())
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't get feeds: %v", err))
return
}
respondWithJSON(w, 200, databaseFeedstoFeeds(feeds))
}
// func (apiCfg *apiConfig) handlerGetNextFeedToFetch(w http.ResponseWriter, r *http.Request) {
// nextFeed, err := apiCfg.DB.GetNextFeedToFetch(r.Context())
// if err != nil {
// respondWithError(w, 400, fmt.Sprintf("Couldn't get next feed to fetch: %v", err))
// return
// }
// respondWithJSON(w, 200, nextFeed)
// }
// func (apiCfg *apiConfig) handlerMarkFeedAsFetched(w http.ResponseWriter, r *http.Request) {
// feedIDStr := chi.URLParam(r, "feedID")
// feedID, err := uuid.Parse(feedIDStr)
// if err != nil {
// respondWithError(w, 400, fmt.Sprintf("Error parsing feed id: %v", err))
// return
// }
// nextFeed, err := apiCfg.DB.MarkFeedAsFetched(r.Context(), feedID)
// if err != nil {
// respondWithError(w, 400, fmt.Sprintf("Couldn't get next feed to fetch: %v", err))
// return
// }
// respondWithJSON(w, 200, nextFeed)
// }