2025-12-05 12:43:44 +08:00
|
|
|
package http
|
2025-12-04 17:09:04 +08:00
|
|
|
|
|
|
|
|
import (
|
2025-12-05 09:59:57 +08:00
|
|
|
"feedsystem_video_go/internal/account"
|
2025-12-04 17:09:04 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UserHandler struct {
|
2025-12-05 09:59:57 +08:00
|
|
|
userService *account.UserService
|
2025-12-04 17:09:04 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-05 09:59:57 +08:00
|
|
|
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 {
|
2025-12-05 13:10:19 +08:00
|
|
|
Username string `json:"username"`
|
|
|
|
|
OldPassword string `json:"old_password"`
|
2025-12-05 09:59:57 +08:00
|
|
|
NewPassword string `json:"new_password"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ChangePasswordResponse struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUserHandler(userService *account.UserService) *UserHandler {
|
2025-12-04 17:09:04 +08:00
|
|
|
return &UserHandler{userService: userService}
|
|
|
|
|
}
|
|
|
|
|
func (h *UserHandler) CreateUser(c *gin.Context) {
|
2025-12-05 09:59:57 +08:00
|
|
|
var req CreateUserRequest
|
2025-12-04 17:09:04 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-05 09:59:57 +08:00
|
|
|
if err := h.userService.CreateUser(&account.User{
|
2025-12-04 17:09:04 +08:00
|
|
|
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) {
|
2025-12-05 09:59:57 +08:00
|
|
|
var req RenameByIDRequest
|
2025-12-04 17:09:04 +08:00
|
|
|
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) {
|
2025-12-05 09:59:57 +08:00
|
|
|
var req ChangePasswordRequest
|
2025-12-04 17:09:04 +08:00
|
|
|
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) {
|
2025-12-05 09:59:57 +08:00
|
|
|
var req FindByIDRequest
|
2025-12-04 17:09:04 +08:00
|
|
|
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) {
|
2025-12-05 09:59:57 +08:00
|
|
|
var req FindByUsernameRequest
|
2025-12-04 17:09:04 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|