init project

This commit is contained in:
koh
2025-03-02 14:59:34 +07:00
commit e2a219cacd
35 changed files with 2269 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package controllers
// Controllers chứa tất cả các controller của ứng dụng
type Controllers struct {
UserController IUserController
ProductController IProductController
}
// NewControllers tạo một instance mới của Controllers
func NewControllers(userController IUserController, productController IProductController) *Controllers {
return &Controllers{
UserController: userController,
ProductController: productController,
}
}

View File

@@ -0,0 +1,22 @@
package controllers
import "github.com/gin-gonic/gin"
// IUserController định nghĩa interface cho UserController
type IUserController interface {
Register(c *gin.Context)
Login(c *gin.Context)
GetProfile(c *gin.Context)
UpdateProfile(c *gin.Context)
}
// IProductController định nghĩa interface cho ProductController
type IProductController interface {
CreateProduct(c *gin.Context)
GetProduct(c *gin.Context)
GetAllProducts(c *gin.Context)
UpdateProduct(c *gin.Context)
DeleteProduct(c *gin.Context)
}
// Các interface khác có thể được thêm vào đây khi cần thiết

View File

@@ -0,0 +1,125 @@
package controllers
import (
"strconv"
"github.com/dungnt11/senflow_app/internal/services"
"github.com/gin-gonic/gin"
)
// ProductController triển khai IProductController
type ProductController struct {
productService services.IProductService
}
// NewProductController tạo một instance mới của ProductController
func NewProductController(productService services.IProductService) *ProductController {
return &ProductController{
productService: productService,
}
}
// CreateProduct xử lý tạo sản phẩm mới
func (pc *ProductController) CreateProduct(c *gin.Context) {
// Gọi service để thực hiện logic tạo sản phẩm
result, err := pc.productService.CreateProduct(c)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(201, result)
}
// GetProduct xử lý lấy thông tin sản phẩm theo ID
func (pc *ProductController) GetProduct(c *gin.Context) {
// Lấy ID từ param
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(400, gin.H{
"error": "ID không hợp lệ",
})
return
}
// Gọi service để lấy thông tin sản phẩm
result, err := pc.productService.GetProduct(uint(id))
if err != nil {
c.JSON(404, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}
// GetAllProducts xử lý lấy tất cả sản phẩm
func (pc *ProductController) GetAllProducts(c *gin.Context) {
// Gọi service để lấy tất cả sản phẩm
result, err := pc.productService.GetAllProducts()
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}
// UpdateProduct xử lý cập nhật thông tin sản phẩm
func (pc *ProductController) UpdateProduct(c *gin.Context) {
// Lấy ID từ param
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(400, gin.H{
"error": "ID không hợp lệ",
})
return
}
// Gọi service để cập nhật thông tin sản phẩm
result, err := pc.productService.UpdateProduct(uint(id), c)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}
// DeleteProduct xử lý xóa sản phẩm
func (pc *ProductController) DeleteProduct(c *gin.Context) {
// Lấy ID từ param
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(400, gin.H{
"error": "ID không hợp lệ",
})
return
}
// Gọi service để xóa sản phẩm
result, err := pc.productService.DeleteProduct(uint(id))
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}

View File

@@ -0,0 +1,93 @@
package controllers
import (
"github.com/dungnt11/senflow_app/internal/services"
"github.com/gin-gonic/gin"
)
// UserController triển khai IUserController
type UserController struct {
userService services.IUserService
}
// NewUserController tạo một instance mới của UserController
func NewUserController(userService services.IUserService) *UserController {
return &UserController{
userService: userService,
}
}
// Register xử lý đăng ký người dùng
func (uc *UserController) Register(c *gin.Context) {
// Gọi service để thực hiện logic đăng ký
result, err := uc.userService.Register(c)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}
// Login xử lý đăng nhập
func (uc *UserController) Login(c *gin.Context) {
// Gọi service để thực hiện logic đăng nhập
result, err := uc.userService.Login(c)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}
// GetProfile xử lý lấy thông tin profile
func (uc *UserController) GetProfile(c *gin.Context) {
// Lấy user ID từ token hoặc param (giả định)
userID := uint(1) // Trong thực tế, bạn sẽ lấy từ JWT token hoặc param
// Gọi service để lấy thông tin profile
result, err := uc.userService.GetProfile(userID)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}
// UpdateProfile xử lý cập nhật profile
func (uc *UserController) UpdateProfile(c *gin.Context) {
// Lấy user ID từ token hoặc param (giả định)
userID := uint(1) // Trong thực tế, bạn sẽ lấy từ JWT token hoặc param
// Đọc dữ liệu từ request
var data map[string]interface{}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
// Gọi service để cập nhật profile
result, err := uc.userService.UpdateProfile(userID, data)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, result)
}