From fdc22b8896064b88ce47b458990b9c2e9319226d Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Mon, 7 Aug 2023 22:01:06 -0400 Subject: [PATCH] I don't know why. Just move on. --- Handlers/Guns/Index.go | 52 ++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 53 insertions(+) diff --git a/Handlers/Guns/Index.go b/Handlers/Guns/Index.go index c0480be..b30ecac 100644 --- a/Handlers/Guns/Index.go +++ b/Handlers/Guns/Index.go @@ -24,6 +24,7 @@ type gunPayload struct { } type gunPost struct { + Id int64 `json:"id"` Make string `json:"make" validate:"required"` Model string `json:"model" validate:"required"` SerialNumber string `json:"serial_number" validate:"required"` @@ -118,6 +119,57 @@ func DeleteById(c echo.Context) error { return nil } +func UpdateGun(c echo.Context) error { + gun := new(gunPost) + err := c.Bind(gun) + if err != nil { + return err + } + + getDb := sql.GetDb() + err = getDb.Queries.UpdateGun(context.Background(), db.UpdateGunParams{ + Make: sql2.NullString{ + String: gun.Make, + Valid: true, + }, + Model: sql2.NullString{ + String: gun.Model, + Valid: true, + }, + SerialNumber: sql2.NullString{ + String: gun.SerialNumber, + Valid: true, + }, + PurchaseAmount: sql2.NullInt64{ + Int64: gun.PurchaseAmount, + Valid: true, + }, + ValueAmount: sql2.NullInt64{ + Int64: gun.ValueAmount, + Valid: true, + }, + DatePurchased: sql2.NullString{ + String: gun.DatePurchased, + Valid: true, + }, + Notes: sql2.NullString{ + String: gun.Notes, + Valid: true, + }, + ID: gun.Id, + }) + if err != nil { + return err + } + + err = c.JSON(http.StatusOK, "") + 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 { diff --git a/main.go b/main.go index 7d477b3..d75a574 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ func main() { })) e.GET("/gun", Guns.Get) + e.PUT("/gun/:id", Guns.UpdateGun) e.GET("/gun/:id", Guns.GetById) e.DELETE("/gun/:id", Guns.DeleteById) e.POST("/gun", Guns.Post)