-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_feed_follows.go
110 lines (93 loc) · 2.79 KB
/
handlers_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
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
108
109
110
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/ayushrakesh/go-rssagg/internal/auth"
"github.com/ayushrakesh/go-rssagg/internal/database"
"github.com/go-chi/chi"
"github.com/google/uuid"
)
func (apiCfg *apiConfig) handlerCreateFeedFollow(w http.ResponseWriter, r *http.Request) {
apiKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, 403, fmt.Sprintf("Auth error- %s", err))
return
}
user, er := apiCfg.DB.GetUserByAPIKey(r.Context(), apiKey)
if er != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't found user with APIKey- %v", er))
return
}
userId := user.ID
type pr struct {
FeedID uuid.UUID `json:"feed_id"`
}
prm := pr{}
decoder := json.NewDecoder(r.Body)
errr := decoder.Decode(&prm)
if errr != nil {
respondWithError(w, 400, fmt.Sprintf("Error parsing json- %v", errr))
return
}
feedFollow, errrr := apiCfg.DB.CreateFeedFollow(r.Context(), database.CreateFeedFollowParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
UserID: userId,
FeedID: prm.FeedID,
})
if errrr != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't create feed_follow- %v", errrr))
return
}
respondWithJSON(w, 201, databaseFeedFollowToFeedFollow(feedFollow))
}
func (apiCfg *apiConfig) handlerGetFeedFollow(w http.ResponseWriter, r *http.Request) {
apiKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, 403, fmt.Sprintf("Auth error- %s", err))
return
}
user, er := apiCfg.DB.GetUserByAPIKey(r.Context(), apiKey)
if er != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't found user with APIKey- %v", er))
return
}
userId := user.ID
feedFollows, errr := apiCfg.DB.GetFeedFollows(r.Context(), userId)
if errr != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't get FeedFollows- %v", errr))
return
}
respondWithJSON(w, 200, databaseFeedFollowsToFeedFollows(feedFollows))
}
func (apiCfg *apiConfig) handlerDeleteFeedFollow(w http.ResponseWriter, r *http.Request) {
apiKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, 403, fmt.Sprintf("Auth error- %s", err))
return
}
user, er := apiCfg.DB.GetUserByAPIKey(r.Context(), apiKey)
if er != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't found user with APIKey- %v", er))
return
}
userId := user.ID
feedF := chi.URLParam(r, "feedFollowID")
feedFollowId, err := uuid.Parse(feedF)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't parse Feed Follow ID- %v", err))
return
}
errr := apiCfg.DB.DeleteFeedFollow(r.Context(), database.DeleteFeedFollowParams{
ID: feedFollowId,
UserID: userId,
})
if errr != nil {
respondWithError(w, 400, fmt.Sprintf("Couldn't delete Feed Follow- %v", errr))
return
}
respondWithJSON(w, 200, struct{}{})
}