2024-06-05 08:43:05 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-06-06 08:11:07 +00:00
|
|
|
"github.com/go-playground/validator/v10"
|
2024-06-05 08:43:05 +00:00
|
|
|
"golang-test/database"
|
2024-06-06 08:11:07 +00:00
|
|
|
"golang-test/libs"
|
2024-06-07 12:52:09 +00:00
|
|
|
"golang-test/types"
|
2024-06-06 08:11:07 +00:00
|
|
|
"golang-test/validators"
|
|
|
|
"log"
|
2024-06-07 12:52:09 +00:00
|
|
|
"net/http"
|
2024-06-06 09:37:44 +00:00
|
|
|
"strconv"
|
2024-06-05 08:43:05 +00:00
|
|
|
//"golang-test/migrations"
|
|
|
|
)
|
|
|
|
|
2024-06-06 08:11:07 +00:00
|
|
|
func CreateProduct(c *gin.Context, product database.Product) {
|
|
|
|
db := database.Connector()
|
|
|
|
validate := validators.Validate
|
2024-06-07 12:52:09 +00:00
|
|
|
//response := types.MessageResponse{Message: "Product Created"}
|
|
|
|
var manufacturer *database.Manufacturer
|
2024-06-05 08:43:05 +00:00
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
if err := validate.Struct(&product); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, libs.GetValidationErrors(err.(validator.ValidationErrors)))
|
2024-06-06 08:11:07 +00:00
|
|
|
return
|
2024-06-05 08:43:05 +00:00
|
|
|
}
|
2024-06-06 08:11:07 +00:00
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
if err := db.First(&manufacturer, product.ManufacturerID).Error; err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, types.NotFoundError("Product", product.ManufacturerID))
|
|
|
|
return
|
2024-06-06 09:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
product.Manufacturer = manufacturer
|
2024-06-06 08:11:07 +00:00
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
log.Println(*product.Manufacturer)
|
2024-06-06 08:11:07 +00:00
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
if err := db.Create(&product).Error; err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, types.ErrorResponse{Message: err.Error()})
|
2024-06-06 08:11:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
c.JSON(http.StatusCreated, types.MessageResponse{Message: "Product created"})
|
2024-06-05 08:43:05 +00:00
|
|
|
}
|
2024-06-06 09:37:44 +00:00
|
|
|
|
|
|
|
func DeleteProduct(c *gin.Context) {
|
|
|
|
db := database.Connector()
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
var product database.Product
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
response := types.MessageResponse{
|
|
|
|
Message: "Product deleted",
|
|
|
|
}
|
|
|
|
|
2024-06-06 09:37:44 +00:00
|
|
|
if db.First(&product, id).Error != nil {
|
2024-06-07 12:52:09 +00:00
|
|
|
c.JSON(http.StatusNotFound, types.NotFoundError("Product", id))
|
2024-06-06 09:37:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Delete(&product, id)
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
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
|
2024-06-06 09:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
c.JSON(http.StatusOK, productResp)
|
2024-06-06 09:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetProductInfo(c *gin.Context) {
|
|
|
|
db := database.Connector()
|
2024-06-07 12:52:09 +00:00
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
2024-06-06 09:37:44 +00:00
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, types.ErrorResponse{Message: err.Error()})
|
|
|
|
}
|
|
|
|
|
|
|
|
//var productResponse types.ProductResponse
|
2024-06-06 09:37:44 +00:00
|
|
|
var product database.Product
|
2024-06-07 12:52:09 +00:00
|
|
|
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
|
|
|
|
}
|
2024-06-06 09:37:44 +00:00
|
|
|
|
|
|
|
if err := db.First(&product, id).Error; err != nil {
|
2024-06-07 12:52:09 +00:00
|
|
|
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"})
|
2024-06-06 09:37:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
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
|
2024-06-06 09:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 12:52:09 +00:00
|
|
|
db.Joins("Products").First(&u)
|
2024-06-06 09:37:44 +00:00
|
|
|
}
|