naise_pos/api/product.go

191 lines
5.3 KiB
Go
Raw Normal View History

2023-03-07 18:52:37 +07:00
package api
import (
2023-03-12 11:01:43 +07:00
"database/sql"
2023-03-15 15:00:36 +07:00
"errors"
2023-04-05 14:04:49 +07:00
"fmt"
2023-03-07 18:52:37 +07:00
"net/http"
2023-04-05 14:04:49 +07:00
"os"
"path/filepath"
2023-03-12 11:01:43 +07:00
"time"
2023-03-07 18:52:37 +07:00
db "git.nochill.in/nochill/naice_pos/db/sqlc"
2023-03-15 15:00:36 +07:00
"git.nochill.in/nochill/naice_pos/token"
2023-04-05 14:04:49 +07:00
"git.nochill.in/nochill/naice_pos/util"
2023-03-07 18:52:37 +07:00
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type createProductRequest struct {
2023-04-05 14:04:49 +07:00
Name string `form:"name" binding:"required"`
SellingPrice float64 `form:"selling_price" binding:"required"`
ProductTypeID int16 `form:"product_type_id" binding:"required"`
PurchasePrice float64 `form:"purchase_price" binding:"required"`
ProductCategoryID string `form:"product_category_id" binding:"required"`
Stock float64 `form:"stock" binding:"number"`
2023-03-07 18:52:37 +07:00
}
func (server *Server) createProduct(ctx *gin.Context) {
var req createProductRequest
2023-04-05 14:04:49 +07:00
var imagePath string
2023-03-07 18:52:37 +07:00
2023-04-05 14:04:49 +07:00
file, _ := ctx.FormFile("image")
if err := ctx.Bind(&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
}
2023-03-15 15:00:36 +07:00
authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
2023-04-05 14:04:49 +07:00
if file != nil {
file := file
fileExt := filepath.Ext(file.Filename)
now := time.Now()
dir := fmt.Sprintf("public/upload/images/%s/products", authPayload.MerchantID)
filename := fmt.Sprintf("%s%s%s", util.RandomString(5), fmt.Sprintf("%v", now.Unix()), fileExt)
imagePath = fmt.Sprintf("%s/%s", dir, filename)
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.Mkdir(dir, 0775)
}
if err := ctx.SaveUploadedFile(file, fmt.Sprintf("%s", imagePath)); err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
return
}
}
2023-03-07 18:52:37 +07:00
arg := db.CreateProductParams{
2023-03-16 20:37:20 +07:00
MerchantID: authPayload.MerchantID,
Name: req.Name,
2023-03-22 13:40:18 +07:00
ProductTypeID: req.ProductTypeID,
2023-03-16 20:37:20 +07:00
SellingPrice: req.SellingPrice,
PurchasePrice: req.PurchasePrice,
ProductCategoryID: uuid.MustParse(req.ProductCategoryID),
Stock: req.Stock,
2023-04-05 14:04:49 +07:00
Image: sql.NullString{Valid: len(imagePath) > 0, String: imagePath},
2023-03-07 18:52:37 +07:00
}
product, err := server.store.CreateProduct(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, product)
}
2023-03-12 11:01:43 +07:00
type getProductRequest struct {
ID string `uri:"id" binding:"required,uuid"`
}
func (server *Server) getProduct(ctx *gin.Context) {
var req getProductRequest
if err := ctx.ShouldBindUri(&req); err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusBadRequest, errorResponse(err, ""))
2023-03-12 11:01:43 +07:00
return
}
product, err := server.store.GetProduct(ctx, uuid.MustParse(req.ID))
if err != nil {
if err == sql.ErrNoRows {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusNotFound, errorResponse(err, ""))
2023-03-12 11:01:43 +07:00
return
}
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
2023-03-12 11:01:43 +07:00
return
}
2023-03-15 15:00:36 +07:00
authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
if product.MerchantID != authPayload.MerchantID {
err := errors.New("Product doesn't belong to the user")
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusUnauthorized, errorResponse(err, ""))
2023-03-15 15:00:36 +07:00
return
}
2023-03-12 11:01:43 +07:00
ctx.JSON(http.StatusOK, product)
}
2023-03-15 15:00:36 +07:00
type listProductsRequest struct {
PageID int32 `form:"page_id" binding:"required,min=1"`
PageSize int32 `form:"page_size" binding:"required,min=5"`
}
func (server *Server) listProducts(ctx *gin.Context) {
var req listProductsRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusBadRequest, errorResponse(err, ""))
2023-03-15 15:00:36 +07:00
return
}
authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
arg := db.ListProductsParams{
MerchantID: authPayload.MerchantID,
Limit: req.PageSize,
Offset: (req.PageID - 1) * req.PageSize,
}
products, err := server.store.ListProducts(ctx, arg)
if err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
2023-03-15 15:00:36 +07:00
return
}
ctx.JSON(http.StatusOK, products)
}
2023-03-12 11:01:43 +07:00
type updateProductRequest struct {
2023-04-05 14:04:49 +07:00
ProductID uuid.UUID `form:"product_id" binding:"required"`
Name string `form:"name" binding:"required"`
SellingPrice float64 `form:"selling_price" binding:"required"`
PurchasePrice float64 `form:"purchase_price" binding:"required"`
2023-03-12 11:01:43 +07:00
}
func (server *Server) updateProduct(ctx *gin.Context) {
var req updateProductRequest
2023-04-05 14:04:49 +07:00
var imagePath string
file, _ := ctx.FormFile("image")
if err := ctx.Bind(&req); err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusBadRequest, errorResponse(err, ""))
2023-03-12 11:01:43 +07:00
return
}
2023-04-05 14:04:49 +07:00
authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
if file != nil {
file := file
fileExt := filepath.Ext(file.Filename)
now := time.Now()
dir := fmt.Sprintf("public/upload/images/%s/products", authPayload.MerchantID)
filename := fmt.Sprintf("%s%s%s", util.RandomString(5), fmt.Sprintf("%v", now.Unix()), fileExt)
imagePath = fmt.Sprintf("%s/%s", dir, filename)
if err := ctx.SaveUploadedFile(file, fmt.Sprintf("%s", imagePath)); err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
return
}
}
2023-03-12 11:01:43 +07:00
arg := db.UpdateProductParams{
ID: req.ProductID,
Name: req.Name,
SellingPrice: req.SellingPrice,
PurchasePrice: req.PurchasePrice,
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
}
product, err := server.store.UpdateProduct(ctx, arg)
if err != nil {
if err == sql.ErrNoRows {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusNotFound, errorResponse(err, ""))
2023-03-12 11:01:43 +07:00
return
}
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
2023-03-12 11:01:43 +07:00
return
}
ctx.JSON(http.StatusOK, product)
}