-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchirp-create.go
More file actions
117 lines (95 loc) · 2.67 KB
/
chirp-create.go
File metadata and controls
117 lines (95 loc) · 2.67 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
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"strings"
"time"
"github.com/google/uuid"
"github.com/mdmdj/bootdev-chirpy/internal/auth"
"github.com/mdmdj/bootdev-chirpy/internal/database"
)
type chirpCreateRequest struct {
Body string `json:"body"`
//UserID uuid.UUID `json:"user_id"`
}
type Chirp struct {
ID uuid.UUID `json:"id"`
Body string `json:"body"`
UserID uuid.UUID `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func chirpCreateRoute(w http.ResponseWriter, r *http.Request) {
requestParams := chirpCreateRequest{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&requestParams)
if err != nil {
log.Printf("Error decoding chirpCreateRoute parameters: %s", err)
respondWithError(w, http.StatusBadRequest, "Something went wrong")
return
}
chirpBody, err := chirpValidate(requestParams.Body)
if err != nil {
log.Printf("Error validating chirp: %s", err)
respondWithError(w, http.StatusBadRequest, "Chirp is invalid")
return
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
log.Printf("Error in GetBearerToken: %s", err)
respondWithError(w, http.StatusUnauthorized, "Something went wrong")
return
}
userID, err := auth.ValidateJWT(token, cfg.secret)
if err != nil {
log.Printf("Error in ValidateJWT: %s", err)
respondWithError(w, http.StatusUnauthorized, "Something went wrong")
return
}
dbChirp, err := cfg.db.CreateChirp(
r.Context(),
database.CreateChirpParams{
UserID: userID,
Body: chirpBody,
})
if err != nil {
log.Printf("Error creating chirp: %s %s %s", err, userID, requestParams.Body)
respondWithError(w, http.StatusInternalServerError, "Something went wrong")
return
}
// Map database.User to main.User
chirp := Chirp{
ID: dbChirp.ID,
UserID: dbChirp.UserID,
Body: dbChirp.Body,
CreatedAt: dbChirp.CreatedAt,
UpdatedAt: dbChirp.UpdatedAt,
}
respondWithJSON(w, http.StatusCreated, chirp)
}
func chirpValidate(input string) (result string, err error) {
chirpValid := (len(input) <= 140)
if !chirpValid {
log.Printf("Chirp is too long: %s", input)
err = errors.New("Chirp is too long")
return
}
result = chirpClean(input)
return result, nil
}
func chirpClean(incoming string) (result string) {
incomingSplit := strings.Split(incoming, " ")
incomingLower := strings.ToLower(incoming)
incomingLowerSplit := strings.Split(incomingLower, " ")
badWords := []string{"kerfuffle", "sharbert", "fornax"}
for i, v := range incomingLowerSplit {
for _, w := range badWords {
if v == w {
incomingSplit[i] = "****"
}
}
}
return strings.Join(incomingSplit, " ")
}