From 04ebe9e85519c87eb44f5f7474495dbc55566395 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Mon, 7 Aug 2023 20:38:50 -0400 Subject: [PATCH] Never gonna let you down --- Handlers/Guns/Index.go | 116 ++++++++++++++++++++++++++++++++++++++--- main.go | 3 ++ sql/db/query.sql.go | 42 +++++++++++++++ sql/query.sql | 13 ++++- 4 files changed, 166 insertions(+), 8 deletions(-) diff --git a/Handlers/Guns/Index.go b/Handlers/Guns/Index.go index 43d35c2..77348d2 100644 --- a/Handlers/Guns/Index.go +++ b/Handlers/Guns/Index.go @@ -73,9 +73,13 @@ func Post(c echo.Context) error { return err } - err = c.JSON(http.StatusAccepted, Handlers.Response[struct{ Id int64 }]{ - Status: http.StatusText(http.StatusOK), - Payload: struct{ Id int64 }{Id: id}, + err = c.JSON(http.StatusAccepted, Handlers.Response[struct { + Id int64 `json:"id"` + }]{ + Status: http.StatusText(http.StatusOK), + Payload: struct { + Id int64 `json:"id"` + }{Id: id}, }) if err != nil { return err @@ -129,6 +133,78 @@ func GetPhotoResize(c echo.Context) error { return nil } +func DeletePhoto(c echo.Context) error { + id, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil { + return nil + } + + getDb := sql.GetDb() + err = getDb.Queries.DeleteGunPhotosById(context.Background(), id) + if err != nil { + return err + } + + err = c.JSON(http.StatusNoContent, "") + if err != nil { + return err + } + + return nil +} + +func PostPhoto(c echo.Context) error { + id, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil { + err := c.JSON(http.StatusNotFound, "") + if err != nil { + return err + } + return nil + } + files, err := c.MultipartForm() + if err != nil { + return err + } + + getDb := sql.GetDb() + byId, err := getDb.Queries.GetGunById(context.Background(), id) + if err != nil { + return err + } + + for _, handler := range files.File { + for _, file := range handler { + open, err := file.Open() + if err != nil { + return err + } + fileData := make([]byte, file.Size) + _, err = open.Read(fileData) + if err != nil { + return err + } + + err = getDb.Queries.InsertGunPhoto(context.Background(), db.InsertGunPhotoParams{ + GunID: sql2.NullInt64{ + Int64: byId.ID, + Valid: true, + }, + Filename: sql2.NullString{ + String: file.Filename, + Valid: true, + }, + Photo: fileData, + }) + if err != nil { + return err + } + } + } + + return nil +} + func GetPhoto(c echo.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { @@ -153,13 +229,35 @@ func GetPhoto(c echo.Context) error { return nil } +func DeleteById(c echo.Context) error { + id, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil { + return err + } + getDb := sql.GetDb() + err = getDb.Queries.DeleteGunPhotosByGunId(context.Background(), sql2.NullInt64{ + Int64: id, + Valid: true, + }) + if err != nil { + return err + } + err = getDb.Queries.DeleteGun(context.Background(), id) + if err != nil { + return err + } + + err = c.JSON(http.StatusNoContent, "") + if err != nil { + return err + } + + return nil +} + func GetById(c echo.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - err := c.JSON(http.StatusNotFound, "") - if err != nil { - return err - } return nil } @@ -183,6 +281,10 @@ func GetById(c echo.Context) error { getDb := sql.GetDb() byId, err := getDb.Queries.GetGunById(context.Background(), id) if err != nil { + err := c.JSON(http.StatusNotFound, "") + if err != nil { + return err + } return err } diff --git a/main.go b/main.go index e0a7517..bc39732 100644 --- a/main.go +++ b/main.go @@ -37,8 +37,11 @@ func main() { e.GET("/gun", Guns.Get) e.GET("/gun/:id", Guns.GetById) + e.DELETE("/gun/:id", Guns.DeleteById) e.POST("/gun", Guns.Post) e.GET("/gun/photo/:id/:filename", Guns.GetPhoto) + e.POST("/gun/photo/:id", Guns.PostPhoto) + e.DELETE("/gun/photo/:id", Guns.DeletePhoto) e.GET("/gun/photo/:id/:size/:filename", Guns.GetPhotoResize) e.Logger.Fatal(e.Start(":8000")) diff --git a/sql/db/query.sql.go b/sql/db/query.sql.go index 5859511..f7de3ba 100644 --- a/sql/db/query.sql.go +++ b/sql/db/query.sql.go @@ -10,6 +10,33 @@ import ( "database/sql" ) +const deleteGun = `-- name: DeleteGun :exec +DELETE from guns where id = ? +` + +func (q *Queries) DeleteGun(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteGun, id) + return err +} + +const deleteGunPhotosByGunId = `-- name: DeleteGunPhotosByGunId :exec +DELETE FROM photos where gun_id = ? +` + +func (q *Queries) DeleteGunPhotosByGunId(ctx context.Context, gunID sql.NullInt64) error { + _, err := q.db.ExecContext(ctx, deleteGunPhotosByGunId, gunID) + return err +} + +const deleteGunPhotosById = `-- name: DeleteGunPhotosById :exec +DELETE FROM photos where id = ? +` + +func (q *Queries) DeleteGunPhotosById(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteGunPhotosById, id) + return err +} + const getAllGuns = `-- name: GetAllGuns :many SELECT id, make, model, value_amount from guns @@ -146,3 +173,18 @@ func (q *Queries) InsertGun(ctx context.Context, arg InsertGunParams) (int64, er err := row.Scan(&id) return id, err } + +const insertGunPhoto = `-- name: InsertGunPhoto :exec +INSERT into photos (gun_id, photo, filename) values (?, ?, ?) +` + +type InsertGunPhotoParams struct { + GunID sql.NullInt64 `json:"gun_id"` + Photo []byte `json:"photo"` + Filename sql.NullString `json:"filename"` +} + +func (q *Queries) InsertGunPhoto(ctx context.Context, arg InsertGunPhotoParams) error { + _, err := q.db.ExecContext(ctx, insertGunPhoto, arg.GunID, arg.Photo, arg.Filename) + return err +} diff --git a/sql/query.sql b/sql/query.sql index d45b63e..b9a7633 100644 --- a/sql/query.sql +++ b/sql/query.sql @@ -17,5 +17,16 @@ order by id desc; -- name: InsertGun :one INSERT into guns (make, model, serial_number, purchase_amount, value_amount, date_purchased, notes) VALUES - (?, ?, ?, ?, ?, ?, ?) returning id + (?, ?, ?, ?, ?, ?, ?) returning id; +-- name: DeleteGun :exec +DELETE from guns where id = ?; + +-- name: DeleteGunPhotosByGunId :exec +DELETE FROM photos where gun_id = ?; + +-- name: DeleteGunPhotosById :exec +DELETE FROM photos where id = ?; + +-- name: InsertGunPhoto :exec +INSERT into photos (gun_id, photo, filename) values (?, ?, ?); \ No newline at end of file