feat: update toolchain
This commit is contained in:
@@ -54,7 +54,7 @@ func (ctl *medias) Delete(ctx fiber.Ctx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := media.ForceDelete(ctx.Context()); err != nil {
|
if err := media.Delete(ctx.Context()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return ctx.SendStatus(fiber.StatusNoContent)
|
return ctx.SendStatus(fiber.StatusNoContent)
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -15,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func (m *Medias) CondID(id int64) Cond {
|
func (m *Medias) CondID(id int64) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Medias.ID.EQ(Int(id)))
|
return cond.AND(tblMedias.ID.EQ(Int(id)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +25,7 @@ func (m *Medias) log() *log.Entry {
|
|||||||
func (m *Medias) Create(ctx context.Context) error {
|
func (m *Medias) Create(ctx context.Context) error {
|
||||||
m.CreatedAt = time.Now()
|
m.CreatedAt = time.Now()
|
||||||
|
|
||||||
stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
stmt := tblMedias.INSERT(tblMedias.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
|
||||||
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -40,7 +38,7 @@ func (m *Medias) Create(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Medias) BatchCreate(ctx context.Context, models []*Medias) error {
|
func (m *Medias) BatchCreate(ctx context.Context, models []*Medias) error {
|
||||||
stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODELS(models)
|
stmt := tblMedias.INSERT(tblMedias.MutableColumns).MODELS(models)
|
||||||
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -52,11 +50,11 @@ func (m *Medias) BatchCreate(ctx context.Context, models []*Medias) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Medias) ForceDelete(ctx context.Context) error {
|
func (m *Medias) Delete(ctx context.Context) error {
|
||||||
stmt := table.Medias.DELETE().WHERE(table.Medias.ID.EQ(Int(m.ID)))
|
stmt := tblMedias.DELETE().WHERE(tblMedias.ID.EQ(Int(m.ID)))
|
||||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
m.log().WithField("func", "Delete").Errorf("error deleting Medias item: %v", err)
|
m.log().WithField("func", "Delete").Errorf("error deleting Medias item: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -65,43 +63,42 @@ func (m *Medias) ForceDelete(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Medias) BatchForceDelete(ctx context.Context, ids []int64) error {
|
// BatchDelete
|
||||||
|
func (m *Medias) BatchDelete(ctx context.Context, ids []int64) error {
|
||||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
stmt := table.Medias.DELETE().WHERE(table.Medias.ID.IN(condIds...))
|
stmt := tblMedias.DELETE().WHERE(tblMedias.ID.IN(condIds...))
|
||||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
m.log().WithField("func", "BatchDelete").Errorf("error deleting Medias items: %v", err)
|
m.log().WithField("func", "BatchDelete").Errorf("error deleting Medias items: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log().WithField("func", "BatchDelete").Infof("Medias items deleted successfully")
|
m.log().WithField("func", "BatchDelete").WithField("ids", ids).Infof("Medias items deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (m *Medias) Update(ctx context.Context) error {
|
func (m *Medias) Update(ctx context.Context) error {
|
||||||
//
|
stmt := tblMedias.UPDATE(tblMediasUpdateMutableColumns).SET(m).WHERE(tblMedias.ID.EQ(Int(m.ID))).RETURNING(tblMedias.AllColumns)
|
||||||
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||||
|
|
||||||
// stmt := table.Medias.UPDATE(table.Medias.MutableColumns.Except(mediasUpdateExcludeColumns...)).SET(m).WHERE(table.Medias.ID.EQ(Int(m.ID))).RETURNING(table.Medias.AllColumns)
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
// m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
m.log().WithField("func", "Update").Errorf("error updating Medias item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// if err := stmt.QueryContext(ctx, db, m); err != nil {
|
m.log().WithField("func", "Update").Infof("Medias item updated successfully")
|
||||||
// m.log().WithField("func","Update").Errorf("error updating Medias item: %v", err)
|
return nil
|
||||||
// return err
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// m.log().WithField("func", "Update").Infof("Medias item updated successfully")
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// GetByCond
|
// GetByCond
|
||||||
func (m *Medias) GetByCond(ctx context.Context, conds ...Cond) (*Medias, error) {
|
func (m *Medias) GetByCond(ctx context.Context, conds ...Cond) (*Medias, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
stmt := table.Medias.SELECT(table.Medias.AllColumns).WHERE(cond)
|
stmt := tblMedias.SELECT(tblMedias.AllColumns).WHERE(cond)
|
||||||
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -122,7 +119,7 @@ func (m *Medias) GetByID(ctx context.Context, id int64, conds ...Cond) (*Medias,
|
|||||||
func (m *Medias) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
func (m *Medias) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := tblMedias
|
||||||
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
|
|||||||
@@ -5,19 +5,18 @@ import (
|
|||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mediasUpdateExcludeColumns = []Column{
|
var tblMediasUpdateMutableColumns = tblMedias.MutableColumns.Except(
|
||||||
table.Medias.CreatedAt,
|
tblMedias.CreatedAt,
|
||||||
}
|
)
|
||||||
|
|
||||||
func (m *Medias) Like(key *string) Cond {
|
func (m *Medias) Like(key *string) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
tbl := table.Medias
|
tbl := tblMedias
|
||||||
if key == nil || *key == "" {
|
if key == nil || *key == "" {
|
||||||
return cond
|
return cond
|
||||||
}
|
}
|
||||||
@@ -33,7 +32,7 @@ func (m *Medias) Like(key *string) Cond {
|
|||||||
func (m *Medias) List(ctx context.Context, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
func (m *Medias) List(ctx context.Context, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := tblMedias
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(CondTrue(conds...)).
|
WHERE(CondTrue(conds...)).
|
||||||
@@ -72,7 +71,7 @@ func (m *Medias) GetByIds(ctx context.Context, ids []int64) ([]*Medias, error) {
|
|||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := tblMedias
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(tbl.ID.IN(condIds...))
|
WHERE(tbl.ID.IN(condIds...))
|
||||||
@@ -92,10 +91,9 @@ func (m *Medias) GetByIds(ctx context.Context, ids []int64) ([]*Medias, error) {
|
|||||||
|
|
||||||
// GetByHash
|
// GetByHash
|
||||||
func (m *Medias) GetByHash(ctx context.Context, hash string) (*Medias, error) {
|
func (m *Medias) GetByHash(ctx context.Context, hash string) (*Medias, error) {
|
||||||
tbl := table.Medias
|
stmt := tblMedias.
|
||||||
stmt := tbl.
|
SELECT(tblMedias.AllColumns).
|
||||||
SELECT(tbl.AllColumns).
|
WHERE(tblMedias.Hash.EQ(String(hash)))
|
||||||
WHERE(tbl.Hash.EQ(String(hash)))
|
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
var media Medias
|
var media Medias
|
||||||
@@ -112,11 +110,10 @@ func (m *Medias) GetByHash(ctx context.Context, hash string) (*Medias, error) {
|
|||||||
func (m *Medias) UpdateMetas(ctx context.Context, id int64, metas fields.MediaMetas) error {
|
func (m *Medias) UpdateMetas(ctx context.Context, id int64, metas fields.MediaMetas) error {
|
||||||
meta := fields.ToJson(metas)
|
meta := fields.ToJson(metas)
|
||||||
|
|
||||||
tbl := table.Medias
|
stmt := tblMedias.
|
||||||
stmt := tbl.
|
UPDATE(tblMedias.Metas).
|
||||||
UPDATE(tbl.Metas).
|
|
||||||
SET(meta).
|
SET(meta).
|
||||||
WHERE(tbl.ID.EQ(Int64(id)))
|
WHERE(tblMedias.ID.EQ(Int64(id)))
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -130,9 +127,8 @@ func (m *Medias) UpdateMetas(ctx context.Context, id int64, metas fields.MediaMe
|
|||||||
|
|
||||||
// GetRelationMedias
|
// GetRelationMedias
|
||||||
func (m *Medias) GetRelations(ctx context.Context, hash string) ([]*Medias, error) {
|
func (m *Medias) GetRelations(ctx context.Context, hash string) ([]*Medias, error) {
|
||||||
tbl := table.Medias
|
stmt := tblMedias.
|
||||||
stmt := tbl.
|
SELECT(tblMedias.AllColumns).
|
||||||
SELECT(tbl.AllColumns).
|
|
||||||
WHERE(
|
WHERE(
|
||||||
RawBool("metas->>'parent_hash' = ?", RawArgs{"?": hash}),
|
RawBool("metas->>'parent_hash' = ?", RawArgs{"?": hash}),
|
||||||
)
|
)
|
||||||
@@ -148,16 +144,3 @@ func (m *Medias) GetRelations(ctx context.Context, hash string) ([]*Medias, erro
|
|||||||
return &media
|
return &media
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Medias) Update(ctx context.Context) error {
|
|
||||||
stmt := table.Medias.UPDATE(table.Medias.MutableColumns.Except(table.Medias.CreatedAt)).SET(m).WHERE(table.Medias.ID.EQ(Int(m.ID))).RETURNING(table.Medias.AllColumns)
|
|
||||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
||||||
m.log().WithField("func", "Update").Errorf("error updating Medias item: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m.log().WithField("func", "Update").Infof("Medias item updated successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
"quyun/app/service/testx"
|
"quyun/app/service/testx"
|
||||||
"quyun/database"
|
"quyun/database"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
"go.ipao.vip/atom/contracts"
|
"go.ipao.vip/atom/contracts"
|
||||||
@@ -43,14 +42,14 @@ func Test_Medias(t *testing.T) {
|
|||||||
|
|
||||||
func (s *MediasTestSuite) Test_Demo() {
|
func (s *MediasTestSuite) Test_Demo() {
|
||||||
Convey("Test_Demo", s.T(), func() {
|
Convey("Test_Demo", s.T(), func() {
|
||||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
database.Truncate(context.Background(), db, tblMedias.TableName())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MediasTestSuite) Test_countByCondition() {
|
func (s *MediasTestSuite) Test_countByCondition() {
|
||||||
Convey("countByCondition", s.T(), func() {
|
Convey("countByCondition", s.T(), func() {
|
||||||
Convey("no cond", func() {
|
Convey("no cond", func() {
|
||||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
database.Truncate(context.Background(), db, tblMedias.TableName())
|
||||||
|
|
||||||
cnt, err := MediasModel().Count(context.Background())
|
cnt, err := MediasModel().Count(context.Background())
|
||||||
Convey("should not return an error", func() {
|
Convey("should not return an error", func() {
|
||||||
@@ -68,7 +67,7 @@ func (s *MediasTestSuite) Test_countByCondition() {
|
|||||||
func (s *MediasTestSuite) Test_BatchCreate() {
|
func (s *MediasTestSuite) Test_BatchCreate() {
|
||||||
Convey("Create", s.T(), func() {
|
Convey("Create", s.T(), func() {
|
||||||
Convey("valid media", func() {
|
Convey("valid media", func() {
|
||||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
database.Truncate(context.Background(), db, tblMedias.TableName())
|
||||||
|
|
||||||
models := []*Medias{
|
models := []*Medias{
|
||||||
{
|
{
|
||||||
@@ -122,7 +121,7 @@ func (s *MediasTestSuite) Test_BatchCreate() {
|
|||||||
func (s *MediasTestSuite) Test_Create() {
|
func (s *MediasTestSuite) Test_Create() {
|
||||||
Convey("Create", s.T(), func() {
|
Convey("Create", s.T(), func() {
|
||||||
Convey("valid media", func() {
|
Convey("valid media", func() {
|
||||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
database.Truncate(context.Background(), db, tblMedias.TableName())
|
||||||
|
|
||||||
model := &Medias{
|
model := &Medias{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
@@ -154,7 +153,7 @@ func (s *MediasTestSuite) Test_Create() {
|
|||||||
func (s *MediasTestSuite) Test_Page() {
|
func (s *MediasTestSuite) Test_Page() {
|
||||||
Convey("Create", s.T(), func() {
|
Convey("Create", s.T(), func() {
|
||||||
Convey("Insert Items", func() {
|
Convey("Insert Items", func() {
|
||||||
database.Truncate(context.Background(), db, table.Medias.TableName())
|
database.Truncate(context.Background(), db, tblMedias.TableName())
|
||||||
|
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
model := &Medias{
|
model := &Medias{
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -15,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func (m *Orders) CondID(id int64) Cond {
|
func (m *Orders) CondID(id int64) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Orders.ID.EQ(Int(id)))
|
return cond.AND(tblOrders.ID.EQ(Int(id)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +26,7 @@ func (m *Orders) Create(ctx context.Context) error {
|
|||||||
m.CreatedAt = time.Now()
|
m.CreatedAt = time.Now()
|
||||||
m.UpdatedAt = time.Now()
|
m.UpdatedAt = time.Now()
|
||||||
|
|
||||||
stmt := table.Medias.INSERT(table.Orders.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
stmt := tblMedias.INSERT(tblOrders.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
|
||||||
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -41,7 +39,7 @@ func (m *Orders) Create(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Orders) BatchCreate(ctx context.Context, models []*Orders) error {
|
func (m *Orders) BatchCreate(ctx context.Context, models []*Orders) error {
|
||||||
stmt := table.Orders.INSERT(table.Orders.MutableColumns).MODELS(models)
|
stmt := tblOrders.INSERT(tblOrders.MutableColumns).MODELS(models)
|
||||||
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -53,11 +51,11 @@ func (m *Orders) BatchCreate(ctx context.Context, models []*Orders) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Orders) ForceDelete(ctx context.Context) error {
|
func (m *Orders) Delete(ctx context.Context) error {
|
||||||
stmt := table.Orders.DELETE().WHERE(table.Orders.ID.EQ(Int(m.ID)))
|
stmt := tblOrders.DELETE().WHERE(tblOrders.ID.EQ(Int(m.ID)))
|
||||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
m.log().WithField("func", "Delete").Errorf("error deleting Orders item: %v", err)
|
m.log().WithField("func", "Delete").Errorf("error deleting Orders item: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -66,45 +64,47 @@ func (m *Orders) ForceDelete(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Orders) BatchForceDelete(ctx context.Context, ids []int64) error {
|
// BatchDelete
|
||||||
|
func (m *Orders) BatchDelete(ctx context.Context, ids []int64) error {
|
||||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
stmt := table.Orders.DELETE().WHERE(table.Orders.ID.IN(condIds...))
|
stmt := tblOrders.DELETE().WHERE(tblOrders.ID.IN(condIds...))
|
||||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
m.log().WithField("func", "BatchDelete").Errorf("error deleting Orders items: %v", err)
|
m.log().WithField("func", "BatchDelete").Errorf("error deleting Orders items: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log().WithField("func", "BatchDelete").Infof("Orders items deleted successfully")
|
m.log().WithField("func", "BatchDelete").WithField("ids", ids).Infof("Orders items deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (m *Orders) Update(ctx context.Context) error {
|
func (m *Orders) Update(ctx context.Context) error {
|
||||||
//
|
m.UpdatedAt = time.Now()
|
||||||
// m.UpdatedAt = time.Now()
|
|
||||||
//
|
|
||||||
|
|
||||||
// stmt := table.Orders.UPDATE(table.Orders.MutableColumns.Except(ordersUpdateExcludeColumns...)).SET(m).WHERE(table.Orders.ID.EQ(Int(m.ID))).RETURNING(table.Orders.AllColumns)
|
stmt := tblOrders.UPDATE(tblOrdersUpdateMutableColumns).
|
||||||
// m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
SET(m).
|
||||||
|
WHERE(tblOrders.ID.EQ(Int(m.ID))).
|
||||||
|
RETURNING(tblOrders.AllColumns)
|
||||||
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||||
|
|
||||||
// if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
// m.log().WithField("func","Update").Errorf("error updating Orders item: %v", err)
|
m.log().WithField("func", "Update").Errorf("error updating Orders item: %v", err)
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
|
|
||||||
// m.log().WithField("func", "Update").Infof("Orders item updated successfully")
|
m.log().WithField("func", "Update").Infof("Orders item updated successfully")
|
||||||
// return nil
|
return nil
|
||||||
// }
|
}
|
||||||
|
|
||||||
// GetByCond
|
// GetByCond
|
||||||
func (m *Orders) GetByCond(ctx context.Context, conds ...Cond) (*Orders, error) {
|
func (m *Orders) GetByCond(ctx context.Context, conds ...Cond) (*Orders, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
stmt := table.Orders.SELECT(table.Orders.AllColumns).WHERE(cond)
|
stmt := tblOrders.SELECT(tblOrders.AllColumns).WHERE(cond)
|
||||||
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -125,7 +125,7 @@ func (m *Orders) GetByID(ctx context.Context, id int64, conds ...Cond) (*Orders,
|
|||||||
func (m *Orders) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
func (m *Orders) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
|
|||||||
@@ -2,45 +2,28 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ordersUpdateExcludeColumns = []Column{
|
var tblOrdersUpdateMutableColumns = tblOrders.MutableColumns.Except(
|
||||||
table.Orders.OrderNo,
|
tblOrders.OrderNo,
|
||||||
table.Orders.Price,
|
tblOrders.Price,
|
||||||
table.Orders.Discount,
|
tblOrders.Discount,
|
||||||
table.Orders.SubOrderNo,
|
tblOrders.SubOrderNo,
|
||||||
table.Orders.PostID,
|
tblOrders.PostID,
|
||||||
table.Orders.UserID,
|
tblOrders.UserID,
|
||||||
}
|
)
|
||||||
|
|
||||||
func (m *Orders) Update(ctx context.Context) error {
|
|
||||||
m.UpdatedAt = time.Now()
|
|
||||||
|
|
||||||
stmt := table.Orders.UPDATE(table.Orders.MutableColumns.Except(table.Orders.OrderNo, table.Orders.Price, table.Orders.Discount, table.Orders.SubOrderNo, table.Orders.PostID, table.Orders.UserID)).SET(m).WHERE(table.Orders.ID.EQ(Int(m.ID))).RETURNING(table.Orders.AllColumns)
|
|
||||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
||||||
m.log().WithField("func", "Update").Errorf("error updating Orders item: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m.log().WithField("func", "Update").Infof("Orders item updated successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BuildConditionWithKey builds the WHERE clause for order queries
|
// BuildConditionWithKey builds the WHERE clause for order queries
|
||||||
func (m *Orders) BuildConditionWithKey(orderNumber *string, userID *int64) BoolExpression {
|
func (m *Orders) BuildConditionWithKey(orderNumber *string, userID *int64) BoolExpression {
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
|
|
||||||
cond := Bool(true)
|
cond := Bool(true)
|
||||||
|
|
||||||
@@ -65,7 +48,7 @@ func (m *Orders) countByCondition(ctx context.Context, expr BoolExpression) (int
|
|||||||
Cnt int64
|
Cnt int64
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
@@ -79,10 +62,14 @@ func (m *Orders) countByCondition(ctx context.Context, expr BoolExpression) (int
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List returns a paginated list of orders
|
// List returns a paginated list of orders
|
||||||
func (m *Orders) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
func (m *Orders) List(
|
||||||
|
ctx context.Context,
|
||||||
|
pagination *requests.Pagination,
|
||||||
|
cond BoolExpression,
|
||||||
|
) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(cond).
|
WHERE(cond).
|
||||||
@@ -158,7 +145,7 @@ func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64)
|
|||||||
m.CreatedAt = time.Now()
|
m.CreatedAt = time.Now()
|
||||||
m.UpdatedAt = time.Now()
|
m.UpdatedAt = time.Now()
|
||||||
m.Status = fields.OrderStatusPending
|
m.Status = fields.OrderStatusPending
|
||||||
m.OrderNo = fmt.Sprintf("%s", time.Now().Format("20060102150405"))
|
m.OrderNo = time.Now().Format("20060102150405")
|
||||||
m.SubOrderNo = m.OrderNo
|
m.SubOrderNo = m.OrderNo
|
||||||
m.UserID = userId
|
m.UserID = userId
|
||||||
m.PostID = postId
|
m.PostID = postId
|
||||||
@@ -166,7 +153,7 @@ func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64)
|
|||||||
m.Price = post.Price
|
m.Price = post.Price
|
||||||
m.Discount = post.Discount
|
m.Discount = post.Discount
|
||||||
|
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
||||||
o.log().Infof("sql: %s", stmt.DebugSql())
|
o.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
@@ -178,7 +165,7 @@ func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Orders) SetMeta(ctx context.Context, metaFunc func(fields.OrderMeta) fields.OrderMeta) error {
|
func (m *Orders) SetMeta(ctx context.Context, metaFunc func(fields.OrderMeta) fields.OrderMeta) error {
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.Meta).
|
UPDATE(tbl.Meta).
|
||||||
SET(fields.ToJson(metaFunc(m.Meta.Data))).
|
SET(fields.ToJson(metaFunc(m.Meta.Data))).
|
||||||
@@ -196,7 +183,7 @@ func (m *Orders) SetMeta(ctx context.Context, metaFunc func(fields.OrderMeta) fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Orders) SetStatus(ctx context.Context, status fields.OrderStatus) error {
|
func (m *Orders) SetStatus(ctx context.Context, status fields.OrderStatus) error {
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.Status).
|
UPDATE(tbl.Status).
|
||||||
SET(status).
|
SET(status).
|
||||||
@@ -215,7 +202,7 @@ func (m *Orders) SetStatus(ctx context.Context, status fields.OrderStatus) error
|
|||||||
|
|
||||||
// SumAmount
|
// SumAmount
|
||||||
func (m *Orders) SumAmount(ctx context.Context) (int64, error) {
|
func (m *Orders) SumAmount(ctx context.Context) (int64, error) {
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := SELECT(SUM(tbl.Price).AS("cnt")).FROM(tbl).WHERE(
|
stmt := SELECT(SUM(tbl.Price).AS("cnt")).FROM(tbl).WHERE(
|
||||||
tbl.Status.EQ(Int(int64(fields.OrderStatusCompleted))),
|
tbl.Status.EQ(Int(int64(fields.OrderStatusCompleted))),
|
||||||
)
|
)
|
||||||
@@ -236,7 +223,7 @@ func (m *Orders) SumAmount(ctx context.Context) (int64, error) {
|
|||||||
|
|
||||||
// GetByOrderNo
|
// GetByOrderNo
|
||||||
func (m *Orders) GetByOrderNo(ctx context.Context, orderNo string) (*Orders, error) {
|
func (m *Orders) GetByOrderNo(ctx context.Context, orderNo string) (*Orders, error) {
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(
|
WHERE(
|
||||||
@@ -256,7 +243,7 @@ func (m *Orders) GetByOrderNo(ctx context.Context, orderNo string) (*Orders, err
|
|||||||
|
|
||||||
// SetTransactionID
|
// SetTransactionID
|
||||||
func (m *Orders) SetTransactionID(ctx context.Context, transactionID string) error {
|
func (m *Orders) SetTransactionID(ctx context.Context, transactionID string) error {
|
||||||
tbl := table.Orders
|
tbl := tblOrders
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.TransactionID).
|
UPDATE(tbl.TransactionID).
|
||||||
SET(String(transactionID)).
|
SET(String(transactionID)).
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"quyun/app/service/testx"
|
"quyun/app/service/testx"
|
||||||
"quyun/database"
|
"quyun/database"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
"go.ipao.vip/atom/contracts"
|
"go.ipao.vip/atom/contracts"
|
||||||
@@ -38,6 +37,6 @@ func Test_Orders(t *testing.T) {
|
|||||||
|
|
||||||
func (s *OrdersTestSuite) Test_Demo() {
|
func (s *OrdersTestSuite) Test_Demo() {
|
||||||
Convey("Test_Demo", s.T(), func() {
|
Convey("Test_Demo", s.T(), func() {
|
||||||
database.Truncate(context.Background(), db, table.Orders.TableName())
|
database.Truncate(context.Background(), db, tblOrders.TableName())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -14,13 +12,19 @@ import (
|
|||||||
// conds
|
// conds
|
||||||
func (m *Posts) CondNotDeleted() Cond {
|
func (m *Posts) CondNotDeleted() Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Posts.DeletedAt.IS_NULL())
|
return cond.AND(tblPosts.DeletedAt.IS_NULL())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Posts) CondDeleted() Cond {
|
||||||
|
return func(cond BoolExpression) BoolExpression {
|
||||||
|
return cond.AND(tblPosts.DeletedAt.IS_NOT_NULL())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Posts) CondID(id int64) Cond {
|
func (m *Posts) CondID(id int64) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Posts.ID.EQ(Int(id)))
|
return cond.AND(tblPosts.ID.EQ(Int(id)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +37,7 @@ func (m *Posts) Create(ctx context.Context) error {
|
|||||||
m.CreatedAt = time.Now()
|
m.CreatedAt = time.Now()
|
||||||
m.UpdatedAt = time.Now()
|
m.UpdatedAt = time.Now()
|
||||||
|
|
||||||
stmt := table.Medias.INSERT(table.Posts.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
stmt := tblMedias.INSERT(tblPosts.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
|
||||||
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -46,7 +50,7 @@ func (m *Posts) Create(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Posts) BatchCreate(ctx context.Context, models []*Posts) error {
|
func (m *Posts) BatchCreate(ctx context.Context, models []*Posts) error {
|
||||||
stmt := table.Posts.INSERT(table.Posts.MutableColumns).MODELS(models)
|
stmt := tblPosts.INSERT(tblPosts.MutableColumns).MODELS(models)
|
||||||
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -59,7 +63,7 @@ func (m *Posts) BatchCreate(ctx context.Context, models []*Posts) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Posts) Delete(ctx context.Context) error {
|
func (m *Posts) Delete(ctx context.Context) error {
|
||||||
stmt := table.Posts.UPDATE().SET(table.Posts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Posts.ID.EQ(Int(m.ID)))
|
stmt := tblPosts.UPDATE().SET(tblPosts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblPosts.ID.EQ(Int(m.ID)))
|
||||||
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -77,7 +81,7 @@ func (m *Posts) BatchDelete(ctx context.Context, ids []int64) error {
|
|||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
stmt := table.Posts.UPDATE().SET(table.Posts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Posts.ID.IN(condIds...))
|
stmt := tblPosts.UPDATE().SET(tblPosts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblPosts.ID.IN(condIds...))
|
||||||
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -85,12 +89,12 @@ func (m *Posts) BatchDelete(ctx context.Context, ids []int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log().WithField("func", "BatchSoftDelete").Infof("Posts items soft deleted successfully")
|
m.log().WithField("func", "BatchSoftDelete").WithField("ids", ids).Infof("Posts items soft deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Posts) ForceDelete(ctx context.Context) error {
|
func (m *Posts) ForceDelete(ctx context.Context) error {
|
||||||
stmt := table.Posts.DELETE().WHERE(table.Posts.ID.EQ(Int(m.ID)))
|
stmt := tblPosts.DELETE().WHERE(tblPosts.ID.EQ(Int(m.ID)))
|
||||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -107,40 +111,38 @@ func (m *Posts) BatchForceDelete(ctx context.Context, ids []int64) error {
|
|||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
stmt := table.Posts.DELETE().WHERE(table.Posts.ID.IN(condIds...))
|
stmt := tblPosts.DELETE().WHERE(tblPosts.ID.IN(condIds...))
|
||||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
m.log().WithField("func", "BatchDelete").Errorf("error deleting Posts items: %v", err)
|
m.log().WithField("func", "BatchForceDelete").Errorf("error deleting Posts items: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log().WithField("func", "BatchDelete").Infof("Posts items deleted successfully")
|
m.log().WithField("func", "BatchForceDelete").WithField("ids", ids).Infof("Posts items deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (m *Posts) Update(ctx context.Context) error {
|
func (m *Posts) Update(ctx context.Context) error {
|
||||||
//
|
m.UpdatedAt = time.Now()
|
||||||
// m.UpdatedAt = time.Now()
|
|
||||||
//
|
|
||||||
|
|
||||||
// stmt := table.Posts.UPDATE(table.Posts.MutableColumns.Except(postsUpdateExcludeColumns...)).SET(m).WHERE(table.Posts.ID.EQ(Int(m.ID))).RETURNING(table.Posts.AllColumns)
|
stmt := tblPosts.UPDATE(tblPostsUpdateMutableColumns).SET(m).WHERE(tblPosts.ID.EQ(Int(m.ID))).RETURNING(tblPosts.AllColumns)
|
||||||
// m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||||
|
|
||||||
// if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
// m.log().WithField("func","Update").Errorf("error updating Posts item: %v", err)
|
m.log().WithField("func", "Update").Errorf("error updating Posts item: %v", err)
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
|
|
||||||
// m.log().WithField("func", "Update").Infof("Posts item updated successfully")
|
m.log().WithField("func", "Update").Infof("Posts item updated successfully")
|
||||||
// return nil
|
return nil
|
||||||
// }
|
}
|
||||||
|
|
||||||
// GetByCond
|
// GetByCond
|
||||||
func (m *Posts) GetByCond(ctx context.Context, conds ...Cond) (*Posts, error) {
|
func (m *Posts) GetByCond(ctx context.Context, conds ...Cond) (*Posts, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
stmt := table.Posts.SELECT(table.Posts.AllColumns).WHERE(cond)
|
stmt := tblPosts.SELECT(tblPosts.AllColumns).WHERE(cond)
|
||||||
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -161,7 +163,7 @@ func (m *Posts) GetByID(ctx context.Context, id int64, conds ...Cond) (*Posts, e
|
|||||||
func (m *Posts) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
func (m *Posts) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
tbl := table.Posts
|
tbl := tblPosts
|
||||||
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
|
|||||||
@@ -6,43 +6,27 @@ import (
|
|||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var postsUpdateExcludeColumns = []Column{
|
var tblPostsUpdateMutableColumns = tblPosts.MutableColumns.Except(
|
||||||
table.Posts.CreatedAt,
|
tblPosts.CreatedAt,
|
||||||
table.Posts.DeletedAt,
|
tblPosts.DeletedAt,
|
||||||
table.Posts.Views,
|
tblPosts.Views,
|
||||||
table.Posts.Likes,
|
tblPosts.Likes,
|
||||||
}
|
)
|
||||||
|
|
||||||
func (m *Posts) Update(ctx context.Context) error {
|
|
||||||
m.UpdatedAt = time.Now()
|
|
||||||
|
|
||||||
stmt := table.Posts.UPDATE(table.Posts.MutableColumns.Except(table.Posts.CreatedAt, table.Posts.DeletedAt, table.Posts.Views, table.Posts.Likes)).SET(m).WHERE(table.Posts.ID.EQ(Int(m.ID))).RETURNING(table.Posts.AllColumns)
|
|
||||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
||||||
m.log().WithField("func", "Update").Errorf("error updating Posts item: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m.log().WithField("func", "Update").Infof("Posts item updated successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Posts) CondStatus(s fields.PostStatus) Cond {
|
func (m *Posts) CondStatus(s fields.PostStatus) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Posts.Status.EQ(Int(int64(s))))
|
return cond.AND(tblPosts.Status.EQ(Int(int64(s))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Posts) CondLike(key *string) Cond {
|
func (m *Posts) CondLike(key *string) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
tbl := table.Posts
|
tbl := tblPosts
|
||||||
if key == nil || *key == "" {
|
if key == nil || *key == "" {
|
||||||
return cond
|
return cond
|
||||||
}
|
}
|
||||||
@@ -62,7 +46,7 @@ func (m *Posts) CondLike(key *string) Cond {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Posts) IncrViewCount(ctx context.Context) error {
|
func (m *Posts) IncrViewCount(ctx context.Context) error {
|
||||||
tbl := table.Posts
|
tbl := tblPosts
|
||||||
|
|
||||||
stmt := tbl.UPDATE(tbl.Views).SET(tbl.Views.ADD(Int64(1))).WHERE(tbl.ID.EQ(Int64(m.ID)))
|
stmt := tbl.UPDATE(tbl.Views).SET(tbl.Views.ADD(Int64(1))).WHERE(tbl.ID.EQ(Int64(m.ID)))
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
@@ -76,31 +60,12 @@ func (m *Posts) IncrViewCount(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// countByCond
|
|
||||||
func (m *Posts) 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 *Posts) List(ctx context.Context, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
cond := CondJoin(m.CondNotDeleted(), conds...)
|
cond := CondJoin(m.CondNotDeleted(), conds...)
|
||||||
|
|
||||||
tbl := table.Posts
|
tbl := tblPosts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(CondTrue(cond...)).
|
WHERE(CondTrue(cond...)).
|
||||||
@@ -132,7 +97,7 @@ func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, conds
|
|||||||
// SendTo
|
// SendTo
|
||||||
func (m *Posts) SendTo(ctx context.Context, userId int64) error {
|
func (m *Posts) SendTo(ctx context.Context, userId int64) error {
|
||||||
// add record to user_posts
|
// add record to user_posts
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(UserPosts{
|
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(UserPosts{
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
@@ -150,7 +115,7 @@ func (m *Posts) SendTo(ctx context.Context, userId int64) error {
|
|||||||
|
|
||||||
// PostBoughtStatistics 获取指定文件 ID 的购买次数
|
// PostBoughtStatistics 获取指定文件 ID 的购买次数
|
||||||
func (m *Posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int64]int64, error) {
|
func (m *Posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int64]int64, error) {
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
|
|
||||||
// select count(user_id), post_id from user_posts up where post_id in (1, 2,3,4,5,6,7,8,9,10) group by post_id
|
// select count(user_id), post_id from user_posts up where post_id in (1, 2,3,4,5,6,7,8,9,10) group by post_id
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
@@ -190,15 +155,15 @@ func (m *Posts) Bought(ctx context.Context, userId int64, pagination *requests.P
|
|||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
// select up.price,up.created_at,p.* from user_posts up left join posts p on up.post_id = p.id where up.user_id =1
|
// select up.price,up.created_at,p.* from user_posts up left join posts p on up.post_id = p.id where up.user_id =1
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(
|
SELECT(
|
||||||
tbl.Price.AS("price"),
|
tbl.Price.AS("price"),
|
||||||
tbl.CreatedAt.AS("bought_at"),
|
tbl.CreatedAt.AS("bought_at"),
|
||||||
table.Posts.Title.AS("title"),
|
tblPosts.Title.AS("title"),
|
||||||
).
|
).
|
||||||
FROM(
|
FROM(
|
||||||
tbl.INNER_JOIN(table.Posts, table.Posts.ID.EQ(tbl.PostID)),
|
tbl.INNER_JOIN(tblPosts, tblPosts.ID.EQ(tbl.PostID)),
|
||||||
).
|
).
|
||||||
WHERE(
|
WHERE(
|
||||||
tbl.UserID.EQ(Int64(userId)),
|
tbl.UserID.EQ(Int64(userId)),
|
||||||
@@ -248,7 +213,7 @@ func (m *Posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]Po
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl := table.Posts
|
tbl := tblPosts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(
|
WHERE(
|
||||||
@@ -275,7 +240,7 @@ func (m *Posts) GetMediaByIds(ctx context.Context, ids []int64) ([]Medias, error
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := tblMedias
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(
|
WHERE(
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"quyun/app/service/testx"
|
"quyun/app/service/testx"
|
||||||
"quyun/database"
|
"quyun/database"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
"go.ipao.vip/atom/contracts"
|
"go.ipao.vip/atom/contracts"
|
||||||
@@ -39,14 +38,14 @@ func Test_Posts(t *testing.T) {
|
|||||||
|
|
||||||
func (s *PostsTestSuite) Test_Demo() {
|
func (s *PostsTestSuite) Test_Demo() {
|
||||||
Convey("Test_Demo", s.T(), func() {
|
Convey("Test_Demo", s.T(), func() {
|
||||||
database.Truncate(context.Background(), db, table.Posts.TableName())
|
database.Truncate(context.Background(), db, tblPosts.TableName())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test_Create
|
// Test_Create
|
||||||
func (s *PostsTestSuite) Test_Create() {
|
func (s *PostsTestSuite) Test_Create() {
|
||||||
Convey("Test_Create", s.T(), func() {
|
Convey("Test_Create", s.T(), func() {
|
||||||
// database.Truncate(context.Background(), db, table.Posts.TableName())
|
// database.Truncate(context.Background(), db, tblPosts.TableName())
|
||||||
|
|
||||||
post := &Posts{
|
post := &Posts{
|
||||||
Title: "Test Post",
|
Title: "Test Post",
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"quyun/database/table"
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"go.ipao.vip/atom"
|
"go.ipao.vip/atom"
|
||||||
"go.ipao.vip/atom/container"
|
"go.ipao.vip/atom/container"
|
||||||
@@ -34,12 +36,21 @@ func CondJoin(cond Cond, conds ...Cond) []Cond {
|
|||||||
return append([]Cond{cond}, conds...)
|
return append([]Cond{cond}, conds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tables
|
||||||
|
var tblMedias = table.Medias
|
||||||
|
var tblOrders = table.Orders
|
||||||
|
var tblPosts = table.Posts
|
||||||
|
var tblUserPosts = table.UserPosts
|
||||||
|
var tblUsers = table.Users
|
||||||
|
|
||||||
|
// models
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
|
||||||
func MediasModel() *Medias { return &Medias{} }
|
func MediasModel() *Medias { return &Medias{} }
|
||||||
func OrdersModel() *Orders { return &Orders{} }
|
func OrdersModel() *Orders { return &Orders{} }
|
||||||
func PostsModel() *Posts { return &Posts{} }
|
func PostsModel() *Posts { return &Posts{} }
|
||||||
func UsersModel() *Users { return &Users{} }
|
func UserPostsModel() *UserPosts { return &UserPosts{} }
|
||||||
|
func UsersModel() *Users { return &Users{} }
|
||||||
|
|
||||||
func Transaction(ctx context.Context) (*sql.Tx, error) { return db.Begin() }
|
func Transaction(ctx context.Context) (*sql.Tx, error) { return db.Begin() }
|
||||||
|
|
||||||
|
|||||||
139
backend/app/model/user_posts.funcs.gen.go
Normal file
139
backend/app/model/user_posts.funcs.gen.go
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
"github.com/samber/lo"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// conds
|
||||||
|
|
||||||
|
func (m *UserPosts) CondID(id int64) Cond {
|
||||||
|
return func(cond BoolExpression) BoolExpression {
|
||||||
|
return cond.AND(tblUserPosts.ID.EQ(Int(id)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// funcs
|
||||||
|
func (m *UserPosts) log() *log.Entry {
|
||||||
|
return log.WithField("model", "UserPosts")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UserPosts) Create(ctx context.Context) error {
|
||||||
|
m.CreatedAt = time.Now()
|
||||||
|
m.UpdatedAt = time.Now()
|
||||||
|
|
||||||
|
stmt := tblMedias.INSERT(tblUserPosts.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
|
||||||
|
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func", "Create").Errorf("error creating UserPosts item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "Create").Infof("UserPosts item created successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UserPosts) BatchCreate(ctx context.Context, models []*UserPosts) error {
|
||||||
|
stmt := tblUserPosts.INSERT(tblUserPosts.MutableColumns).MODELS(models)
|
||||||
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
|
m.log().WithField("func", "Create").Errorf("error creating UserPosts item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "BatchCreate").Infof("UserPosts items created successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UserPosts) Delete(ctx context.Context) error {
|
||||||
|
stmt := tblUserPosts.DELETE().WHERE(tblUserPosts.ID.EQ(Int(m.ID)))
|
||||||
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func", "Delete").Errorf("error deleting UserPosts item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "Delete").Infof("UserPosts item deleted successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchDelete
|
||||||
|
func (m *UserPosts) BatchDelete(ctx context.Context, ids []int64) error {
|
||||||
|
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||||
|
return Int64(id)
|
||||||
|
})
|
||||||
|
|
||||||
|
stmt := tblUserPosts.DELETE().WHERE(tblUserPosts.ID.IN(condIds...))
|
||||||
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func", "BatchDelete").Errorf("error deleting UserPosts items: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "BatchDelete").WithField("ids", ids).Infof("UserPosts items deleted successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *UserPosts) Update(ctx context.Context) error {
|
||||||
|
m.UpdatedAt = time.Now()
|
||||||
|
|
||||||
|
stmt := tblUserPosts.UPDATE(tblUserPostsUpdateMutableColumns).SET(m).WHERE(tblUserPosts.ID.EQ(Int(m.ID))).RETURNING(tblUserPosts.AllColumns)
|
||||||
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func", "Update").Errorf("error updating UserPosts item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "Update").Infof("UserPosts item updated successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByCond
|
||||||
|
func (m *UserPosts) GetByCond(ctx context.Context, conds ...Cond) (*UserPosts, error) {
|
||||||
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
|
stmt := tblUserPosts.SELECT(tblUserPosts.AllColumns).WHERE(cond)
|
||||||
|
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func", "GetByCond").Errorf("error getting UserPosts item by ID: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "GetByCond").Infof("UserPosts item retrieved successfully")
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByID
|
||||||
|
func (m *UserPosts) GetByID(ctx context.Context, id int64, conds ...Cond) (*UserPosts, error) {
|
||||||
|
return m.GetByCond(ctx, CondJoin(m.CondID(id), conds...)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count
|
||||||
|
func (m *UserPosts) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
||||||
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
|
tbl := tblUserPosts
|
||||||
|
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||||
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
|
var count struct {
|
||||||
|
Count int64
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, &count); err != nil {
|
||||||
|
m.log().Errorf("error counting UserPosts items: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return count.Count, nil
|
||||||
|
}
|
||||||
10
backend/app/model/user_posts.go
Normal file
10
backend/app/model/user_posts.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"quyun/database/table"
|
||||||
|
)
|
||||||
|
|
||||||
|
var tblUserPostsUpdateMutableColumns = tblUserPosts.MutableColumns.Except(
|
||||||
|
table.UserPosts.CreatedAt,
|
||||||
|
table.UserPosts.UpdatedAt,
|
||||||
|
)
|
||||||
43
backend/app/model/user_posts_test.go
Normal file
43
backend/app/model/user_posts_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"quyun/app/service/testx"
|
||||||
|
"quyun/database"
|
||||||
|
"quyun/database/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 UserPostsInjectParams struct {
|
||||||
|
dig.In
|
||||||
|
Initials []contracts.Initial `group:"initials"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserPostsTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
|
||||||
|
UserPostsInjectParams
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_UserPosts(t *testing.T) {
|
||||||
|
providers := testx.Default().With(Provide)
|
||||||
|
testx.Serve(providers, t, func(params UserPostsInjectParams) {
|
||||||
|
suite.Run(t, &UserPostsTestSuite{
|
||||||
|
UserPostsInjectParams: params,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserPostsTestSuite) Test_Demo() {
|
||||||
|
Convey("Test_Demo", s.T(), func() {
|
||||||
|
database.Truncate(context.Background(), db, table.UserPosts.TableName())
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -14,13 +12,19 @@ import (
|
|||||||
// conds
|
// conds
|
||||||
func (m *Users) CondNotDeleted() Cond {
|
func (m *Users) CondNotDeleted() Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Users.DeletedAt.IS_NULL())
|
return cond.AND(tblUsers.DeletedAt.IS_NULL())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Users) CondDeleted() Cond {
|
||||||
|
return func(cond BoolExpression) BoolExpression {
|
||||||
|
return cond.AND(tblUsers.DeletedAt.IS_NOT_NULL())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Users) CondID(id int64) Cond {
|
func (m *Users) CondID(id int64) Cond {
|
||||||
return func(cond BoolExpression) BoolExpression {
|
return func(cond BoolExpression) BoolExpression {
|
||||||
return cond.AND(table.Users.ID.EQ(Int(id)))
|
return cond.AND(tblUsers.ID.EQ(Int(id)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +37,7 @@ func (m *Users) Create(ctx context.Context) error {
|
|||||||
m.CreatedAt = time.Now()
|
m.CreatedAt = time.Now()
|
||||||
m.UpdatedAt = time.Now()
|
m.UpdatedAt = time.Now()
|
||||||
|
|
||||||
stmt := table.Medias.INSERT(table.Users.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
stmt := tblMedias.INSERT(tblUsers.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
|
||||||
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -46,7 +50,7 @@ func (m *Users) Create(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Users) BatchCreate(ctx context.Context, models []*Users) error {
|
func (m *Users) BatchCreate(ctx context.Context, models []*Users) error {
|
||||||
stmt := table.Users.INSERT(table.Users.MutableColumns).MODELS(models)
|
stmt := tblUsers.INSERT(tblUsers.MutableColumns).MODELS(models)
|
||||||
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -59,7 +63,7 @@ func (m *Users) BatchCreate(ctx context.Context, models []*Users) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Users) Delete(ctx context.Context) error {
|
func (m *Users) Delete(ctx context.Context) error {
|
||||||
stmt := table.Users.UPDATE().SET(table.Users.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Users.ID.EQ(Int(m.ID)))
|
stmt := tblUsers.UPDATE().SET(tblUsers.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblUsers.ID.EQ(Int(m.ID)))
|
||||||
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -77,7 +81,7 @@ func (m *Users) BatchDelete(ctx context.Context, ids []int64) error {
|
|||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
stmt := table.Users.UPDATE().SET(table.Users.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Users.ID.IN(condIds...))
|
stmt := tblUsers.UPDATE().SET(tblUsers.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblUsers.ID.IN(condIds...))
|
||||||
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -85,12 +89,12 @@ func (m *Users) BatchDelete(ctx context.Context, ids []int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log().WithField("func", "BatchSoftDelete").Infof("Users items soft deleted successfully")
|
m.log().WithField("func", "BatchSoftDelete").WithField("ids", ids).Infof("Users items soft deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Users) ForceDelete(ctx context.Context) error {
|
func (m *Users) ForceDelete(ctx context.Context) error {
|
||||||
stmt := table.Users.DELETE().WHERE(table.Users.ID.EQ(Int(m.ID)))
|
stmt := tblUsers.DELETE().WHERE(tblUsers.ID.EQ(Int(m.ID)))
|
||||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
@@ -107,40 +111,38 @@ func (m *Users) BatchForceDelete(ctx context.Context, ids []int64) error {
|
|||||||
return Int64(id)
|
return Int64(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
stmt := table.Users.DELETE().WHERE(table.Users.ID.IN(condIds...))
|
stmt := tblUsers.DELETE().WHERE(tblUsers.ID.IN(condIds...))
|
||||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
m.log().WithField("func", "BatchDelete").Errorf("error deleting Users items: %v", err)
|
m.log().WithField("func", "BatchForceDelete").Errorf("error deleting Users items: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log().WithField("func", "BatchDelete").Infof("Users items deleted successfully")
|
m.log().WithField("func", "BatchForceDelete").WithField("ids", ids).Infof("Users items deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (m *Users) Update(ctx context.Context) error {
|
func (m *Users) Update(ctx context.Context) error {
|
||||||
//
|
m.UpdatedAt = time.Now()
|
||||||
// m.UpdatedAt = time.Now()
|
|
||||||
//
|
|
||||||
|
|
||||||
// stmt := table.Users.UPDATE(table.Users.MutableColumns.Except(usersUpdateExcludeColumns...)).SET(m).WHERE(table.Users.ID.EQ(Int(m.ID))).RETURNING(table.Users.AllColumns)
|
stmt := tblUsers.UPDATE(tblUsersUpdateMutableColumns).SET(m).WHERE(tblUsers.ID.EQ(Int(m.ID))).RETURNING(tblUsers.AllColumns)
|
||||||
// m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||||
|
|
||||||
// if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
// m.log().WithField("func","Update").Errorf("error updating Users item: %v", err)
|
m.log().WithField("func", "Update").Errorf("error updating Users item: %v", err)
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
|
|
||||||
// m.log().WithField("func", "Update").Infof("Users item updated successfully")
|
m.log().WithField("func", "Update").Infof("Users item updated successfully")
|
||||||
// return nil
|
return nil
|
||||||
// }
|
}
|
||||||
|
|
||||||
// GetByCond
|
// GetByCond
|
||||||
func (m *Users) GetByCond(ctx context.Context, conds ...Cond) (*Users, error) {
|
func (m *Users) GetByCond(ctx context.Context, conds ...Cond) (*Users, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
stmt := table.Users.SELECT(table.Users.AllColumns).WHERE(cond)
|
stmt := tblUsers.SELECT(tblUsers.AllColumns).WHERE(cond)
|
||||||
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
@@ -161,7 +163,7 @@ func (m *Users) GetByID(ctx context.Context, id int64, conds ...Cond) (*Users, e
|
|||||||
func (m *Users) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
func (m *Users) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
||||||
cond := CondTrue(conds...)
|
cond := CondTrue(conds...)
|
||||||
|
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/table"
|
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/go-jet/jet/v2/qrm"
|
"github.com/go-jet/jet/v2/qrm"
|
||||||
@@ -14,31 +13,16 @@ import (
|
|||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var usersUpdateExcludeColumns = []Column{
|
var tblUsersUpdateMutableColumns = tblOrders.MutableColumns.Except(
|
||||||
table.Users.OpenID,
|
tblUsers.OpenID,
|
||||||
table.Users.Balance,
|
tblUsers.Balance,
|
||||||
table.Users.CreatedAt,
|
tblUsers.CreatedAt,
|
||||||
table.Users.DeletedAt,
|
tblUsers.DeletedAt,
|
||||||
}
|
)
|
||||||
|
|
||||||
func (m *Users) Update(ctx context.Context) error {
|
|
||||||
m.UpdatedAt = time.Now()
|
|
||||||
|
|
||||||
stmt := table.Users.UPDATE(table.Users.MutableColumns.Except(table.Users.OpenID, table.Users.Balance, table.Users.CreatedAt, table.Users.DeletedAt)).SET(m).WHERE(table.Users.ID.EQ(Int(m.ID))).RETURNING(table.Users.AllColumns)
|
|
||||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
|
||||||
|
|
||||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
||||||
m.log().WithField("func", "Update").Errorf("error updating Users item: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m.log().WithField("func", "Update").Infof("Users item updated successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BuildConditionWithKey builds the WHERE clause for user queries
|
// BuildConditionWithKey builds the WHERE clause for user queries
|
||||||
func (m *Users) BuildConditionWithKey(key *string) BoolExpression {
|
func (m *Users) BuildConditionWithKey(key *string) BoolExpression {
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
|
|
||||||
cond := tbl.DeletedAt.IS_NULL()
|
cond := tbl.DeletedAt.IS_NULL()
|
||||||
|
|
||||||
@@ -59,7 +43,7 @@ func (m *Users) countByCondition(ctx context.Context, expr BoolExpression) (int6
|
|||||||
Cnt int64
|
Cnt int64
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
||||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
@@ -73,10 +57,14 @@ func (m *Users) countByCondition(ctx context.Context, expr BoolExpression) (int6
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List returns a paginated list of users
|
// List returns a paginated list of users
|
||||||
func (m *Users) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
func (m *Users) List(
|
||||||
|
ctx context.Context,
|
||||||
|
pagination *requests.Pagination,
|
||||||
|
cond BoolExpression,
|
||||||
|
) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(cond).
|
WHERE(cond).
|
||||||
@@ -106,14 +94,19 @@ func (m *Users) List(ctx context.Context, pagination *requests.Pagination, cond
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PostList returns a paginated list of posts for a user
|
// PostList returns a paginated list of posts for a user
|
||||||
func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
func (m *Users) PostList(
|
||||||
|
ctx context.Context,
|
||||||
|
userId int64,
|
||||||
|
pagination *requests.Pagination,
|
||||||
|
conds ...Cond,
|
||||||
|
) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
tblUserPosts := table.UserPosts
|
tblUserPosts := tblUserPosts
|
||||||
|
|
||||||
cond := CondJoin(ExprCond(tblUserPosts.UserID.EQ(Int64(userId))), conds...)
|
cond := CondJoin(ExprCond(tblUserPosts.UserID.EQ(Int64(userId))), conds...)
|
||||||
|
|
||||||
tbl := table.Posts
|
tbl := tblPosts
|
||||||
stmt := SELECT(tbl.AllColumns).
|
stmt := SELECT(tbl.AllColumns).
|
||||||
FROM(tbl.
|
FROM(tbl.
|
||||||
RIGHT_JOIN(
|
RIGHT_JOIN(
|
||||||
@@ -163,7 +156,7 @@ func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests
|
|||||||
|
|
||||||
// GetUserIDByOpenID
|
// GetUserIDByOpenID
|
||||||
func (m *Users) GetUserByOpenID(ctx context.Context, openID string) (*Users, error) {
|
func (m *Users) GetUserByOpenID(ctx context.Context, openID string) (*Users, error) {
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
|
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
@@ -213,7 +206,7 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(
|
WHERE(
|
||||||
@@ -235,7 +228,7 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[int64]bool, error) {
|
func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[int64]bool, error) {
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
|
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
|
||||||
tbl.UserID.EQ(Int64(m.ID)).AND(
|
tbl.UserID.EQ(Int64(m.ID)).AND(
|
||||||
tbl.PostID.IN(lo.Map(postIDs, func(id int64, _ int) Expression { return Int64(id) })...),
|
tbl.PostID.IN(lo.Map(postIDs, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||||
@@ -259,7 +252,7 @@ func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[i
|
|||||||
|
|
||||||
// HasBought
|
// HasBought
|
||||||
func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
|
func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.ID).
|
SELECT(tbl.ID).
|
||||||
WHERE(
|
WHERE(
|
||||||
@@ -284,7 +277,7 @@ func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
|
|||||||
|
|
||||||
// SetUsername
|
// SetUsername
|
||||||
func (m *Users) SetUsername(ctx context.Context, username string) error {
|
func (m *Users) SetUsername(ctx context.Context, username string) error {
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.Username).
|
UPDATE(tbl.Username).
|
||||||
SET(String(username)).
|
SET(String(username)).
|
||||||
@@ -302,7 +295,7 @@ func (m *Users) SetUsername(ctx context.Context, username string) error {
|
|||||||
|
|
||||||
// UpdateUserToken
|
// UpdateUserToken
|
||||||
func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken) error {
|
func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken) error {
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.AuthToken).
|
UPDATE(tbl.AuthToken).
|
||||||
SET(fields.ToJson(token)).
|
SET(fields.ToJson(token)).
|
||||||
@@ -320,7 +313,7 @@ func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken)
|
|||||||
|
|
||||||
// BuyPosts
|
// BuyPosts
|
||||||
func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
|
func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
INSERT(tbl.MutableColumns).
|
INSERT(tbl.MutableColumns).
|
||||||
MODEL(&UserPosts{
|
MODEL(&UserPosts{
|
||||||
@@ -341,7 +334,7 @@ func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
|
func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
|
||||||
tbl := table.UserPosts
|
tbl := tblUserPosts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
DELETE().
|
DELETE().
|
||||||
WHERE(
|
WHERE(
|
||||||
@@ -360,7 +353,7 @@ func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
|
|||||||
|
|
||||||
// SetBalance
|
// SetBalance
|
||||||
func (m *Users) SetBalance(ctx context.Context, balance int64) error {
|
func (m *Users) SetBalance(ctx context.Context, balance int64) error {
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.Balance).
|
UPDATE(tbl.Balance).
|
||||||
SET(Int64(balance)).
|
SET(Int64(balance)).
|
||||||
@@ -378,7 +371,7 @@ func (m *Users) SetBalance(ctx context.Context, balance int64) error {
|
|||||||
|
|
||||||
// AddBalance adds the given amount to the user's balance
|
// AddBalance adds the given amount to the user's balance
|
||||||
func (m *Users) AddBalance(ctx context.Context, amount int64) error {
|
func (m *Users) AddBalance(ctx context.Context, amount int64) error {
|
||||||
tbl := table.Users
|
tbl := tblUsers
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
UPDATE(tbl.Balance).
|
UPDATE(tbl.Balance).
|
||||||
SET(tbl.Balance.ADD(Int64(amount))).
|
SET(tbl.Balance.ADD(Int64(amount))).
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ ignores:
|
|||||||
- migrations
|
- migrations
|
||||||
model:
|
model:
|
||||||
- migrations
|
- migrations
|
||||||
- user_posts
|
|
||||||
types:
|
types:
|
||||||
# users: # table name
|
# users: # table name
|
||||||
# meta: UserMeta
|
# meta: UserMeta
|
||||||
|
|||||||
Reference in New Issue
Block a user