move handler from http to module

This commit is contained in:
Leon
2025-12-08 20:35:56 +08:00
parent 79454f1275
commit 6a3480698a
4 changed files with 11 additions and 13 deletions

View File

@@ -1,167 +0,0 @@
package http
import (
"feedsystem_video_go/internal/account"
"github.com/gin-gonic/gin"
)
type AccountHandler struct {
accountService *account.AccountService
}
type CreateAccountRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type CreateAccountResponse 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 {
Username string `json:"username"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}
type ChangePasswordResponse struct {
}
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type LoginResponse struct {
Token string `json:"token"`
}
type LogoutRequest struct {
ID uint `json:"id"`
}
type LogoutResponse struct {
}
func NewAccountHandler(accountService *account.AccountService) *AccountHandler {
return &AccountHandler{accountService: accountService}
}
func (h *AccountHandler) CreateAccount(c *gin.Context) {
var req CreateAccountRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.accountService.CreateAccount(&account.Account{
Username: req.Username,
Password: req.Password,
}); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "account created"})
}
func (h *AccountHandler) 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.accountService.RenameByID(req.ID, req.NewUsername); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "account renamed"})
}
func (h *AccountHandler) 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.accountService.ChangePassword(req.Username, req.OldPassword, req.NewPassword); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "password changed"})
}
func (h *AccountHandler) FindByID(c *gin.Context) {
var req FindByIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if account, err := h.accountService.FindByID(req.ID); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, account)
}
}
func (h *AccountHandler) FindByUsername(c *gin.Context) {
var req FindByUsernameRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if account, err := h.accountService.FindByUsername(req.Username); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, account)
}
}
func (h *AccountHandler) Login(c *gin.Context) {
var req LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if token, err := h.accountService.Login(req.Username, req.Password); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, LoginResponse{Token: token})
}
}
func (h *AccountHandler) Logout(c *gin.Context) {
var req LogoutRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.accountService.Logout(req.ID); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, LogoutResponse{})
}

View File

@@ -1,43 +0,0 @@
package http
import (
"feedsystem_video_go/internal/feed"
"time"
"github.com/gin-gonic/gin"
)
type ListLatestRequest struct {
Limit int `json:"limit"`
LatestTime int64 `json:"latest_time"`
}
type FeedHandler struct {
service *feed.FeedService
}
func NewFeedHandler(service *feed.FeedService) *FeedHandler {
return &FeedHandler{service: service}
}
func (f *FeedHandler) ListLatest(c *gin.Context) {
var req ListLatestRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if req.Limit <= 0 || req.Limit > 50 {
req.Limit = 10
}
ctx := c.Request.Context()
var latestTime time.Time
if req.LatestTime > 0 {
latestTime = time.Unix(req.LatestTime, 0)
}
feeds, err := f.service.ListLatest(ctx, req.Limit, latestTime)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, feeds)
}

View File

@@ -15,7 +15,7 @@ func SetRouter(db *gorm.DB) *gin.Engine {
// account
accountRepository := account.NewAccountRepository(db)
accountService := account.NewAccountService(accountRepository)
accountHandler := NewAccountHandler(accountService)
accountHandler := account.NewAccountHandler(accountService)
accountGroup := r.Group("/account")
{
accountGroup.POST("/register", accountHandler.CreateAccount)
@@ -33,7 +33,7 @@ func SetRouter(db *gorm.DB) *gin.Engine {
// video
videoRepository := video.NewVideoRepository(db)
videoService := video.NewVideoService(videoRepository)
videoHandler := NewVideoHandler(videoService)
videoHandler := video.NewVideoHandler(videoService)
videoGroup := r.Group("/video")
{
videoGroup.POST("/listByAuthorID", videoHandler.ListByAuthorID)
@@ -48,7 +48,7 @@ func SetRouter(db *gorm.DB) *gin.Engine {
// feed
feedRepository := feed.NewFeedRepository(db)
feedService := feed.NewFeedService(feedRepository)
feedHandler := NewFeedHandler(feedService)
feedHandler := feed.NewFeedHandler(feedService)
feedGroup := r.Group("/feed")
{
feedGroup.POST("/listLatest", feedHandler.ListLatest)

View File

@@ -1,96 +0,0 @@
package http
import (
"feedsystem_video_go/internal/video"
"time"
"github.com/gin-gonic/gin"
)
type VideoHandler struct {
service *video.VideoService
}
func NewVideoHandler(service *video.VideoService) *VideoHandler {
return &VideoHandler{service: service}
}
type PublishVideoRequest struct {
Title string `json:"title"`
Description string `json:"description"`
PlayURL string `json:"play_url"`
}
type ListByAuthorIDRequest struct {
AuthorID uint `json:"author_id"`
}
type GetDetailRequest struct {
ID uint `json:"id"`
}
func (vh *VideoHandler) PublishVideo(c *gin.Context) {
var req PublishVideoRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if req.Title == "" {
c.JSON(400, gin.H{"error": "title is required"})
return
}
if req.PlayURL == "" {
c.JSON(400, gin.H{"error": "play url is required"})
return
}
uidValue, exists := c.Get("accountID")
if !exists {
c.JSON(400, gin.H{"error": "accountID not found"})
return
}
authorID, ok := uidValue.(uint)
if !ok {
c.JSON(400, gin.H{"error": "accountID has invalid type"})
return
}
video := &video.Video{
AuthorID: authorID,
Title: req.Title,
Description: req.Description,
PlayURL: req.PlayURL,
CreateTime: time.Now(),
}
if err := vh.service.Publish(c, video); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, video)
}
func (vh *VideoHandler) ListByAuthorID(c *gin.Context) {
var req ListByAuthorIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
videos, err := vh.service.ListByAuthorID(c, req.AuthorID)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, videos)
}
func (vh *VideoHandler) GetDetail(c *gin.Context) {
var req GetDetailRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
video, err := vh.service.GetDetail(c, req.ID)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, video)
}