feat: update
This commit is contained in:
@@ -12,32 +12,15 @@ import (
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (m *Orders) log() *log.Entry {
|
||||
return log.WithField("model", "OrdersModel")
|
||||
}
|
||||
|
||||
// GetByID returns an order by ID
|
||||
func (m *Orders) GetByID(ctx context.Context, id int64) (*Orders, error) {
|
||||
tbl := table.Orders
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var order Orders
|
||||
err := stmt.QueryContext(ctx, db, &order)
|
||||
if err != nil {
|
||||
m.log().Errorf("error querying order by ID: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &order, nil
|
||||
var ordersUpdateExcludeColumns = []Column{
|
||||
table.Orders.OrderNo,
|
||||
table.Orders.Price,
|
||||
table.Orders.Discount,
|
||||
table.Orders.SubOrderNo,
|
||||
table.Orders.PostID,
|
||||
table.Orders.UserID,
|
||||
}
|
||||
|
||||
// BuildConditionWithKey builds the WHERE clause for order queries
|
||||
@@ -150,7 +133,7 @@ func (m *Orders) List(ctx context.Context, pagination *requests.Pagination, cond
|
||||
}
|
||||
|
||||
// Create creates a new order
|
||||
func (o *Orders) Create(ctx context.Context, userId, postId int64) (*Orders, error) {
|
||||
func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64) (*Orders, error) {
|
||||
post, err := PostsModel().GetByID(ctx, postId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get post")
|
||||
@@ -215,25 +198,6 @@ func (m *Orders) SetStatus(ctx context.Context, status fields.OrderStatus) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// Count
|
||||
func (m *Orders) Count(ctx context.Context, cond BoolExpression) (int64, error) {
|
||||
tbl := table.Orders
|
||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(cond)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var cnt struct {
|
||||
Cnt int64
|
||||
}
|
||||
|
||||
err := stmt.QueryContext(ctx, db, &cnt)
|
||||
if err != nil {
|
||||
m.log().Errorf("error counting orders: %v", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return cnt.Cnt, nil
|
||||
}
|
||||
|
||||
// SumAmount
|
||||
func (m *Orders) SumAmount(ctx context.Context) (int64, error) {
|
||||
tbl := table.Orders
|
||||
@@ -292,31 +256,3 @@ func (m *Orders) SetTransactionID(ctx context.Context, transactionID string) err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update
|
||||
func (m *Orders) Update(ctx context.Context) error {
|
||||
tbl := table.Orders
|
||||
stmt := tbl.
|
||||
UPDATE(
|
||||
tbl.MutableColumns.Except(
|
||||
tbl.OrderNo,
|
||||
tbl.Price,
|
||||
tbl.Discount,
|
||||
tbl.SubOrderNo,
|
||||
tbl.PostID,
|
||||
tbl.UserID,
|
||||
),
|
||||
).
|
||||
MODEL(m).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
).
|
||||
RETURNING(tbl.AllColumns)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().Errorf("error updating order: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user