You've already forked reloading-manager
No changes made
This commit is contained in:
167
backend/handlers/manufacturer/handler.go
Normal file
167
backend/handlers/manufacturer/handler.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package manufacturer
|
||||
|
||||
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/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 db.Db.Close(context.Background())
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
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 db.Db.Close(context.Background())
|
||||
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, 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 db.Db.Close(context.Background())
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, 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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user