You've already forked reloading-manager
A full commitment's what I'm thinking of
This commit is contained in:
@@ -2,7 +2,6 @@ package loads
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.siteworxpro.com/reloading-manager/backend/.gen/loading/public/table"
|
||||
"git.siteworxpro.com/reloading-manager/backend/database"
|
||||
"git.siteworxpro.com/reloading-manager/backend/handlers"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/labstack/echo/v4"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@@ -65,11 +65,29 @@ type ResultChan[T any] struct {
|
||||
}
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
var exists *loads.GetLoadByIdRow
|
||||
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
if id != "" {
|
||||
uuid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "id is not a valid UUID")
|
||||
}
|
||||
|
||||
gbi, err := db.Loads.GetLoadById(context.Background(), *uuid)
|
||||
if err != nil || gbi.ID.Valid == false {
|
||||
return handlers.NotFound(c, "load not found")
|
||||
}
|
||||
|
||||
exists = &gbi
|
||||
}
|
||||
|
||||
cartridgeID, err := handlers.ParseUuid(c.FormValue("cartridge_id"))
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "cartridge_id is not a valid UUID")
|
||||
@@ -91,8 +109,11 @@ func Post(c echo.Context) error {
|
||||
}
|
||||
|
||||
file, err := handlers.ReadFile(c, "photo")
|
||||
if err != nil {
|
||||
if err != nil && exists == nil {
|
||||
return handlers.BadRequest(c, "photo is not valid")
|
||||
} else if err != nil {
|
||||
// If we are updating an existing load, we can ignore the error
|
||||
file = exists.Photo
|
||||
}
|
||||
|
||||
meta := c.FormValue("meta")
|
||||
@@ -119,6 +140,26 @@ func Post(c echo.Context) error {
|
||||
return handlers.BadRequest(c, "col is not valid")
|
||||
}
|
||||
|
||||
if exists != nil {
|
||||
err = db.Loads.UpdateLoad(context.Background(), loads.UpdateLoadParams{
|
||||
ID: exists.ID,
|
||||
CartridgeID: *cartridgeID,
|
||||
Col: colFl,
|
||||
PowderID: *powderId,
|
||||
PowderGr: powderGrFl,
|
||||
PrimerID: *primerId,
|
||||
BulletID: *bulletId,
|
||||
Photo: file,
|
||||
Meta: []byte(meta),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, handlers.Response[string]{Payload: exists.ID.String()})
|
||||
}
|
||||
|
||||
uid, err := db.Loads.CreateLoad(context.Background(), loads.CreateLoadParams{
|
||||
CartridgeID: *cartridgeID,
|
||||
Col: colFl,
|
||||
@@ -137,6 +178,33 @@ func Post(c echo.Context) error {
|
||||
return c.JSON(http.StatusCreated, handlers.Response[string]{Payload: uid.String()})
|
||||
}
|
||||
|
||||
func Photo(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
return handlers.BadRequest(c, "id is required")
|
||||
}
|
||||
|
||||
uuid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "id is not a valid UUID")
|
||||
}
|
||||
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
file, err := db.Loads.GetLoadById(context.Background(), *uuid)
|
||||
if err != nil {
|
||||
return handlers.NotFound(c, "load photo not found")
|
||||
}
|
||||
|
||||
mt, _, _ := mime.ParseMediaType(string(file.Photo))
|
||||
|
||||
return c.Blob(http.StatusOK, mt, file.Photo)
|
||||
|
||||
}
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
cResults := make(chan ResultChan[[]loadResponseResults])
|
||||
@@ -213,8 +281,6 @@ func execResultsQuery(ch chan ResultChan[[]loadResponseResults], c echo.Context)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(q.DebugSql())
|
||||
|
||||
sql, params := q.Sql()
|
||||
rows, err := db.Db.Query(context.Background(), sql, params...)
|
||||
|
||||
@@ -496,6 +562,35 @@ func getQuery(c echo.Context, countOnly bool) postgres.SelectStatement {
|
||||
return q
|
||||
}
|
||||
|
||||
func Delete(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
return handlers.BadRequest(c, "id is required")
|
||||
}
|
||||
|
||||
uuid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
return handlers.BadRequest(c, "id is not a valid UUID")
|
||||
}
|
||||
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
exists, err := db.Loads.GetLoadById(context.Background(), *uuid)
|
||||
if err != nil || exists.ID.Valid == false {
|
||||
return handlers.NotFound(c, "load not found")
|
||||
}
|
||||
|
||||
err = db.Loads.DeleteLoad(context.Background(), *uuid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func getUuidExpr(ids []string) []postgres.Expression {
|
||||
expr := make([]postgres.Expression, 0)
|
||||
for _, id := range ids {
|
||||
|
||||
Reference in New Issue
Block a user