chirpy_server/chirp.go

170 lines
3.8 KiB
Go
Raw Normal View History

2024-10-16 21:34:04 +02:00
package main
import (
2024-10-23 22:16:51 +02:00
"database/sql"
2024-10-16 21:34:04 +02:00
"encoding/json"
2024-10-23 22:16:51 +02:00
"errors"
2024-10-16 21:34:04 +02:00
"log"
"net/http"
"strings"
2024-10-23 22:16:51 +02:00
"time"
2024-10-25 23:31:57 +02:00
"github.com/finchrelia/chirpy-server/internal/auth"
2024-10-23 22:16:51 +02:00
"github.com/finchrelia/chirpy-server/internal/database"
"github.com/google/uuid"
2024-10-16 21:34:04 +02:00
)
2024-10-23 22:16:51 +02:00
type Chirp struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
UserID uuid.UUID `json:"user_id"`
Body string `json:"body"`
}
func (cfg *apiConfig) chirpsCreate(w http.ResponseWriter, r *http.Request) {
2024-10-16 21:34:04 +02:00
type parameters struct {
2024-10-25 23:31:57 +02:00
Content string `json:"body"`
2024-10-16 21:34:04 +02:00
}
2024-10-25 23:31:57 +02:00
token, err := auth.GetBearerToken(r.Header)
if err != nil {
log.Printf("Error extracting token: %s", err)
w.WriteHeader(401)
return
}
userId, err := auth.ValidateJWT(token, cfg.JWT)
if err != nil {
log.Printf("Invalid JWT: %s", err)
w.WriteHeader(401)
return
2024-10-16 21:34:04 +02:00
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
2024-10-25 23:31:57 +02:00
err = decoder.Decode(&params)
2024-10-16 21:34:04 +02:00
if err != nil {
log.Printf("Error decoding parameters: %s", err)
w.WriteHeader(500)
return
} else if len(params.Content) > 140 {
type errorVals struct {
Data string `json:"error"`
}
respBody := errorVals{
Data: "Chirp is too long",
}
dat, err := json.Marshal(respBody)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(400)
w.Write(dat)
} else {
cleanedData := cleanText(params.Content)
2024-10-23 22:16:51 +02:00
chirp, err := cfg.DB.CreateChirp(r.Context(), database.CreateChirpParams{
Body: cleanedData,
2024-10-25 23:31:57 +02:00
UserID: userId,
2024-10-23 22:16:51 +02:00
})
if err != nil {
log.Printf("Error creating chirp: %s", err)
w.WriteHeader(500)
return
2024-10-16 21:34:04 +02:00
}
2024-10-23 22:16:51 +02:00
dat, err := json.Marshal(Chirp{
ID: chirp.ID,
CreatedAt: chirp.CreatedAt,
UpdatedAt: chirp.UpdatedAt,
Body: chirp.Body,
UserID: chirp.UserID,
})
2024-10-16 21:34:04 +02:00
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
2024-10-23 22:16:51 +02:00
w.WriteHeader(201)
2024-10-16 21:34:04 +02:00
w.Write(dat)
}
}
func cleanText(s string) string {
splittedString := strings.Split(s, " ")
for idx, element := range splittedString {
switch strings.ToLower(element) {
case
"kerfuffle",
"sharbert",
"fornax":
splittedString[idx] = "****"
}
}
return strings.Join(splittedString, " ")
}
2024-10-23 22:16:51 +02:00
func (cfg *apiConfig) getChirps(w http.ResponseWriter, r *http.Request) {
chirps, err := cfg.DB.GetChirps(r.Context())
if err != nil {
log.Printf("Error getting chirps: %s", err)
w.WriteHeader(500)
return
}
newChirps := []Chirp{}
for _, chirp := range chirps {
newChirps = append(newChirps, Chirp{
ID: chirp.ID,
CreatedAt: chirp.CreatedAt,
UpdatedAt: chirp.UpdatedAt,
Body: chirp.Body,
UserID: chirp.UserID,
})
}
dat, err := json.Marshal(newChirps)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(dat)
}
func (cfg *apiConfig) getChirp(w http.ResponseWriter, r *http.Request) {
idFromQuery := r.PathValue("chirpID")
id, err := uuid.Parse(idFromQuery)
if err != nil {
log.Printf("Not a valid ID: %s", err)
w.WriteHeader(500)
return
}
chirp, err := cfg.DB.GetChirp(r.Context(), id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
w.WriteHeader(http.StatusNotFound)
return
}
log.Printf("Error getting chirp: %s", err)
w.WriteHeader(500)
return
}
dat, err := json.Marshal(Chirp{
ID: chirp.ID,
CreatedAt: chirp.CreatedAt,
UpdatedAt: chirp.UpdatedAt,
Body: chirp.Body,
UserID: chirp.UserID,
})
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(dat)
}