You've already forked reloading-manager
No changes made
This commit is contained in:
217
backend/handlers/powder/handler.go
Normal file
217
backend/handlers/powder/handler.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package powder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.s.int/reloading-manager/backend/database"
|
||||
"git.s.int/reloading-manager/backend/handlers"
|
||||
"git.s.int/reloading-manager/backend/models/powder"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Photo(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
|
||||
id := c.Param("id")
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "Invalid UUID")
|
||||
}
|
||||
|
||||
byId, err := db.Powder.GetPowderById(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 {
|
||||
id := c.Param("id")
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "Invalid UUID")
|
||||
}
|
||||
|
||||
err = db.Powder.DeletePowder(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, handlers.Response[string]{})
|
||||
}
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "Invalid UUID")
|
||||
}
|
||||
|
||||
p, err := db.Powder.GetPowderById(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return handlers.NotFound(c, "Not found")
|
||||
}
|
||||
|
||||
file, _ := c.FormFile("photo")
|
||||
if file != nil {
|
||||
handler, _ := file.Open()
|
||||
buf := make([]byte, file.Size)
|
||||
_, _ = handler.Read(buf)
|
||||
p.Photo = buf
|
||||
}
|
||||
|
||||
if c.FormValue("name") != "" {
|
||||
p.Name = c.FormValue("name")
|
||||
}
|
||||
|
||||
muid, err := handlers.ParseUuid(c.FormValue("manufacturer_id"))
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "Invalid UUID")
|
||||
}
|
||||
|
||||
if c.FormValue("manufacturer_id") != "" {
|
||||
p.ManufacturerID = *muid
|
||||
}
|
||||
|
||||
metaString := c.FormValue("meta")
|
||||
if metaString == "" {
|
||||
metaString = string(p.Meta)
|
||||
}
|
||||
|
||||
err = db.Powder.UpdatePowder(context.Background(), powder.UpdatePowderParams{
|
||||
ID: p.ID,
|
||||
Name: p.Name,
|
||||
ManufacturerID: p.ManufacturerID,
|
||||
Photo: p.Photo,
|
||||
Meta: []byte(metaString),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, handlers.Response[string]{})
|
||||
}
|
||||
|
||||
buf, err := handlers.ReadFile(c, "photo")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newUuid := uuid.New().String()
|
||||
pgUuid, _ := handlers.ParseUuid(newUuid)
|
||||
mfUid, _ := handlers.ParseUuid(c.FormValue("manufacturer_id"))
|
||||
|
||||
jsonMap := make(map[string]interface{})
|
||||
jsonMap["name"] = c.FormValue("name")
|
||||
if jsonMap["name"] == nil || jsonMap["name"] == "" {
|
||||
return handlers.BadRequest(c, "Name is required.")
|
||||
}
|
||||
|
||||
jsonMap["manufacturer_id"] = c.FormValue("manufacturer_id")
|
||||
if jsonMap["manufacturer_id"] == nil || jsonMap["manufacturer_id"] == "" {
|
||||
return handlers.BadRequest(c, "manufacturer_id is required.")
|
||||
}
|
||||
|
||||
metaString := c.FormValue("meta")
|
||||
if metaString == "" {
|
||||
metaString = "{}"
|
||||
}
|
||||
var meta json.RawMessage = []byte(metaString)
|
||||
err = db.Powder.InsertPowder(context.Background(), powder.InsertPowderParams{
|
||||
ID: *pgUuid,
|
||||
Name: jsonMap["name"].(string),
|
||||
ManufacturerID: *mfUid,
|
||||
Meta: meta,
|
||||
Photo: buf,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, handlers.Response[struct {
|
||||
Id string `json:"id"`
|
||||
}]{
|
||||
Status: http.StatusText(http.StatusCreated),
|
||||
Payload: struct {
|
||||
Id string `json:"id"`
|
||||
}{Id: newUuid},
|
||||
})
|
||||
}
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "Invalid UUID")
|
||||
}
|
||||
|
||||
p, err := db.Powder.GetPowderById(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "Not found")
|
||||
}
|
||||
|
||||
response := handlers.Response[handlers.Powder]{
|
||||
Payload: handlers.Powder{
|
||||
Id: uid.String(),
|
||||
Name: p.Name,
|
||||
Manufacturer: handlers.Manufacturer{Id: p.ManufacturerID.String(), Name: p.ManufacturerName, Url: p.ManufacturerUrl.String},
|
||||
},
|
||||
}
|
||||
|
||||
response.Status = http.StatusText(http.StatusOK)
|
||||
|
||||
return c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
powders, err := db.Powder.GetPowders(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response := handlers.Response[[]handlers.Powder]{
|
||||
Payload: make([]handlers.Powder, 0),
|
||||
}
|
||||
|
||||
for _, p := range powders {
|
||||
response.Payload = append(response.Payload, handlers.Powder{
|
||||
Id: p.ID.String(),
|
||||
Name: p.Name,
|
||||
Manufacturer: handlers.Manufacturer{
|
||||
Id: p.ManufacturerID.String(),
|
||||
Name: p.ManufacturerName,
|
||||
Url: p.ManufacturerUrl.String,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
response.Status = http.StatusText(http.StatusOK)
|
||||
|
||||
return c.JSON(200, response)
|
||||
}
|
||||
Reference in New Issue
Block a user