113 lines
3.3 KiB
Go
113 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"video-factory/common"
|
|
|
|
"video-factory/shortdrama/consts/public"
|
|
"video-factory/shortdrama/dao"
|
|
"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 := common.GetRole(r)
|
|
if role != "customer" {
|
|
return nil
|
|
}
|
|
userId := common.GetUserId(r)
|
|
d, err := dao.Drama.GetOne(ctx, dramaId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d == nil {
|
|
return fmt.Errorf("drama not found")
|
|
}
|
|
if d.UserId != userId {
|
|
return fmt.Errorf("no permission to operate on this drama")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ==================== Drama CRUD ====================
|
|
|
|
func (c *drama) List(ctx context.Context, req *dto.ListDramaReq) (res *dto.ListDramaRes, err error) {
|
|
r := g.RequestFromCtx(ctx)
|
|
role := common.GetRole(r)
|
|
userId := common.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 := common.GetUserId(r)
|
|
id, err := service.DramaService.Create(ctx, req.Title, req.Type, req.Config, req.AspectRatio, int64(req.EpisodeDuration), 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) {
|
|
drama, err := dao.Drama.GetOne(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if drama == nil {
|
|
return nil, fmt.Errorf("drama not found")
|
|
}
|
|
// 权限校验
|
|
r := g.RequestFromCtx(ctx)
|
|
role := common.GetRole(r)
|
|
userId := common.GetUserId(r)
|
|
if role == "customer" && drama.UserId != userId {
|
|
return nil, fmt.Errorf("no permission to operate on this drama")
|
|
}
|
|
|
|
d, characters, scenes, props, backgroundMusic, err := service.DramaService.Get(ctx, drama)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.GetDramaRes{
|
|
Drama: d,
|
|
Characters: characters,
|
|
Scenes: scenes,
|
|
Props: props,
|
|
BackgroundMusic: backgroundMusic,
|
|
}, 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.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
|
|
}
|