feat: add video module
This commit is contained in:
105
internal/http/video_handler.go
Normal file
105
internal/http/video_handler.go
Normal file
@@ -0,0 +1,105 @@
|
||||
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("account_id")
|
||||
if !exists {
|
||||
c.JSON(400, gin.H{"error": "account_id not found"})
|
||||
return
|
||||
}
|
||||
authorID, ok := uidValue.(uint)
|
||||
if !ok {
|
||||
c.JSON(400, gin.H{"error": "account_id 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) ListLatest(c *gin.Context) {
|
||||
videos, err := vh.service.ListLatest(c)
|
||||
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)
|
||||
}
|
||||
12
internal/video/entity.go
Normal file
12
internal/video/entity.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package video
|
||||
|
||||
import "time"
|
||||
|
||||
type Video struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
AuthorID uint `gorm:"index;not null"`
|
||||
Title string `gorm:"type:varchar(255);not null"`
|
||||
Description string `gorm:"type:varchar(255);"`
|
||||
PlayURL string `gorm:"type:varchar(255);not null"`
|
||||
CreateTime time.Time `gorm:"autoCreateTime"`
|
||||
}
|
||||
55
internal/video/repo.go
Normal file
55
internal/video/repo.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type VideoRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewVideoRepository(db *gorm.DB) *VideoRepository {
|
||||
return &VideoRepository{db: db}
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) CreateVideo(ctx context.Context, video *Video) error {
|
||||
if err := vr.db.WithContext(ctx).Create(video).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) ListByAuthorID(ctx context.Context, authorID int64) ([]Video, error) {
|
||||
var videos []Video
|
||||
if err := vr.db.WithContext(ctx).
|
||||
Where("author_id = ?", authorID).
|
||||
Order("create_time desc").
|
||||
Limit(5).
|
||||
Offset(0).
|
||||
Find(&videos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return videos, nil
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) ListLatest(ctx context.Context) ([]Video, error) {
|
||||
var videos []Video
|
||||
if err := vr.db.WithContext(ctx).
|
||||
Order("create_time desc").
|
||||
Limit(5).
|
||||
Offset(0).
|
||||
Find(&videos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return videos, nil
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) GetByID(ctx context.Context, id uint) (*Video, error) {
|
||||
var video Video
|
||||
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &video, nil
|
||||
}
|
||||
51
internal/video/service.go
Normal file
51
internal/video/service.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type VideoService struct {
|
||||
repo *VideoRepository
|
||||
}
|
||||
|
||||
func NewVideoService(repo *VideoRepository) *VideoService {
|
||||
return &VideoService{repo: repo}
|
||||
}
|
||||
|
||||
func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
||||
if video.Title == "" {
|
||||
return errors.New("title is required")
|
||||
}
|
||||
if video.PlayURL == "" {
|
||||
return errors.New("play url is required")
|
||||
}
|
||||
if err := vs.repo.CreateVideo(ctx, video); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vs *VideoService) ListByAuthorID(ctx context.Context, authorID uint) ([]Video, error) {
|
||||
videos, err := vs.repo.ListByAuthorID(ctx, int64(authorID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return videos, nil
|
||||
}
|
||||
|
||||
func (vs *VideoService) ListLatest(ctx context.Context) ([]Video, error) {
|
||||
videos, err := vs.repo.ListLatest(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return videos, nil
|
||||
}
|
||||
|
||||
func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error) {
|
||||
video, err := vs.repo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return video, nil
|
||||
}
|
||||
Reference in New Issue
Block a user