refactor: reorganize packages into a domain-based layered structure

This commit is contained in:
Leon
2025-12-05 09:59:57 +08:00
parent 34b05ad83f
commit c1862b5475
8 changed files with 83 additions and 94 deletions

View File

@@ -0,0 +1,126 @@
package account
import (
"feedsystem_video_go/internal/account"
"github.com/gin-gonic/gin"
)
type UserHandler struct {
userService *account.UserService
}
type CreateUserRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type CreateUserResponse struct {
}
type RenameByIDRequest struct {
ID uint `json:"id"`
NewUsername string `json:"new_username"`
}
type RenameByIDResponse struct {
}
type FindByIDRequest struct {
ID uint `json:"id"`
}
type FindByIDResponse struct {
ID uint `json:"id"`
Username string `json:"username"`
}
type FindByUsernameRequest struct {
Username string `json:"username"`
}
type FindByUsernameResponse struct {
ID uint `json:"id"`
Username string `json:"username"`
}
type ChangePasswordRequest struct {
ID uint `json:"id"`
NewPassword string `json:"new_password"`
}
type ChangePasswordResponse struct {
}
func NewUserHandler(userService *account.UserService) *UserHandler {
return &UserHandler{userService: userService}
}
func (h *UserHandler) CreateUser(c *gin.Context) {
var req CreateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.userService.CreateUser(&account.User{
Username: req.Username,
Password: req.Password,
}); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "user created"})
}
func (h *UserHandler) RenameByID(c *gin.Context) {
var req RenameByIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.userService.RenameByID(req.ID, req.NewUsername); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "user renamed"})
}
func (h *UserHandler) ChangePassword(c *gin.Context) {
var req ChangePasswordRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.userService.ChangePassword(req.ID, req.NewPassword); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "password changed"})
}
func (h *UserHandler) FindByID(c *gin.Context) {
var req FindByIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if user, err := h.userService.FindByID(req.ID); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, user)
}
}
func (h *UserHandler) FindByUsername(c *gin.Context) {
var req FindByUsernameRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if user, err := h.userService.FindByUsername(req.Username); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, user)
}
}

26
internal/http/router.go Normal file
View File

@@ -0,0 +1,26 @@
package http
import (
"feedsystem_video_go/internal/account"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func SetRouter(db *gorm.DB) *gin.Engine {
r := gin.Default()
userRepository := account.NewUserRepository(db)
userService := account.NewUserService(userRepository)
userHandler := NewUserHandler(userService)
userGroup := r.Group("/user")
{
userGroup.POST("/register", userHandler.CreateUser)
userGroup.POST("/rename", userHandler.RenameByID)
userGroup.POST("/changePassword", userHandler.ChangePassword)
userGroup.POST("/findByID", userHandler.FindByID)
userGroup.POST("/findByUsername", userHandler.FindByUsername)
}
return r
}