From e2c8b1bcd93d8830629e5c1a21976610ca6b9364 Mon Sep 17 00:00:00 2001 From: wagslane Date: Tue, 30 May 2023 21:42:09 -0600 Subject: [PATCH] working notely --- handler_notes.go | 62 ---------------------- internal/database/notes.sql.go | 26 --------- main.go | 3 +- sql/queries/notes.sql | 8 --- static/index.html | 96 ++++++++++++++++++++++------------ 5 files changed, 63 insertions(+), 132 deletions(-) diff --git a/handler_notes.go b/handler_notes.go index 4210221..b3f6848 100644 --- a/handler_notes.go +++ b/handler_notes.go @@ -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(¶ms) - 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"` diff --git a/internal/database/notes.sql.go b/internal/database/notes.sql.go index 4314f1c..d17361d 100644 --- a/internal/database/notes.sql.go +++ b/internal/database/notes.sql.go @@ -34,16 +34,6 @@ func (q *Queries) CreateNote(ctx context.Context, arg CreateNoteParams) error { return err } -const deleteNote = `-- name: DeleteNote :exec - -DELETE FROM notes WHERE id = ? -` - -func (q *Queries) DeleteNote(ctx context.Context, id string) error { - _, err := q.db.ExecContext(ctx, deleteNote, id) - return err -} - const getNote = `-- name: GetNote :one SELECT id, created_at, updated_at, note, user_id FROM notes WHERE id = ? @@ -95,19 +85,3 @@ func (q *Queries) GetNotesForUser(ctx context.Context, userID string) ([]Note, e } return items, nil } - -const updateNote = `-- name: UpdateNote :exec - -UPDATE notes SET updated_at = ?, note = ? WHERE id = ? -` - -type UpdateNoteParams struct { - UpdatedAt time.Time - Note string - ID string -} - -func (q *Queries) UpdateNote(ctx context.Context, arg UpdateNoteParams) error { - _, err := q.db.ExecContext(ctx, updateNote, arg.UpdatedAt, arg.Note, arg.ID) - return err -} diff --git a/main.go b/main.go index 8679f45..08665d2 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ func main() { } dbQueries := database.New(db) apiCfg.DB = dbQueries + log.Println("Connected to database!") } router := chi.NewRouter() @@ -85,8 +86,6 @@ func main() { v1Router.Get("/users", apiCfg.middlewareAuth(apiCfg.handlerUsersGet)) v1Router.Get("/notes", apiCfg.middlewareAuth(apiCfg.handlerNotesGet)) v1Router.Post("/notes", apiCfg.middlewareAuth(apiCfg.handlerNotesCreate)) - v1Router.Delete("/notes/{noteID}", apiCfg.middlewareAuth(apiCfg.handlerNotesDelete)) - v1Router.Put("/notes/{noteID}", apiCfg.middlewareAuth(apiCfg.handlerNotesUpdate)) } v1Router.Get("/healthz", handlerReadiness) diff --git a/sql/queries/notes.sql b/sql/queries/notes.sql index c70f4b1..72b50bc 100644 --- a/sql/queries/notes.sql +++ b/sql/queries/notes.sql @@ -10,11 +10,3 @@ SELECT * FROM notes WHERE id = ?; -- name: GetNotesForUser :many SELECT * FROM notes WHERE user_id = ?; -- - --- name: UpdateNote :exec -UPDATE notes SET updated_at = ?, note = ? WHERE id = ?; --- - --- name: DeleteNote :exec -DELETE FROM notes WHERE id = ?; --- diff --git a/static/index.html b/static/index.html index 33ac421..1e94b5f 100644 --- a/static/index.html +++ b/static/index.html @@ -6,38 +6,31 @@ Notely - +

Notely

- -

Create Note

- - +
+ + +
-

Your Notes

-
+