feat: Update project structure and configuration files

This commit is contained in:
rogeecn
2025-03-24 09:29:38 +08:00
parent ea15a51556
commit 8de43c2861
159 changed files with 498 additions and 0 deletions

View 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
}

View 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)
})
})
})
}

View File

@@ -0,0 +1,29 @@
// 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 Posts *postsModel
var Users *usersModel
// @provider(model)
type models struct {
db *sql.DB
medias *mediasModel
posts *postsModel
users *usersModel
}
func (m *models) Prepare() error {
db = m.db
Medias = m.medias
Posts = m.posts
Users = m.users
return nil
}

206
backend/app/models/posts.go Normal file
View File

@@ -0,0 +1,206 @@
package models
import (
"context"
"errors"
"quyun/app/requests"
"quyun/database/fields"
"quyun/database/schemas/public/model"
"quyun/database/schemas/public/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/qrm"
"github.com/sirupsen/logrus"
)
// @provider
type postsModel struct {
log *logrus.Entry `inject:"false"`
}
func (m *postsModel) Prepare() error {
m.log = logrus.WithField("model", "postsModel")
return nil
}
// BuildConditionWithKey
func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
tbl := table.Posts
cond := tbl.DeletedAt.IS_NULL().AND(
tbl.Status.EQ(Int32(int32(fields.PostStatusPublished))),
)
if key == nil || *key == "" {
return cond
}
cond = tbl.Title.LIKE(String("%" + *key + "%")).
OR(
tbl.Content.LIKE(String("%" + *key + "%")),
).
OR(
tbl.Description.LIKE(String("%" + *key + "%")),
)
return cond
}
// GetByID
func (m *postsModel) GetByID(ctx context.Context, id int64) (*model.Posts, error) {
tbl := table.Posts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.ID.EQ(Int64(id)).AND(
tbl.DeletedAt.IS_NULL(),
).AND(
tbl.Status.EQ(Int32(int32(fields.PostStatusPublished))),
),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var post model.Posts
err := stmt.QueryContext(ctx, db, &post)
if err != nil {
m.log.Errorf("error getting post: %v", err)
return nil, err
}
return &post, nil
}
// Create
func (m *postsModel) Create(ctx context.Context, model *model.Posts) error {
tbl := table.Posts
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(model)
m.log.Infof("sql: %s", stmt.DebugSql())
_, err := stmt.ExecContext(ctx, db)
if err != nil {
m.log.Errorf("error creating post: %v", err)
return err
}
return nil
}
// Update
func (m *postsModel) Update(ctx context.Context, id int64, model *model.Posts) error {
tbl := table.Posts
stmt := tbl.UPDATE(tbl.MutableColumns).SET(model).WHERE(tbl.ID.EQ(Int64(id)))
m.log.Infof("sql: %s", stmt.DebugSql())
_, err := stmt.ExecContext(ctx, db)
if err != nil {
m.log.Errorf("error updating post: %v", err)
return err
}
return nil
}
// countByCond
func (m *postsModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
var cnt struct {
Cnt int64
}
tbl := table.Posts
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 post items: %v", err)
return 0, err
}
return cnt.Cnt, nil
}
func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
limit := pagination.Limit
offset := pagination.Offset()
tbl := table.Posts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(cond).
ORDER_BY(tbl.ID.DESC()).
LIMIT(limit).
OFFSET(offset)
m.log.Infof("sql: %s", stmt.DebugSql())
var posts []model.Posts
err := stmt.QueryContext(ctx, db, &posts)
if err != nil {
m.log.Errorf("error querying post items: %v", err)
return nil, err
}
count, err := m.countByCondition(ctx, cond)
if err != nil {
m.log.Errorf("error getting post count: %v", err)
return nil, err
}
return &requests.Pager{
Items: posts,
Total: count,
Pagination: *pagination,
}, nil
}
func (m *postsModel) IsUserBought(ctx context.Context, userId, postId int64) (bool, error) {
tbl := table.UserPosts
stmt := tbl.
SELECT(tbl.ID).
WHERE(
tbl.UserID.EQ(Int64(userId)).AND(
tbl.PostID.EQ(Int64(postId)),
),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var userPost model.UserPosts
err := stmt.QueryContext(ctx, db, &userPost)
if err != nil {
if errors.Is(err, qrm.ErrNoRows) {
return false, nil
}
m.log.Errorf("error querying user post item: %v", err)
return false, err
}
return userPost.ID > 0, nil
}
func (m *postsModel) Buy(ctx context.Context, userId, postId int64) error {
tbl := table.UserPosts
post, err := m.GetByID(ctx, postId)
if err != nil {
m.log.Errorf("error getting post by ID: %v", err)
return err
}
user, err := Users.GetByID(ctx, userId)
if err != nil {
m.log.Errorf("error getting user by ID: %v", err)
return err
}
record := model.UserPosts{
UserID: user.ID,
PostID: post.ID,
Price: post.Price * int64(post.Discount) / 100,
}
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(record)
m.log.Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log.Errorf("error buying post: %v", err)
return err
}
return nil
}

