feat: Update project structure and configuration files
This commit is contained in:
86
backend/app/models/medias.go
Normal file
86
backend/app/models/medias.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type mediasModel struct {
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (m *mediasModel) Prepare() error {
|
||||
m.log = logrus.WithField("module", "mediasModel")
|
||||
return nil
|
||||
}
|
||||
|
||||
// countByCond
|
||||
func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
|
||||
var cnt struct {
|
||||
Cnt int64
|
||||
}
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
err := stmt.QueryContext(ctx, db, &cnt)
|
||||
if err != nil {
|
||||
m.log.Errorf("error counting media items: %v", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return cnt.Cnt, nil
|
||||
}
|
||||
|
||||
func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination) (*requests.Pager, error) {
|
||||
limit := pagination.Limit
|
||||
offset := pagination.Offset()
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(limit).
|
||||
OFFSET(offset)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var medias []model.Medias
|
||||
err := stmt.QueryContext(ctx, db, &medias)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying media items: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := m.countByCondition(ctx, Bool(true))
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting media count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: medias,
|
||||
Total: count,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *mediasModel) Create(ctx context.Context, model *model.Medias) error {
|
||||
stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(model)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log.Errorf("error creating media item: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log.Infof("media item created successfully")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user