-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_feed_follows.go
71 lines (63 loc) · 2.02 KB
/
handler_feed_follows.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/preetDev004/rss-aggregator/db"
)
func (apiCfg *apiConfig) handleCreateFeedFollow(w http.ResponseWriter, r *http.Request, user db.User) {
type parameters struct {
FeedID uuid.UUID `json:"feed_id"`
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err := decoder.Decode(¶ms)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error Parsing JSON: %v", err))
return
}
feedFollow, err := apiCfg.DB.CreateFeedFollows(r.Context(), db.CreateFeedFollowsParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
FeedID: params.FeedID,
UserID: user.ID,
})
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error Creating the feed follow: %v", err))
return
}
respondWithJSON(w, 201, dbFeedFollowToFeedFollow(feedFollow))
}
func (apiCfg *apiConfig) handleGetUserFeedFollows(w http.ResponseWriter, r *http.Request, user db.User){
FeedFollows, err := apiCfg.DB.GetUserFeedFollows(r.Context(), user.ID)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't find any feed follows: %v", err))
return
}
if FeedFollows == nil{
respondWithJSON(w, 200, successful{Msg: "You Don't follow any feeds right now!"})
return
}
respondWithJSON(w, 200, dbFeedFollowsToFeedFollows(FeedFollows))
}
func (apiCfg *apiConfig) handleDeleteUserFeedFollow(w http.ResponseWriter, r *http.Request, user db.User){
feedFollowIDStr := chi.URLParam(r, "feedFollowID")
feedFollowId, err:=uuid.Parse(feedFollowIDStr)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't parse feed follow id: %v", err))
return
}
_, err = apiCfg.DB.DeleteUserFeedFollow(r.Context(),db.DeleteUserFeedFollowParams{
FeedID: feedFollowId,
UserID: user.ID,
})
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't delete the feed follows: %v", err))
return
}
respondWithJSON(w, 200, successful{Msg:"Unfollowed the feed, successfully!"})
}