-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchirpRoutes.go
134 lines (108 loc) · 2.7 KB
/
chirpRoutes.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
package api
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"regexp"
"slices"
"starter/internal/database"
"strconv"
)
const (
Asc string = "asc"
Desc = "desc"
)
func CreateChirp(w http.ResponseWriter, r *http.Request) {
chirps := database.GetChirps()
user := r.Context().Value("currentUser").(*database.User)
var id int
if len(chirps) == 0 {
id = 1
} else if last := chirps[len(chirps)-1]; last.Id != 0 {
id = last.Id + 1
}
body, _ := io.ReadAll(r.Body)
c, err := validateChirp(body)
if err != nil {
respondWithJSON(w, 400, err)
return
}
c.Id = id
c.AuthorId = user.Id
chirps = append(chirps, c)
database.SaveChirps(chirps)
respondWithJSON(w, 201, c)
}
func GetChirps(w http.ResponseWriter, r *http.Request) {
chirps := database.GetChirps()
authorIdParam := r.URL.Query().Get("author_id")
if authorIdParam != "" {
authorId, err := strconv.Atoi(authorIdParam)
if err != nil {
respondWithJSON(w, 400, "invalid author id")
return
}
chirps = slices.DeleteFunc(chirps, func(chirp database.Chirp) bool {
return chirp.AuthorId != authorId
})
}
sortParam := r.URL.Query().Get("sort")
if sortParam != "" {
slices.SortFunc(chirps, func(a, b database.Chirp) int {
if sortParam == Asc {
return a.Id - b.Id
}
return b.Id - a.Id
})
}
respondWithJSON(w, 200, chirps)
}
func GetChirp(w http.ResponseWriter, r *http.Request) {
chirpId, _ := strconv.Atoi(r.PathValue("id"))
chirps := database.GetChirps()
index := slices.IndexFunc(chirps, func(c database.Chirp) bool {
return c.Id == chirpId
})
if index == -1 {
respondWithJSON(w, 404, "Could not load chirp")
return
}
respondWithJSON(w, 200, chirps[index])
}
func DeleteChirp(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("currentUser").(*database.User)
chirpId, _ := strconv.Atoi(r.PathValue("id"))
chirps := database.GetChirps()
index := slices.IndexFunc(chirps, func(c database.Chirp) bool {
return c.Id == chirpId
})
if index == -1 {
respondWithJSON(w, 404, "Could not load chirp")
return
}
chirp := &chirps[index]
if chirp.AuthorId != user.Id {
respondWithJSON(w, 403, "Only the author can delete this tweet")
return
}
slices.DeleteFunc(chirps, func(chirp database.Chirp) bool {
return chirp.Id == chirpId
})
database.SaveChirps(chirps)
respondWithJSON(w, 204, nil)
}
func validateChirp(body []byte) (database.Chirp, error) {
c := database.Chirp{}
err := json.Unmarshal(body, &c)
if err != nil {
log.Fatal(err)
}
if len(c.Body) > 140 {
return c, errors.New("max length is 140")
}
containsBadWords := regexp.MustCompile(`(?i) (kerfuffle|sharbert|fornax) `)
c.Body = containsBadWords.ReplaceAllString(c.Body, " **** ")
return c, nil
}