124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
package services
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/dungnt11/todoms_golang/internal/models"
|
|
"github.com/dungnt11/todoms_golang/internal/repositories"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ProductService triển khai IProductService
|
|
type ProductService struct {
|
|
productRepo repositories.IProductRepository
|
|
}
|
|
|
|
// NewProductService tạo một instance mới của ProductService
|
|
func NewProductService(productRepo repositories.IProductRepository) *ProductService {
|
|
return &ProductService{
|
|
productRepo: productRepo,
|
|
}
|
|
}
|
|
|
|
// CreateProduct tạo một sản phẩm mới
|
|
func (s *ProductService) CreateProduct(c *gin.Context) (map[string]interface{}, error) {
|
|
var product models.Product
|
|
if err := c.ShouldBindJSON(&product); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if product.Name == "" {
|
|
return nil, errors.New("tên sản phẩm không được để trống")
|
|
}
|
|
|
|
if product.Price <= 0 {
|
|
return nil, errors.New("giá sản phẩm phải lớn hơn 0")
|
|
}
|
|
|
|
if err := s.productRepo.Create(&product); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"message": "tạo sản phẩm thành công",
|
|
"product": product,
|
|
}, nil
|
|
}
|
|
|
|
// GetProduct lấy thông tin sản phẩm theo ID
|
|
func (s *ProductService) GetProduct(id uint) (map[string]interface{}, error) {
|
|
product, err := s.productRepo.FindByID(id)
|
|
if err != nil {
|
|
return nil, errors.New("không tìm thấy sản phẩm")
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"product": product,
|
|
}, nil
|
|
}
|
|
|
|
// GetAllProducts lấy tất cả sản phẩm
|
|
func (s *ProductService) GetAllProducts() (map[string]interface{}, error) {
|
|
products, err := s.productRepo.FindAll()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"products": products,
|
|
}, nil
|
|
}
|
|
|
|
// UpdateProduct cập nhật thông tin sản phẩm
|
|
func (s *ProductService) UpdateProduct(id uint, c *gin.Context) (map[string]interface{}, error) {
|
|
product, err := s.productRepo.FindByID(id)
|
|
if err != nil {
|
|
return nil, errors.New("không tìm thấy sản phẩm")
|
|
}
|
|
|
|
var updatedProduct models.Product
|
|
if err := c.ShouldBindJSON(&updatedProduct); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Cập nhật thông tin
|
|
if updatedProduct.Name != "" {
|
|
product.Name = updatedProduct.Name
|
|
}
|
|
if updatedProduct.Description != "" {
|
|
product.Description = updatedProduct.Description
|
|
}
|
|
if updatedProduct.Price > 0 {
|
|
product.Price = updatedProduct.Price
|
|
}
|
|
if updatedProduct.Quantity >= 0 {
|
|
product.Quantity = updatedProduct.Quantity
|
|
}
|
|
|
|
if err := s.productRepo.Update(product); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"message": "cập nhật sản phẩm thành công",
|
|
"product": product,
|
|
}, nil
|
|
}
|
|
|
|
// DeleteProduct xóa sản phẩm theo ID
|
|
func (s *ProductService) DeleteProduct(id uint) (map[string]interface{}, error) {
|
|
// Kiểm tra sản phẩm tồn tại
|
|
_, err := s.productRepo.FindByID(id)
|
|
if err != nil {
|
|
return nil, errors.New("không tìm thấy sản phẩm")
|
|
}
|
|
|
|
if err := s.productRepo.Delete(id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"message": "xóa sản phẩm thành công",
|
|
}, nil
|
|
}
|