You've already forked reloading-manager
Reviewed-on: Siteworxpro/reloading-manager#8 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
174 lines
4.2 KiB
Go
174 lines
4.2 KiB
Go
package manufacturer
|
|
|
|
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/manufacturer"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
)
|
|
|
|
type manufacturerResponse struct {
|
|
Id string `json:"id"`
|
|
Name string `json:"name"`
|
|
Url string `json:"url"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
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.Manufacturer.GetById(context.Background(), *uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
response := handlers.Response[manufacturerResponse]{
|
|
Status: http.StatusText(http.StatusOK),
|
|
Payload: manufacturerResponse{
|
|
Id: byId.ID.String(),
|
|
Name: byId.Name,
|
|
Url: byId.Url.String,
|
|
CreatedAt: byId.CreatedAt.Time.Format(handlers.DateFormat),
|
|
},
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
all, err := db.Manufacturer.GetAll(context.Background())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var payload []manufacturerResponse
|
|
for _, m := range all {
|
|
payload = append(payload, manufacturerResponse{
|
|
Id: m.ID.String(),
|
|
Name: m.Name,
|
|
Url: m.Url.String,
|
|
CreatedAt: m.CreatedAt.Time.Format(handlers.DateFormat),
|
|
})
|
|
}
|
|
|
|
response := handlers.Response[[]manufacturerResponse]{
|
|
Status: http.StatusText(http.StatusOK),
|
|
Payload: payload,
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
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.Manufacturer.GetById(context.Background(), *uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
err = db.Manufacturer.DeleteById(context.Background(), *uid)
|
|
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Cannot delete manufacturer")
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
func Post(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")
|
|
}
|
|
|
|
_, err = db.Manufacturer.GetById(context.Background(), *uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
jsonMap := make(map[string]interface{})
|
|
_ = json.NewDecoder(c.Request().Body).Decode(&jsonMap)
|
|
|
|
if jsonMap["name"] == "" {
|
|
return handlers.BadRequest(c, "Name is required.")
|
|
}
|
|
|
|
err = db.Manufacturer.UpdateManufacturer(context.Background(), manufacturer.UpdateManufacturerParams{
|
|
Name: jsonMap["name"].(string),
|
|
Url: pgtype.Text{
|
|
String: jsonMap["url"].(string),
|
|
Valid: true,
|
|
},
|
|
ID: *uid,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
newUuid := uuid.New().String()
|
|
uid, _ := handlers.ParseUuid(newUuid)
|
|
|
|
jsonMap := make(map[string]interface{})
|
|
_ = json.NewDecoder(c.Request().Body).Decode(&jsonMap)
|
|
|
|
if jsonMap["name"] == nil || jsonMap["name"] == "" {
|
|
return handlers.BadRequest(c, "Name is required.")
|
|
}
|
|
|
|
var url string
|
|
if jsonMap["url"] != nil {
|
|
url = jsonMap["url"].(string)
|
|
}
|
|
|
|
err := db.Manufacturer.CreateManufacturer(context.Background(), manufacturer.CreateManufacturerParams{
|
|
ID: *uid,
|
|
Name: jsonMap["name"].(string),
|
|
Url: pgtype.Text{
|
|
String: url,
|
|
Valid: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusAccepted, handlers.Response[string]{
|
|
Status: http.StatusText(http.StatusAccepted),
|
|
Payload: newUuid,
|
|
})
|
|
}
|