pejhancctv/handlers/product.go

260 lines
4.7 KiB
Go

package handlers
import (
database "app/database"
"app/models"
"app/repositories"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
)
// Create(p *models.Product) error
// GetByName(name string) ([]models.Product, error)
// GetByID(id uint) (*models.Product, error)
// GetByCategory(category string) ([]models.Product, error)
// UpdateName(name string, id uint) error
// UpdateDescription(content string, id uint) error
// UpdateWarranty(name string, id uint) error
// UpdateQuantity(quantity uint, id uint) error
// UpdateCategory(category string, id uint) error
// UpdatePrice(price uint64, id uint) error
// Delete(name string, id uint) error
func initProduct() *repositories.Product_repository {
db := database.Db()
productRepository := new(repositories.Product_repository)
productRepository.DB = &db
return productRepository
}
func CreateProduct(c echo.Context) error {
product := new(models.Product)
if err := c.Bind(product); err != nil {
panic(err)
}
productRepository := initProduct()
err := productRepository.Create(product)
if err != nil {
panic(err)
}
return c.JSON(http.StatusCreated, product)
}
func GetProductByName(c echo.Context) error {
name := c.Param("name")
productRepository := initProduct()
product, err := productRepository.GetByName(name)
if err != nil {
return err
}
return c.JSON(http.StatusAccepted, product)
}
func GetProductByID(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
productRepository := initProduct()
product, err := productRepository.GetByID(uint(id))
if err != nil {
panic(err)
}
return c.JSON(http.StatusAccepted, product)
}
func GetProductByCategory(c echo.Context) error {
name := c.Param("name")
productRepository := initProduct()
product, err := productRepository.GetByCategory(name)
if err != nil {
return err
}
return c.JSON(http.StatusAccepted, product)
}
func UpdateProductName(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
var update struct {
NewName string `json:"name"`
}
if err := c.Bind(update); err != nil {
panic(err)
}
product := initProduct()
if update.NewName != "" {
err = product.UpdateName(update.NewName, uint(id))
if err != nil {
panic(err)
}
}
return c.String(http.StatusAccepted, "updated")
}
func UpdateDescription(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
var update struct {
NewDescription string `json:"description"`
}
if err := c.Bind(update); err != nil {
panic(err)
}
product := initProduct()
if update.NewDescription != "" {
err = product.UpdateDescription(update.NewDescription, uint(id))
if err != nil {
panic(err)
}
}
return c.String(http.StatusAccepted, "updated")
}
func UpdateWarranty(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
var update struct {
NewWarranty string `json:"warranty"`
}
if err := c.Bind(update); err != nil {
panic(err)
}
product := initProduct()
if update.NewWarranty != "" {
err = product.UpdateWarranty(update.NewWarranty, uint(id))
if err != nil {
panic(err)
}
}
return c.String(http.StatusAccepted, "updated")
}
func UpdateQuantity(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
var update struct {
NewQuantity uint `json:"description"`
}
if err := c.Bind(update); err != nil {
panic(err)
}
product := initProduct()
err = product.UpdateQuantity(update.NewQuantity, uint(id))
if err != nil {
panic(err)
}
return c.String(http.StatusAccepted, "updated")
}
func UpdateCategory(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
var update struct {
NewCategory string `json:"category"`
}
if err := c.Bind(update); err != nil {
panic(err)
}
product := initProduct()
if update.NewCategory != "" {
err = product.UpdateCategory(update.NewCategory, uint(id))
if err != nil {
panic(err)
}
}
return c.String(http.StatusAccepted, "updated")
}
func UpdatePrice(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
var update struct {
NewPrie uint64 `json:"price"`
}
if err := c.Bind(update); err != nil {
panic(err)
}
product := initProduct()
err = product.UpdatePrice(update.NewPrie, uint(id))
if err != nil {
panic(err)
}
return c.String(http.StatusAccepted, "updated")
}
func DeleteProduct(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
product := initProduct()
err = product.Delete(uint(id))
if err != nil {
panic(err)
}
return c.String(http.StatusAccepted, "removed")
}