You've already forked reloading-manager
304 lines
7.2 KiB
Go
304 lines
7.2 KiB
Go
package bullets
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"git.siteworxpro.com/reloading-manager/backend/database"
|
|
"git.siteworxpro.com/reloading-manager/backend/handlers"
|
|
"git.siteworxpro.com/reloading-manager/backend/models/bullets"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type BulletResponse struct {
|
|
Id string `json:"id"`
|
|
Name string `json:"name"`
|
|
Diameter int32 `json:"diameter"`
|
|
Weight int32 `json:"weight"`
|
|
Manufacturer handlers.Manufacturer `json:"manufacturer"`
|
|
}
|
|
|
|
func Photo(c echo.Context) error {
|
|
db := c.(*database.CustomContext).Db
|
|
|
|
defer func() {
|
|
_ = db.Db.Close(context.Background())
|
|
}()
|
|
|
|
id := c.Param("id")
|
|
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
|
}
|
|
|
|
byId, err := db.Bullets.GetBulletById(context.Background(), *uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
mime := http.DetectContentType(byId.Photo)
|
|
|
|
c.Response().Header().Set("Content-Type", mime)
|
|
c.Response().Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
c.Response().Header().Set("Pragma", "no-cache")
|
|
c.Response().Header().Set("Expires", "0")
|
|
|
|
_, err = c.Response().Write(byId.Photo)
|
|
|
|
return err
|
|
}
|
|
|
|
func Delete(c echo.Context) error {
|
|
db := c.(*database.CustomContext).Db
|
|
|
|
defer func() {
|
|
_ = db.Db.Close(context.Background())
|
|
}()
|
|
|
|
id := c.Param("id")
|
|
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
|
if err != nil || uid == nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
|
}
|
|
|
|
err = db.Bullets.DeleteBullet(context.Background(), *uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, handlers.Response[string]{})
|
|
}
|
|
|
|
func Put(c echo.Context) error {
|
|
db := c.(*database.CustomContext).Db
|
|
defer func() {
|
|
_ = db.Db.Close(context.Background())
|
|
}()
|
|
|
|
id := c.Param("id")
|
|
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
|
if err != nil || uid == nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
|
}
|
|
|
|
byId, err := db.Bullets.GetBulletById(context.Background(), *uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
file, err := c.FormFile("photo")
|
|
var fileData []byte
|
|
if err == nil {
|
|
handler, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
buf := make([]byte, file.Size)
|
|
_, _ = handler.Read(buf)
|
|
fileData = buf
|
|
} else {
|
|
fileData = byId.Photo
|
|
}
|
|
|
|
var meta json.RawMessage = []byte(c.FormValue("meta"))
|
|
if len(meta) == 0 {
|
|
meta = []byte("{}")
|
|
}
|
|
weight, _ := strconv.ParseInt(c.FormValue("weight"), 10, 32)
|
|
diameter, _ := strconv.ParseInt(c.FormValue("diameter"), 10, 32)
|
|
manufacturerId := c.FormValue("manufacturer_id")
|
|
manufacturerUid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
|
if err == nil || manufacturerUid == nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
|
}
|
|
|
|
name := c.FormValue("name")
|
|
if name == "" {
|
|
_ = c.JSON(http.StatusBadRequest, struct {
|
|
Message string `json:"message"`
|
|
}{
|
|
Message: "Name is required.",
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
manufacturer, err := db.Manufacturer.GetById(context.Background(), *manufacturerUid)
|
|
if err != nil {
|
|
_ = c.JSON(http.StatusBadRequest, struct {
|
|
Message string `json:"message"`
|
|
}{
|
|
Message: "Invalid Manufacturer ID",
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
err = db.Bullets.UpdateBullet(context.Background(), bullets.UpdateBulletParams{
|
|
Name: name,
|
|
Weight: int32(weight),
|
|
Diameter: int32(diameter),
|
|
Photo: fileData,
|
|
Meta: meta,
|
|
ManufacturerID: manufacturer.ID,
|
|
ID: byId.ID,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Get(c echo.Context) error {
|
|
db := c.(*database.CustomContext).Db
|
|
defer func() {
|
|
_ = db.Db.Close(context.Background())
|
|
}()
|
|
|
|
if c.Param("id") != "" {
|
|
id := c.Param("id")
|
|
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
|
if err != nil || uid == nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
|
}
|
|
|
|
byId, err := db.Bullets.GetBulletById(context.Background(), *uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
response := handlers.Response[BulletResponse]{
|
|
Status: http.StatusText(http.StatusOK),
|
|
Payload: BulletResponse{
|
|
Id: uid.String(),
|
|
Name: byId.Name,
|
|
Diameter: byId.Diameter,
|
|
Weight: byId.Weight,
|
|
Manufacturer: handlers.Manufacturer{Id: byId.ManufactuererID.String(), Name: byId.ManufacutuerName, Url: byId.ManufacturerUrl.String},
|
|
},
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
bulletList, err := db.Bullets.ForPage(context.Background(), bullets.ForPageParams{
|
|
Limit: 100,
|
|
Offset: 0,
|
|
})
|
|
|
|
if bulletList == nil {
|
|
bulletList = make([]bullets.ForPageRow, 0)
|
|
}
|
|
|
|
var payload = make([]BulletResponse, 0)
|
|
for _, bullet := range bulletList {
|
|
payload = append(payload, BulletResponse{
|
|
Id: bullet.ID.String(),
|
|
Name: bullet.Name,
|
|
Weight: bullet.Weight,
|
|
Diameter: bullet.Diameter,
|
|
Manufacturer: handlers.Manufacturer{Id: bullet.ManufacturerID.String(), Name: bullet.ManufacturerName, Url: bullet.ManufacturerUrl.String},
|
|
})
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = c.JSON(http.StatusOK, handlers.Response[[]BulletResponse]{
|
|
Payload: payload,
|
|
Status: "OK",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Post(c echo.Context) error {
|
|
file, err := c.FormFile("photo")
|
|
if err != nil {
|
|
c.Logger().Error(err)
|
|
_ = c.JSON(http.StatusBadRequest, struct {
|
|
Message string `json:"message"`
|
|
}{
|
|
Message: "No file provided",
|
|
})
|
|
|
|
return nil
|
|
}
|
|
handler, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
buf := make([]byte, file.Size)
|
|
_, _ = handler.Read(buf)
|
|
|
|
weight, _ := strconv.ParseInt(c.FormValue("weight"), 10, 32)
|
|
diameter, _ := strconv.ParseInt(c.FormValue("diameter"), 10, 32)
|
|
manufacturerId := c.FormValue("manufacturer_id")
|
|
name := c.FormValue("name")
|
|
if name == "" {
|
|
_ = c.JSON(http.StatusBadRequest, struct {
|
|
Message string `json:"message"`
|
|
}{
|
|
Message: "Name is required.",
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
db := c.(*database.CustomContext).Db
|
|
defer func() {
|
|
_ = db.Db.Close(context.Background())
|
|
}()
|
|
|
|
manufacturerUid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
|
if err != nil || manufacturerUid == nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
|
}
|
|
|
|
manufacturer, err := db.Manufacturer.GetById(context.Background(), *manufacturerUid)
|
|
if err != nil {
|
|
_ = c.JSON(http.StatusBadRequest, struct {
|
|
Message string `json:"message"`
|
|
}{
|
|
Message: "Invalid Manufacturer ID",
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
var meta json.RawMessage = []byte(c.FormValue("meta"))
|
|
if len(meta) == 0 {
|
|
meta = []byte("{}")
|
|
}
|
|
|
|
id, err := db.Bullets.AddBullet(context.Background(), bullets.AddBulletParams{
|
|
Name: name,
|
|
Weight: int32(weight),
|
|
Diameter: int32(diameter),
|
|
Photo: buf,
|
|
Meta: meta,
|
|
ManufacturerID: manufacturer.ID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.JSON(http.StatusAccepted, handlers.Response[string]{
|
|
Status: http.StatusText(http.StatusAccepted),
|
|
Payload: id.String(),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|