You've already forked gun-manager-backend
357 lines
7.1 KiB
Go
357 lines
7.1 KiB
Go
package Guns
|
|
|
|
import (
|
|
"bytes"
|
|
"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"
|
|
"github.com/nfnt/resize"
|
|
"image/jpeg"
|
|
"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 {
|
|
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,
|
|
},
|
|
Model: sql2.NullString{
|
|
String: gun.Model,
|
|
},
|
|
SerialNumber: sql2.NullString{
|
|
String: gun.SerialNumber,
|
|
},
|
|
PurchaseAmount: sql2.NullInt64{
|
|
Int64: gun.PurchaseAmount,
|
|
},
|
|
ValueAmount: sql2.NullInt64{
|
|
Int64: gun.ValueAmount,
|
|
},
|
|
DatePurchased: sql2.NullString{
|
|
String: gun.DatePurchased,
|
|
},
|
|
Notes: sql2.NullString{
|
|
String: gun.Notes,
|
|
},
|
|
}
|
|
|
|
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 GetPhotoResize(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
|
|
}
|
|
|
|
size, err := strconv.ParseInt(c.Param("size"), 10, 64)
|
|
if err != nil {
|
|
err := c.JSON(http.StatusNotFound, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
getDb := sql.GetDb()
|
|
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
img, err := jpeg.Decode(bytes.NewBuffer(photo))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m := resize.Resize(uint(size), 0, img, resize.Bicubic)
|
|
image := bytes.NewBuffer(make([]byte, 0))
|
|
err = jpeg.Encode(image, m, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = c.String(http.StatusOK, image.String())
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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 {
|
|
err := c.JSON(http.StatusNotFound, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
getDb := sql.GetDb()
|
|
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.String(http.StatusOK, string(photo))
|
|
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 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
|
|
}
|