From bad1d140ee1a361f190332c0f782cbcab030cb95 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Tue, 11 Mar 2025 23:04:05 +0200 Subject: [PATCH] Delete chirps --- internal/database/DeleteChirpByID.sql.go | 21 +++++++++++ main.go | 45 ++++++++++++++++++++++++ sql/queries/DeleteChirpByID.sql | 2 ++ 3 files changed, 68 insertions(+) create mode 100644 internal/database/DeleteChirpByID.sql.go create mode 100644 sql/queries/DeleteChirpByID.sql diff --git a/internal/database/DeleteChirpByID.sql.go b/internal/database/DeleteChirpByID.sql.go new file mode 100644 index 0000000..4da0b59 --- /dev/null +++ b/internal/database/DeleteChirpByID.sql.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.28.0 +// source: DeleteChirpByID.sql + +package database + +import ( + "context" + + "github.com/google/uuid" +) + +const deleteChirpByID = `-- name: DeleteChirpByID :exec +DELETE FROM chirps WHERE id = $1 +` + +func (q *Queries) DeleteChirpByID(ctx context.Context, id uuid.UUID) error { + _, err := q.db.ExecContext(ctx, deleteChirpByID, id) + return err +} diff --git a/main.go b/main.go index 741b88b..abf8a32 100644 --- a/main.go +++ b/main.go @@ -139,6 +139,50 @@ func getChirpByIDHandler(cfg *apiConfig) http.HandlerFunc { } } +func deleteChirpByIDHandler(cfg *apiConfig) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + token, err := auth.GetBearerToken(r.Header) + if err != nil { + httpResponse.JSONHandler(w, http.StatusUnauthorized, `{"error": "Unauthorized"}`) + return + } + + userId, err := auth.ValidateJWT(token, cfg.secret) + + _, err = cfg.db.GetUserByID(context.Background(), userId) + if err != nil { + httpResponse.JSONHandler(w, http.StatusUnauthorized, `{"error": "Unauthorized"}`) + return + } + + chirpID, err := uuid.Parse(r.PathValue("chirpID")) + if err != nil { + httpResponse.JSONHandler(w, http.StatusBadRequest, `{"error": "InvalidChirpID"}`) + return + } + + chirp, err := cfg.db.GetChirpByID(context.Background(), chirpID) + if err != nil { + httpResponse.JSONHandler(w, http.StatusNotFound, `{"error": "ChirpNotFound"}`) + return + } + + if chirp.UserID != userId { + httpResponse.JSONHandler(w, http.StatusForbidden, `{"error": "Forbidden"}`) + return + } + + err = cfg.db.DeleteChirpByID(context.Background(), chirp.ID) + if err != nil { + httpResponse.SomethingWentWrong(w) + return + } + + httpResponse.PlainTextHandler(w, http.StatusNoContent, "") + } +} + func updateUserDatahandler(cfg *apiConfig) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { type parameters struct { @@ -477,6 +521,7 @@ func main() { mux.HandleFunc("POST /api/chirps", postChirpsHandler(apiCfg)) mux.HandleFunc("GET /api/chirps", getAllChirpsHandler(apiCfg)) mux.HandleFunc("GET /api/chirps/{chirpID}", getChirpByIDHandler(apiCfg)) + mux.HandleFunc("DELETE /api/chirps/{chirpID}", deleteChirpByIDHandler(apiCfg)) mux.HandleFunc("PUT /api/users", updateUserDatahandler(apiCfg)) mux.HandleFunc("GET /admin/metrics", adminMetricsHandler(apiCfg)) diff --git a/sql/queries/DeleteChirpByID.sql b/sql/queries/DeleteChirpByID.sql new file mode 100644 index 0000000..c709b15 --- /dev/null +++ b/sql/queries/DeleteChirpByID.sql @@ -0,0 +1,2 @@ +-- name: DeleteChirpByID :exec +DELETE FROM chirps WHERE id = $1; \ No newline at end of file