Go-TestAPI/controllers/productController.go

170 lines
4.1 KiB
Go

package controllers
import (
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"golang-test/database"
"golang-test/libs"
"golang-test/types"
"golang-test/validators"
"log"
"net/http"
"strconv"
//"golang-test/migrations"
)
func CreateProduct(c *gin.Context, product database.Product) {
db := database.Connector()
validate := validators.Validate
//response := types.MessageResponse{Message: "Product Created"}
var manufacturer *database.Manufacturer
if err := validate.Struct(&product); err != nil {
c.JSON(http.StatusBadRequest, libs.GetValidationErrors(err.(validator.ValidationErrors)))
return
}
if err := db.First(&manufacturer, product.ManufacturerID).Error; err != nil {
c.JSON(http.StatusBadRequest, types.NotFoundError("Product", product.ManufacturerID))
return
}
product.Manufacturer = manufacturer
log.Println(*product.Manufacturer)
if err := db.Create(&product).Error; err != nil {
c.JSON(http.StatusBadRequest, types.ErrorResponse{Message: err.Error()})
return
}
c.JSON(http.StatusCreated, types.MessageResponse{Message: "Product created"})
}
func DeleteProduct(c *gin.Context) {
db := database.Connector()
id := c.Param("id")
var product database.Product
response := types.MessageResponse{
Message: "Product deleted",
}
if db.First(&product, id).Error != nil {
c.JSON(http.StatusNotFound, types.NotFoundError("Product", id))
return
}
db.Delete(&product, id)
c.JSON(http.StatusOK, response)
}
func GetProducts(c *gin.Context) {
db := database.Connector()
var product []database.Product
var productResp []types.ProductResponse
db.Find(&product).Scan(&productResp)
for i, element := range productResp {
var manufacturer types.ManufacturerResponse
db.First(&database.Manufacturer{}, element.ManufacturerID).Scan(&manufacturer)
productResp[i].Manufacturer = manufacturer
}
c.JSON(http.StatusOK, productResp)
}
func GetProductInfo(c *gin.Context) {
db := database.Connector()
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, types.ErrorResponse{Message: err.Error()})
}
//var productResponse types.ProductResponse
var product database.Product
var productResp types.ProductResponse
var manufacturer types.ManufacturerResponse
if err := db.First(&product, id).Select("ID", "Name", "Price", "ManufacturerID").Scan(&productResp).Error; err != nil {
c.JSON(http.StatusBadRequest, types.NotFoundError("Product", id))
return
}
if err := db.First(&product.Manufacturer, product.ManufacturerID).Scan(&manufacturer).Error; err != nil {
c.JSON(http.StatusBadRequest, types.NotFoundError("Product", product.ManufacturerID))
return
}
productResp.Manufacturer = manufacturer
log.Println(product.Manufacturer)
c.JSON(http.StatusOK, productResp)
}
func BuyProduct(c *gin.Context) {
var product database.Product
db := database.Connector()
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, types.ErrorResponse{Message: err.Error()})
return
}
if err := db.First(&product, id).Error; err != nil {
c.JSON(http.StatusBadRequest, types.NotFoundError("Product", id))
return
}
token, err := libs.GetTokenFromHeaders(c)
if err != nil {
c.JSON(http.StatusUnauthorized, types.ErrorResponse{Message: "Invalid token. Please login"})
return
}
u, err := GetUserByToken(token)
if err != nil {
c.JSON(http.StatusUnauthorized, types.ErrorResponse{Message: "Invalid token. Please login"})
return
}
if u.Money-product.Price < 0 {
c.JSON(http.StatusOK, types.MessageResponse{Message: "Not enough money"})
return
}
u.Money -= product.Price
u.Products = append(u.Products, product)
db.Save(&u)
c.JSON(http.StatusOK, types.MessageResponse{Message: "Product successful buyed"})
}
func GetBuyedProducts(c *gin.Context) {
db := database.Connector()
token, err := libs.GetTokenFromHeaders(c)
if err != nil {
c.JSON(http.StatusUnauthorized, types.ErrorResponse{Message: "Invalid token. Please login"})
return
}
u, err := GetUserByToken(token)
if err != nil {
c.JSON(http.StatusUnauthorized, types.ErrorResponse{Message: "Invalid token. Please login"})
return
}
db.Joins("Products").First(&u)
}