naise_pos/api/supplier.go

53 lines
1.1 KiB
Go
Raw Normal View History

2023-03-07 18:52:37 +07:00
package api
import (
"encoding/json"
"net/http"
db "git.nochill.in/nochill/naice_pos/db/sqlc"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/tabbed/pqtype"
)
type CreateSuppliersRequest struct {
MerchantID uuid.UUID `json:"merchant_id" binding:"required"`
Name string `json:"name" binding:"required"`
Detail []map[string]any `json:"detail"`
}
type SupplierDetail struct {
name string
value string
}
func (server *Server) createSupplier(ctx *gin.Context) {
var req CreateSuppliersRequest
var supDetail pqtype.NullRawMessage
if err := ctx.ShouldBindJSON(&req); err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusBadRequest, errorResponse(err, ""))
2023-03-07 18:52:37 +07:00
return
}
b, _ := json.Marshal(req.Detail)
supDetail = pqtype.NullRawMessage{
RawMessage: b,
Valid: len(req.Detail) > 0,
}
arg := db.CreateSuppliersParams{
MerchantID: req.MerchantID,
Name: req.Name,
Detail: supDetail,
}
supplier, err := server.store.CreateSuppliers(ctx, arg)
if err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
2023-03-07 18:52:37 +07:00
return
}
ctx.JSON(http.StatusOK, supplier)
}