complete backend

This commit is contained in:
yanghao05
2025-04-11 14:48:36 +08:00
parent 736991e3ea
commit 79972e963c
10 changed files with 131 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package models
import (
"context"
"time"
"quyun/app/requests"
"quyun/database/fields"
@@ -127,6 +128,7 @@ 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)
m.log.Infof("sql: %s", stmt.DebugSql())
@@ -193,3 +195,39 @@ func (m *mediasModel) GetByIds(ctx context.Context, ids []int64) ([]*model.Media
return &media
}), nil
}
// GetByHash
func (m *mediasModel) GetByHash(ctx context.Context, hash string) (*model.Medias, error) {
tbl := table.Medias
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(tbl.Hash.EQ(String(hash)))
m.log.Infof("sql: %s", stmt.DebugSql())
var media model.Medias
err := stmt.QueryContext(ctx, db, &media)
if err != nil {
m.log.Errorf("error querying media item by hash: %v", err)
return nil, err
}
return &media, nil
}
// Update
func (m *mediasModel) Update(ctx context.Context, hash string, model *model.Medias) error {
tbl := table.Medias
stmt := tbl.
UPDATE(tbl.MutableColumns.Except(tbl.CreatedAt)).
MODEL(model).
WHERE(table.Medias.Hash.EQ(String(hash)))
m.log.Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log.Errorf("error updating media item: %v", err)
return err
}
m.log.Infof("media item updated successfully")
return nil
}