working notely

This commit is contained in:
wagslane
2023-05-30 21:42:09 -06:00
parent 80ec635705
commit e2c8b1bcd9
5 changed files with 63 additions and 132 deletions

View File

@@ -6,7 +6,6 @@ import (
"time"
"github.com/bootdotdev/learn-cicd-starter/internal/database"
"github.com/go-chi/chi"
"github.com/google/uuid"
)
@@ -20,67 +19,6 @@ func (cfg *apiConfig) handlerNotesGet(w http.ResponseWriter, r *http.Request, us
respondWithJSON(w, http.StatusOK, databasePostsToPosts(posts))
}
func (cfg *apiConfig) handlerNotesUpdate(w http.ResponseWriter, r *http.Request, user database.User) {
type parameters struct {
Note string `json:"note"`
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err := decoder.Decode(&params)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
return
}
noteID := chi.URLParam(r, "noteID")
note, err := cfg.DB.GetNote(r.Context(), noteID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get note")
return
}
if note.UserID != user.ID {
respondWithError(w, http.StatusForbidden, "Can't update another user's note")
return
}
err = cfg.DB.UpdateNote(r.Context(), database.UpdateNoteParams{
UpdatedAt: time.Now().UTC(),
Note: params.Note,
ID: noteID,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't update note")
return
}
note, err = cfg.DB.GetNote(r.Context(), noteID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get note")
return
}
respondWithJSON(w, http.StatusOK, databaseNoteToNote(note))
}
func (cfg *apiConfig) handlerNotesDelete(w http.ResponseWriter, r *http.Request, user database.User) {
noteID := chi.URLParam(r, "noteID")
note, err := cfg.DB.GetNote(r.Context(), noteID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get note")
return
}
if note.UserID != user.ID {
respondWithError(w, http.StatusForbidden, "Can't update another user's note")
return
}
err = cfg.DB.DeleteNote(r.Context(), noteID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't delete note")
return
}
respondWithJSON(w, http.StatusOK, struct{}{})
}
func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request, user database.User) {
type parameters struct {
Note string `json:"note"`