feat: update jobs

This commit is contained in:
yanghao05
2025-04-22 20:01:50 +08:00
parent 163a7c11fe
commit 707cbbb639
21 changed files with 359 additions and 87 deletions

View File

@@ -106,10 +106,10 @@ func (m *mediasModel) BatchCreate(ctx context.Context, models []*model.Medias) e
func (m *mediasModel) Create(ctx context.Context, model *model.Medias) error {
model.CreatedAt = time.Now()
stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(model)
stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(model).RETURNING(table.Medias.AllColumns)
m.log.Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
if err := stmt.QueryContext(ctx, db, model); err != nil {
m.log.Errorf("error creating media item: %v", err)
return err
}
@@ -236,3 +236,24 @@ func (m *mediasModel) UpdateMetas(ctx context.Context, id int64, metas fields.Me
m.log.Infof("media (%d) metas updated successfully", id)
return nil
}
// GetRelationMedias
func (m *mediasModel) GetRelations(ctx context.Context, hash string) ([]*model.Medias, error) {
tbl := table.Medias
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
RawBool("metas->>'parent_hash' = ?", RawArgs{"?": hash}),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var medias []model.Medias
if err := stmt.QueryContext(ctx, db, &medias); err != nil {
m.log.Errorf("error querying media items: %v", err)
return nil, err
}
return lo.Map(medias, func(media model.Medias, _ int) *model.Medias {
return &media
}), nil
}

View File

@@ -188,3 +188,32 @@ func (s *MediasTestSuite) Test_Page() {
})
})
}
func (s *MediasTestSuite) Test_CreateGetID() {
Convey("Create", s.T(), func() {
model := &model.Medias{
Name: fmt.Sprintf("test-%d", 1),
CreatedAt: time.Now(),
MimeType: "application/pdf",
Size: 100,
Path: "path/to/media.pdf",
}
err := Medias.Create(context.Background(), model)
So(err, ShouldBeNil)
So(model.ID, ShouldNotBeEmpty)
s.T().Logf("model id :%d", model.ID)
})
}
func (s *MediasTestSuite) Test_GetRelations() {
Convey("GetByHash", s.T(), func() {
hash := "ce4cd071128cef282cf315dda75bdab4"
media, err := Medias.GetRelations(context.Background(), hash)
So(err, ShouldBeNil)
So(media, ShouldNotBeNil)
s.T().Logf("media: %+v", media)
})
}