You've already forked gun-manager-backend
add LICENSE file and update README with application image
This commit is contained in:
271
Handlers/Guns/index.go
Normal file
271
Handlers/Guns/index.go
Normal file
@@ -0,0 +1,271 @@
|
||||
package Guns
|
||||
|
||||
import (
|
||||
"context"
|
||||
sql2 "database/sql"
|
||||
"git.siteworxpro.com/gun-manager/Handlers"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"git.siteworxpro.com/gun-manager/sql/db"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type gunResp struct {
|
||||
Status string `json:"status"`
|
||||
PayLoad []gunPayload `json:"payload"`
|
||||
}
|
||||
|
||||
type gunPayload struct {
|
||||
Id int64 `json:"id"`
|
||||
Make string `json:"make"`
|
||||
Model string `json:"model"`
|
||||
ValueAmount int64 `json:"value_amount"`
|
||||
}
|
||||
|
||||
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"`
|
||||
PurchaseAmount int64 `json:"purchase_amount" validate:"required"`
|
||||
ValueAmount int64 `json:"value_amount" validate:"required"`
|
||||
DatePurchased string `json:"date_purchased" validate:"required"`
|
||||
Notes string `json:"notes" validate:"required"`
|
||||
}
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
gun := new(gunPost)
|
||||
err := c.Bind(gun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
insertRow := db.InsertGunParams{
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
id, err := getDb.Queries.InsertGun(context.Background(), insertRow)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 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 {
|
||||
return nil
|
||||
}
|
||||
|
||||
type photo struct {
|
||||
Id int64 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
}
|
||||
|
||||
type gunResponse struct {
|
||||
Id int64 `json:"id"`
|
||||
Make string `json:"make"`
|
||||
Model string `json:"model"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
PurchaseAmount int64 `json:"purchase_amount"`
|
||||
ValueAmount int64 `json:"value_amount"`
|
||||
DatePurchased string `json:"date_purchased"`
|
||||
Notes string `json:"notes"`
|
||||
Photos []photo `json:"photos"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
photos, err := getDb.Queries.GetGunPhotos(context.Background(), sql2.NullInt64{Int64: byId.ID, Valid: true})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
respData := gunResponse{
|
||||
Id: id,
|
||||
Make: byId.Make.String,
|
||||
Model: byId.Model.String,
|
||||
SerialNumber: byId.SerialNumber.String,
|
||||
PurchaseAmount: byId.PurchaseAmount.Int64,
|
||||
ValueAmount: byId.ValueAmount.Int64,
|
||||
DatePurchased: byId.DatePurchased.String,
|
||||
Notes: byId.Notes.String,
|
||||
}
|
||||
|
||||
for _, photoData := range photos {
|
||||
respData.Photos = append(respData.Photos, photo{
|
||||
Id: photoData.ID,
|
||||
FileName: photoData.Filename.String,
|
||||
})
|
||||
}
|
||||
|
||||
resp := Handlers.Response[gunResponse]{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
Payload: respData,
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusOK, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
|
||||
getDb := sql.GetDb()
|
||||
guns, err := getDb.Queries.GetAllGuns(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var resp gunResp
|
||||
resp.Status = http.StatusText(http.StatusOK)
|
||||
for _, gun := range guns {
|
||||
resp.PayLoad = append(
|
||||
resp.PayLoad,
|
||||
gunPayload{
|
||||
Id: gun.ID,
|
||||
Make: gun.Make.String,
|
||||
Model: gun.Model.String,
|
||||
ValueAmount: gun.ValueAmount.Int64,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusOK, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user