-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchirps.go
176 lines (146 loc) · 4.15 KB
/
chirps.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"chirpy/internal/auth"
"encoding/json"
"errors"
"net/http"
"sort"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
)
type Chirp struct {
ID int `json:"id"`
Body string `json:"body"`
AuthorID int `json:"author_id"`
}
func (cfg *apiConfig) handleGetChirps(w http.ResponseWriter, r *http.Request) {
dbChirps, err := cfg.DB.GetChirps()
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't retrieve chirps")
return
}
chirps := []Chirp{}
for _, chirp := range dbChirps {
chirps = append(chirps, Chirp{ID: chirp.ID, Body: chirp.Body, AuthorID: chirp.AuthorID})
}
sort.Slice(chirps, func(i, j int) bool {
return chirps[i].ID < chirps[j].ID
})
respondWithJSON(w, http.StatusOK, chirps)
}
func (cfg *apiConfig) handleGetChirp(w http.ResponseWriter, r *http.Request) {
chripIDString := chi.URLParam(r, "chirpID")
chirpId, err := strconv.Atoi(chripIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid chirp ID")
}
dbChirp, err := cfg.DB.GetChirp(chirpId)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get chirp")
return
}
respondWithJSON(w, http.StatusOK, Chirp{ID: dbChirp.ID, Body: dbChirp.Body, AuthorID: dbChirp.AuthorID})
}
func (cfg *apiConfig) handleCreateChirp(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Body string `json:"body"`
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT")
return
}
subject, err := auth.ValidateAccessJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT")
return
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err = decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
return
}
userID, err := strconv.Atoi(subject)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't parse user ID")
return
}
cleanedBody, err := validateChirp(params.Body)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
chirp, err := cfg.DB.CreateChirp(cleanedBody, userID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create chirp")
return
}
respondWithJSON(w, http.StatusCreated, Chirp{
ID: chirp.ID,
Body: chirp.Body,
AuthorID: chirp.AuthorID,
})
}
func validateChirp(body string) (string, error) {
const chirpMaxLength = 140
if len(body) > chirpMaxLength {
return "", errors.New("Chirp is too long")
}
cleanBody := profanityFilter(body)
return cleanBody, nil
}
func profanityFilter(body string) string {
profanityWords := map[string]struct{}{
"kerfuffle": {},
"sharbert": {},
"fornax": {},
}
splitWords := strings.Split(body, " ")
for i, word := range splitWords {
if _, ok := profanityWords[strings.ToLower(word)]; ok {
splitWords[i] = "****"
}
}
return strings.Join(splitWords, " ")
}
func (cfg *apiConfig) handleDeleteChirp(w http.ResponseWriter, r *http.Request) {
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT")
return
}
subject, err := auth.ValidateAccessJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT")
return
}
userID, err := strconv.Atoi(subject)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't parse user ID")
return
}
chripIDString := chi.URLParam(r, "chirpID")
chirpId, err := strconv.Atoi(chripIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid chirp ID")
return
}
chirp, err := cfg.DB.GetChirp(chirpId)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Chirp does not exist")
return
}
if chirp.AuthorID != userID {
respondWithError(w, http.StatusForbidden, "You are not the author of this chirp.")
return
}
err = cfg.DB.DeleteChirp(chirpId)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Error deleting chirp")
return
}
respondWithJSON(w, http.StatusOK, Chirp{})
}