-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostChirps.go
127 lines (107 loc) · 2.94 KB
/
postChirps.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
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"strings"
"github.com/adamhu714/chirpy/internal/database"
)
func (cfg *apiConfig) handlerPostChirps(w http.ResponseWriter, r *http.Request) {
authorId, err := cfg.GetIDFromAccessToken(w, r)
if err != nil {
log.Printf("handlerPostChirps - Error getting id from token: %s", err.Error())
return
}
body, err := validateChirp(w, r)
if err != nil {
return
}
// connect database
db, err := database.NewDB("database.json")
if err != nil {
log.Printf("Error connecting database: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
err = db.CreateChirp(body, authorId)
if err != nil {
log.Printf("Error adding chirp to database: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
chirps, err := db.GetChirps()
if err != nil {
log.Printf("Error adding chirp to database: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
respChirp := chirps[len(chirps)-1]
data, err := json.Marshal(respChirp)
if err != nil {
log.Printf("Error while json marshalling: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(data)
}
func validateChirp(w http.ResponseWriter, r *http.Request) (string, error) {
type requestParams struct {
Body string `json:"body"`
}
type errorStruct struct {
Error string `json:"error"`
}
var requestBody requestParams
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&requestBody)
if err != nil {
log.Printf("Error while json decoding: %s", err.Error())
respBody := errorStruct{
Error: "Something went wrong",
}
respondWithJSON(w, http.StatusInternalServerError, respBody)
return "", err
}
if len(requestBody.Body) == 0 {
respBody := errorStruct{
Error: "Chirp message not provided",
}
respondWithJSON(w, http.StatusBadRequest, respBody)
return "", errors.New("chirp message not provided")
}
if len(requestBody.Body) > 140 {
respBody := errorStruct{
Error: "Chirp is too long",
}
respondWithJSON(w, http.StatusBadRequest, respBody)
return "", errors.New("chirp is too long")
}
splitBody := strings.Split(requestBody.Body, " ")
for i := 0; i < len(splitBody); i++ {
if strings.ToLower(splitBody[i]) == "kerfuffle" {
splitBody[i] = "****"
}
if strings.ToLower(splitBody[i]) == "sharbert" {
splitBody[i] = "****"
}
if strings.ToLower(splitBody[i]) == "fornax" {
splitBody[i] = "****"
}
}
cleanedBody := strings.Join(splitBody, " ")
return cleanedBody, nil
}
func respondWithJSON(w http.ResponseWriter, code int, respBody interface{}) {
dat, err := json.Marshal(respBody)
if err != nil {
log.Printf("Error while json marshalling: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(dat)
}