161 lines
5.0 KiB
Go
161 lines
5.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"video-factory/shortdrama/consts/public"
|
|
"video-factory/shortdrama/dao"
|
|
"video-factory/shortdrama/middleware"
|
|
"video-factory/shortdrama/model/dto"
|
|
"video-factory/shortdrama/service"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
type drama struct{}
|
|
|
|
var Drama = new(drama)
|
|
|
|
// checkDramaAccess 校验客户只能操作自己的短剧
|
|
func checkDramaAccess(ctx context.Context, dramaId int64) error {
|
|
r := g.RequestFromCtx(ctx)
|
|
role := middleware.GetRole(r)
|
|
if role != "customer" {
|
|
return nil
|
|
}
|
|
userId := middleware.GetUserId(r)
|
|
d, err := dao.Drama.GetOne(ctx, dramaId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d == nil {
|
|
return fmt.Errorf("短剧不存在")
|
|
}
|
|
if d.UserId != userId {
|
|
return fmt.Errorf("无权操作该短剧")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ==================== Drama CRUD ====================
|
|
|
|
func (c *drama) List(ctx context.Context, req *dto.ListDramaReq) (res *dto.ListDramaRes, err error) {
|
|
r := g.RequestFromCtx(ctx)
|
|
role := middleware.GetRole(r)
|
|
userId := middleware.GetUserId(r)
|
|
list, total, err := service.DramaService.List(ctx, req.Page, req.PageSize, role, userId, req.Keyword)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.ListDramaRes{List: list, Total: total, Page: req.Page, PageSize: req.PageSize}, nil
|
|
}
|
|
|
|
func (c *drama) Create(ctx context.Context, req *dto.CreateDramaReq) (res *dto.CreateDramaRes, err error) {
|
|
r := g.RequestFromCtx(ctx)
|
|
userId := middleware.GetUserId(r)
|
|
id, err := service.DramaService.Create(ctx, req.Title, req.Type, req.Config, req.AspectRatio, int64(req.EpisodeDuration), req.MinShotDuration, req.MaxShotDuration, req.Resolution, userId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.CreateDramaRes{Id: id}, nil
|
|
}
|
|
|
|
func (c *drama) Get(ctx context.Context, req *dto.GetDramaReq) (res *dto.GetDramaRes, err error) {
|
|
if err := checkDramaAccess(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
drama, characters, episodes, scenes, props, backgroundMusic, tasks, err := service.DramaService.Get(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.GetDramaRes{
|
|
Drama: drama,
|
|
Characters: characters,
|
|
Episodes: episodes,
|
|
Scenes: scenes,
|
|
Props: props,
|
|
BackgroundMusic: backgroundMusic,
|
|
Tasks: tasks,
|
|
}, nil
|
|
}
|
|
|
|
func (c *drama) Update(ctx context.Context, req *dto.UpdateDramaReq) (res *struct{}, err error) {
|
|
if err := checkDramaAccess(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, service.DramaService.Update(ctx, req.Id, req.Title, req.Type, req.Config, req.AspectRatio, int64(req.EpisodeDuration), req.MinShotDuration, req.MaxShotDuration, req.Resolution)
|
|
}
|
|
|
|
func (c *drama) Delete(ctx context.Context, req *dto.DeleteDramaReq) (res *struct{}, err error) {
|
|
if err := checkDramaAccess(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, service.DramaService.Delete(ctx, req.Id)
|
|
}
|
|
|
|
func (c *drama) GetFieldDefinitions(ctx context.Context, req *dto.GetFieldDefinitionsReq) (res *dto.GetFieldDefinitionsRes, err error) {
|
|
return &dto.GetFieldDefinitionsRes{
|
|
ContentTypes: []string{public.TypeShortDrama, public.TypeComicDrama, public.TypeAdVideo},
|
|
Fields: public.ContentTypeFields,
|
|
}, nil
|
|
}
|
|
|
|
// ==================== Background Music CRUD ====================
|
|
|
|
func (c *drama) ListBackgroundMusic(ctx context.Context, req *dto.ListBackgroundMusicReq) (res *dto.ListBackgroundMusicRes, err error) {
|
|
list, err := service.DramaService.ListBackgroundMusic(ctx, req.DramaId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.ListBackgroundMusicRes{List: list}, nil
|
|
}
|
|
|
|
func (c *drama) AddBackgroundMusic(ctx context.Context, req *dto.AddBackgroundMusicReq) (res *dto.GetDramaRes, err error) {
|
|
var filePath string
|
|
if req.AudioFile != nil {
|
|
f, err := req.AudioFile.Open()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
filePath, err = service.DramaService.SaveScenePropFile(ctx, req.DramaId, f, req.AudioFile.Filename, "背景音乐", req.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
_, err = service.DramaService.AddBackgroundMusic(ctx, req.DramaId, req.Name, filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.Get(ctx, &dto.GetDramaReq{Id: req.DramaId})
|
|
}
|
|
|
|
func (c *drama) UpdateBackgroundMusic(ctx context.Context, req *dto.UpdateBackgroundMusicReq) (res *dto.GetDramaRes, err error) {
|
|
var filePath string
|
|
if req.AudioFile != nil {
|
|
f, err := req.AudioFile.Open()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
filePath, err = service.DramaService.SaveScenePropFile(ctx, req.DramaId, f, req.AudioFile.Filename, "背景音乐", req.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
err = service.DramaService.UpdateBackgroundMusic(ctx, req.Id, req.DramaId, req.Name, filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.Get(ctx, &dto.GetDramaReq{Id: req.DramaId})
|
|
}
|
|
|
|
func (c *drama) DeleteBackgroundMusic(ctx context.Context, req *dto.DeleteBackgroundMusicReq) (res *dto.GetDramaRes, err error) {
|
|
err = service.DramaService.DeleteBackgroundMusic(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.Get(ctx, &dto.GetDramaReq{Id: req.DramaId})
|
|
}
|