init project
This commit is contained in:
22
internal/services/interfaces.go
Normal file
22
internal/services/interfaces.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package services
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// IUserService định nghĩa interface cho UserService
|
||||
type IUserService interface {
|
||||
Register(c *gin.Context) (map[string]interface{}, error)
|
||||
Login(c *gin.Context) (map[string]interface{}, error)
|
||||
GetProfile(userID uint) (map[string]interface{}, error)
|
||||
UpdateProfile(userID uint, data map[string]interface{}) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
// IProductService định nghĩa interface cho ProductService
|
||||
type IProductService interface {
|
||||
CreateProduct(c *gin.Context) (map[string]interface{}, error)
|
||||
GetProduct(id uint) (map[string]interface{}, error)
|
||||
GetAllProducts() (map[string]interface{}, error)
|
||||
UpdateProduct(id uint, c *gin.Context) (map[string]interface{}, error)
|
||||
DeleteProduct(id uint) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
// Các interface khác có thể được thêm vào đây khi cần thiết
|
123
internal/services/product_service.go
Normal file
123
internal/services/product_service.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/dungnt11/senflow_app/internal/models"
|
||||
"github.com/dungnt11/senflow_app/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
|
||||
}
|
118
internal/services/user_service.go
Normal file
118
internal/services/user_service.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/dungnt11/senflow_app/internal/models"
|
||||
"github.com/dungnt11/senflow_app/internal/repositories"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UserService triển khai IUserService
|
||||
type UserService struct {
|
||||
userRepo repositories.IUserRepository
|
||||
}
|
||||
|
||||
// NewUserService tạo một instance mới của UserService
|
||||
func NewUserService(userRepo repositories.IUserRepository) *UserService {
|
||||
return &UserService{
|
||||
userRepo: userRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// Register xử lý logic đăng ký người dùng
|
||||
func (us *UserService) Register(c *gin.Context) (map[string]interface{}, error) {
|
||||
// Đọc dữ liệu từ request
|
||||
var user models.User
|
||||
if err := c.ShouldBindJSON(&user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Kiểm tra email đã tồn tại chưa
|
||||
existingUser, _ := us.userRepo.FindByEmail(user.Email)
|
||||
if existingUser != nil {
|
||||
return nil, errors.New("email đã được sử dụng")
|
||||
}
|
||||
|
||||
// Lưu vào database thông qua repository
|
||||
err := us.userRepo.Create(&user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Trả về kết quả
|
||||
return map[string]interface{}{
|
||||
"message": "Đăng ký thành công",
|
||||
"user_id": user.ID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Login xử lý logic đăng nhập
|
||||
func (us *UserService) Login(c *gin.Context) (map[string]interface{}, error) {
|
||||
// Đọc dữ liệu từ request
|
||||
var loginData struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&loginData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Tìm user theo email
|
||||
user, err := us.userRepo.FindByEmail(loginData.Email)
|
||||
if err != nil {
|
||||
return nil, errors.New("email hoặc mật khẩu không đúng")
|
||||
}
|
||||
|
||||
// Kiểm tra mật khẩu (giả định)
|
||||
if user.Password != loginData.Password {
|
||||
return nil, errors.New("email hoặc mật khẩu không đúng")
|
||||
}
|
||||
|
||||
// Trả về kết quả
|
||||
return map[string]interface{}{
|
||||
"message": "Đăng nhập thành công",
|
||||
"user_id": user.ID,
|
||||
"token": "jwt_token_here", // Trong thực tế, bạn sẽ tạo JWT token
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetProfile lấy thông tin profile của người dùng
|
||||
func (us *UserService) GetProfile(userID uint) (map[string]interface{}, error) {
|
||||
// Tìm user theo ID
|
||||
user, err := us.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return nil, errors.New("không tìm thấy người dùng")
|
||||
}
|
||||
|
||||
// Trả về kết quả
|
||||
return map[string]interface{}{
|
||||
"user": user,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateProfile cập nhật thông tin profile của người dùng
|
||||
func (us *UserService) UpdateProfile(userID uint, data map[string]interface{}) (map[string]interface{}, error) {
|
||||
// Tìm user theo ID
|
||||
user, err := us.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return nil, errors.New("không tìm thấy người dùng")
|
||||
}
|
||||
|
||||
// Cập nhật thông tin (giả định)
|
||||
if name, ok := data["name"].(string); ok {
|
||||
user.Name = name
|
||||
}
|
||||
|
||||
// Lưu vào database
|
||||
err = us.userRepo.Update(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Trả về kết quả
|
||||
return map[string]interface{}{
|
||||
"message": "Cập nhật thành công",
|
||||
"user": user,
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user