init
This commit is contained in:
86
app/models/medias.go
Normal file
86
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
|
||||
}
|
||||
134
app/models/medias_test.go
Normal file
134
app/models/medias_test.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/app/service/testx"
|
||||
"quyun/database"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
|
||||
// . "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type MediasInjectParams struct {
|
||||
dig.In
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
}
|
||||
|
||||
type MediasTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
MediasInjectParams
|
||||
}
|
||||
|
||||
func Test_medias(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
testx.Serve(providers, t, func(params MediasInjectParams) {
|
||||
suite.Run(t, &MediasTestSuite{MediasInjectParams: params})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MediasTestSuite) Test_countByCondition() {
|
||||
Convey("countByCondition", s.T(), func() {
|
||||
Convey("no cond", func() {
|
||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
||||
|
||||
cnt, err := Medias.countByCondition(context.Background(), nil)
|
||||
Convey("should not return an error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
Convey("should return a count of zero", func() {
|
||||
So(cnt, ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MediasTestSuite) Test_Create() {
|
||||
Convey("Create", s.T(), func() {
|
||||
Convey("valid media", func() {
|
||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
||||
|
||||
model := &model.Medias{
|
||||
Name: "test",
|
||||
CreatedAt: time.Now(),
|
||||
MimeType: "application/pdf",
|
||||
Size: 100,
|
||||
Path: "path/to/media.pdf",
|
||||
}
|
||||
|
||||
err := Medias.Create(context.Background(), model)
|
||||
Convey("Create should not return an error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
cnt, err := Medias.countByCondition(context.Background(), nil)
|
||||
Convey("Count should not return an error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
Convey("should return a count of one", func() {
|
||||
So(cnt, ShouldEqual, 1)
|
||||
})
|
||||
Convey("should create the media successfully", func() {
|
||||
So(model.ID, ShouldNotBeEmpty)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MediasTestSuite) Test_Page() {
|
||||
Convey("Create", s.T(), func() {
|
||||
Convey("Insert Items", func() {
|
||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
model := &model.Medias{
|
||||
Name: fmt.Sprintf("test-%d", i),
|
||||
CreatedAt: time.Now(),
|
||||
MimeType: "application/pdf",
|
||||
Size: 100,
|
||||
Path: "path/to/media.pdf",
|
||||
}
|
||||
|
||||
err := Medias.Create(context.Background(), model)
|
||||
So(err, ShouldBeNil)
|
||||
}
|
||||
|
||||
cnt, err := Medias.countByCondition(context.Background(), nil)
|
||||
So(err, ShouldBeNil)
|
||||
So(cnt, ShouldEqual, 20)
|
||||
})
|
||||
|
||||
Convey("Page", func() {
|
||||
Convey("page 1", func() {
|
||||
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 1, Limit: 10})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 20)
|
||||
So(pager.Items, ShouldHaveLength, 10)
|
||||
})
|
||||
Convey("page 2", func() {
|
||||
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 2, Limit: 10})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 20)
|
||||
So(pager.Items, ShouldHaveLength, 10)
|
||||
})
|
||||
|
||||
Convey("page 3", func() {
|
||||
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 3, Limit: 10})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 20)
|
||||
So(pager.Items, ShouldBeEmpty)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
8
app/models/migrations.go
Normal file
8
app/models/migrations.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
// @provider
|
||||
type migrationsModel struct{}
|
||||
|
||||
func (m *migrationsModel) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
26
app/models/models.gen.go
Normal file
26
app/models/models.gen.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
var Medias *mediasModel
|
||||
var Migrations *migrationsModel
|
||||
|
||||
// @provider(model)
|
||||
type models struct {
|
||||
db *sql.DB
|
||||
medias *mediasModel
|
||||
migrations *migrationsModel
|
||||
}
|
||||
|
||||
func (m *models) Prepare() error {
|
||||
db = m.db
|
||||
Medias = m.medias
|
||||
Migrations = m.migrations
|
||||
return nil
|
||||
}
|
||||
49
app/models/provider.gen.go
Executable file
49
app/models/provider.gen.go
Executable file
@@ -0,0 +1,49 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func() (*mediasModel, error) {
|
||||
obj := &mediasModel{}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*migrationsModel, error) {
|
||||
obj := &migrationsModel{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
db *sql.DB,
|
||||
medias *mediasModel,
|
||||
migrations *migrationsModel,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &models{
|
||||
db: db,
|
||||
medias: medias,
|
||||
migrations: migrations,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupInitial); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user