-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_chirps.go
More file actions
148 lines (140 loc) · 3.64 KB
/
handlers_chirps.go
File metadata and controls
148 lines (140 loc) · 3.64 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/go-chi/chi/v5"
"github/ntvviktor/GoServer/internal/auth"
"github/ntvviktor/GoServer/internal/database"
"log"
"net/http"
"sort"
"strconv"
)
type Chirp struct {
ID int `json:"id"`
Body string `json:"body"`
AuthorID int `json:"author_id"`
}
func (apiConfig *apiConfig) handlePostChirps(w http.ResponseWriter, req *http.Request) {
tokenString, canCut := getTokenFromHeader(req)
if !canCut {
respondWithError(w, http.StatusBadRequest, "Malformed JSON token")
return
}
authorID, _, err := auth.ValidateJWT(tokenString, apiConfig.jwtSecret)
if err != nil {
respondWithError(w, http.StatusNotFound, "Invalid Token")
return
}
decoder := json.NewDecoder(req.Body)
type parameter struct {
Body string `json:"body"`
}
param := parameter{}
err = decoder.Decode(¶m)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Malformed JSON token")
return
}
chirp, err := apiConfig.DB.CreateChirp(param.Body, authorID)
type response struct {
Chirp
}
respondWithJSON(w, http.StatusCreated, response{
Chirp{
ID: chirp.ID,
Body: param.Body,
AuthorID: chirp.AuthorID,
},
})
}
func (apiConfig *apiConfig) handleGetChirps(w http.ResponseWriter, req *http.Request) {
var responseChirps []database.Chirp
var authorID int
authorIDString := req.URL.Query().Get("author_id")
chirps, err := apiConfig.DB.GetChirps()
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Internal error")
return
}
if authorIDString == "" {
responseChirps = chirps
} else {
authorID, err = strconv.Atoi(authorIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Internal error")
return
}
for _, v := range chirps {
if v.AuthorID == authorID {
chirp := database.Chirp{
ID: v.ID,
Body: v.Body,
AuthorID: v.AuthorID,
}
responseChirps = append(responseChirps, chirp)
}
}
}
sortCriteria := req.URL.Query().Get("sort")
if sortCriteria == "desc" {
sort.Slice(responseChirps, func(i, j int) bool {
return responseChirps[i].ID > responseChirps[j].ID
})
} else {
sort.Slice(responseChirps, func(i, j int) bool {
return responseChirps[i].ID < responseChirps[j].ID
})
}
respondWithJSON(w, http.StatusOK, responseChirps)
}
func (apiConfig *apiConfig) handleDeleteChirp(w http.ResponseWriter, req *http.Request) {
tokenString, canCut := getTokenFromHeader(req)
if !canCut {
respondWithError(w, http.StatusBadRequest, "Malformed JSON token")
return
}
authorID, _, err := auth.ValidateJWT(tokenString, apiConfig.jwtSecret)
if err != nil {
respondWithError(w, http.StatusNotFound, "Invalid Token")
return
}
paramID := chi.URLParam(req, "chirpID")
chirpID, err := strconv.Atoi(paramID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Malformed URL request")
return
}
chirp, err := apiConfig.DB.DeleteChirp(chirpID, authorID)
if err != nil {
respondWithJSON(w, http.StatusForbidden, "Cannot delete")
return
}
respondWithJSON(w, http.StatusOK, chirp)
}
func (apiConfig *apiConfig) getChirpsById(w http.ResponseWriter, req *http.Request) {
id := chi.URLParam(req, "chirpID")
chirps, err := apiConfig.DB.GetChirps()
if err != nil {
http.Error(w, "Internal Error", 500)
log.Fatal(err)
}
sort.Slice(chirps, func(i, j int) bool {
return chirps[i].ID < chirps[j].ID
})
chirpID, err := strconv.Atoi(id)
if err != nil {
http.Error(w, "Internal Error", 500)
log.Fatal(err)
}
for _, v := range chirps {
if chirpID == v.ID {
w.WriteHeader(200)
dat, _ := json.Marshal(v)
w.Write(dat)
fmt.Println(string(dat))
return
}
}
w.WriteHeader(404)
}