53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
|
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 {
|
||
|
ctx.JSON(http.StatusBadRequest, errorResponse(err))
|
||
|
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 {
|
||
|
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ctx.JSON(http.StatusOK, supplier)
|
||
|
}
|