View File

@@ -0,0 +1,43 @@
package models
import (
"context"
"testing"
"quyun/app/service/testx"
"quyun/database"
"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 PostsInjectParams struct {
dig.In
Initials []contracts.Initial `group:"initials"`
}
type PostsTestSuite struct {
suite.Suite
PostsInjectParams
}
func Test_Posts(t *testing.T) {
providers := testx.Default().With(Provide)
testx.Serve(providers, t, func(params PostsInjectParams) {
suite.Run(t, &PostsTestSuite{
PostsInjectParams: params,
})
})
}
func (s *PostsTestSuite) Test_Demo() {
Convey("Test_Demo", s.T(), func() {
database.Truncate(context.Background(), db, table.Posts.TableName())
})
}

View File

@@ -0,0 +1,64 @@
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(
db *sql.DB,
medias *mediasModel,
posts *postsModel,
users *usersModel,
) (contracts.Initial, error) {
obj := &models{
db: db,
medias: medias,
posts: posts,
users: users,
}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}, atom.GroupInitial); err != nil {
return err
}
if err := container.Container.Provide(func() (*postsModel, error) {
obj := &postsModel{}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func() (*usersModel, error) {
obj := &usersModel{}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,77 @@
package models
import (
"context"
"quyun/database/schemas/public/model"
"quyun/database/schemas/public/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
// @provider
type usersModel struct {
log *logrus.Entry `inject:"false"`
}
func (m *usersModel) Prepare() error {
m.log = logrus.WithField("model", "usersModel")
return nil
}
// GetByID
func (m *usersModel) GetByID(ctx context.Context, id int64) (*model.Users, error) {
tbl := table.Users
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.ID.EQ(Int64(id)),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var user model.Users
err := stmt.QueryContext(ctx, db, &user)
if err != nil {
m.log.Errorf("error querying user by ID: %v", err)
return nil, err
}
return &user, nil
}
func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, error) {
tblUserPosts := table.UserPosts
stmtUserPosts := tblUserPosts.
SELECT(tblUserPosts.PostID).
WHERE(tblUserPosts.UserID.EQ(Int64(userID)))
m.log.Infof("sql: %s", stmtUserPosts.DebugSql())
var userPosts []model.UserPosts
err := stmtUserPosts.QueryContext(ctx, db, &userPosts)
if err != nil {
m.log.Errorf("error querying user posts: %v", err)
return nil, err
}
postIds := lo.Map(userPosts, func(up model.UserPosts, _ int) Expression {
return Int64(up.PostID)
})
tbl := table.Posts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(tbl.ID.IN(postIds...))
m.log.Infof("sql: %s", stmt.DebugSql())
var posts []*model.Posts
if err := stmt.QueryContext(ctx, db, &posts); err != nil {
m.log.Errorf("error querying posts by user ID: %v", err)
return nil, err
}
return posts, nil
}

View File

@@ -0,0 +1,43 @@
package models
import (
"context"
"testing"
"quyun/app/service/testx"
"quyun/database"
"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 UsersInjectParams struct {
dig.In
Initials []contracts.Initial `group:"initials"`
}
type UsersTestSuite struct {
suite.Suite
UsersInjectParams
}
func Test_Users(t *testing.T) {
providers := testx.Default().With(Provide)
testx.Serve(providers, t, func(params UsersInjectParams) {
suite.Run(t, &UsersTestSuite{
UsersInjectParams: params,
})
})
}
func (s *UsersTestSuite) Test_Demo() {
Convey("Test_Demo", s.T(), func() {
database.Truncate(context.Background(), db, table.Users.TableName())
})
}