first
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package units
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
type unitResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Symbol string `json:"symbol"`
|
||||
}
|
||||
|
||||
func toResponse(u *Unit) unitResponse {
|
||||
return unitResponse{ID: u.ID, Name: u.Name, Symbol: u.Symbol}
|
||||
}
|
||||
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
list, err := h.service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list units"})
|
||||
return
|
||||
}
|
||||
resp := make([]unitResponse, 0, len(list))
|
||||
for _, u := range list {
|
||||
resp = append(resp, toResponse(u))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"units": resp})
|
||||
}
|
||||
|
||||
type upsertRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Symbol string `json:"symbol" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
var req upsertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
u, err := h.service.Create(c.Request.Context(), req.Name, req.Symbol)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrSymbolTaken) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "symbol already in use"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create unit"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, toResponse(u))
|
||||
}
|
||||
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
var req upsertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
u, err := h.service.Update(c.Request.Context(), id, req.Name, req.Symbol)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, ErrNotFound):
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "unit not found"})
|
||||
case errors.Is(err, ErrSymbolTaken):
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "symbol already in use"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update unit"})
|
||||
}
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toResponse(u))
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, ErrNotFound):
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "unit not found"})
|
||||
case errors.Is(err, ErrInUse):
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "unit is used by existing price tiers"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete unit"})
|
||||
}
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
Reference in New Issue
Block a user