You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

9 months ago
  1. package api
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/gogf/gf/v2/database/gdb"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. v1 "xgit.pub/module/cms/app/api/api/v1"
  8. "xgit.pub/module/cms/app/dao"
  9. "xgit.pub/module/cms/app/model/entity"
  10. "xgit.pub/module/cms/app/service"
  11. )
  12. type sApi struct {
  13. }
  14. func init() {
  15. Api := New()
  16. service.RegisterApi(Api)
  17. }
  18. func New() *sApi {
  19. return &sApi{}
  20. }
  21. // Create 创建
  22. func (s *sApi) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  23. err = dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  24. if req.CollectId < 1 {
  25. return errors.New("请输入采集站点信息")
  26. }
  27. collectName := service.Collect().GetCollectNameById(ctx, req.CollectId)
  28. if collectName == "" {
  29. err = gerror.New("采集站点信息不存在")
  30. return err
  31. }
  32. video, _ := s.GetBySourceUrl(ctx, req.SourceUrl)
  33. if video != nil && video.Id > 0 {
  34. _, err = dao.Video.Ctx(ctx).Data(req).Where(dao.Video.Columns().Id, video.Id).Update()
  35. } else {
  36. _, err = dao.Video.Ctx(ctx).InsertAndGetId(req)
  37. }
  38. return err
  39. })
  40. return
  41. }
  42. func (s *sApi) GetBySourceUrl(ctx context.Context, SourceUrl string) (res *entity.Video, err error) {
  43. err = dao.Video.Ctx(ctx).Where("source_url", SourceUrl).Scan(&res)
  44. return
  45. }