43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
db "git.nochill.in/nochill/naice_pos/db/sqlc"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/google/uuid"
|
||
|
)
|
||
|
|
||
|
type createProductRequest struct {
|
||
|
MerchantID uuid.UUID `json:"merchant_id" binding:"required"`
|
||
|
Name string `json:"name" binding:"required"`
|
||
|
SellingPrice float64 `json:"selling_price" binding:"required"`
|
||
|
PurchasePrice float64 `json:"purchase_price" binding:"required"`
|
||
|
Stock float64 `json:"stock" binding:"required"`
|
||
|
}
|
||
|
|
||
|
func (server *Server) createProduct(ctx *gin.Context) {
|
||
|
var req createProductRequest
|
||
|
|
||
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||
|
ctx.JSON(http.StatusBadRequest, errorResponse(err))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
arg := db.CreateProductParams{
|
||
|
MerchantID: req.MerchantID,
|
||
|
Name: req.Name,
|
||
|
SellingPrice: req.SellingPrice,
|
||
|
PurchasePrice: req.PurchasePrice,
|
||
|
Stock: req.Stock,
|
||
|
}
|
||
|
|
||
|
product, err := server.store.CreateProduct(ctx, arg)
|
||
|
if err != nil {
|
||
|
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ctx.JSON(http.StatusOK, product)
|
||
|
}
|