feat: add order module
This commit is contained in:
77
backend/app/http/orders/service.go
Normal file
77
backend/app/http/orders/service.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"backend/app/requests"
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/database/models/qvyun_v2/public/table"
|
||||
"backend/providers/otel"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
)
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (svc *Service) Prepare() error {
|
||||
svc.log = log.WithField("module", "orders.service")
|
||||
_ = Int(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserOrders
|
||||
func (svc *Service) GetOrders(ctx context.Context, pagination *requests.Pagination, filter *UserOrderFilter) ([]model.Orders, int64, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetUserOrders")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("user.id", filter.UserID),
|
||||
attribute.Int64("page.page", pagination.Page),
|
||||
attribute.Int64("page.limit", pagination.Limit),
|
||||
)
|
||||
|
||||
tbl := table.Orders
|
||||
|
||||
cond := tbl.UserID.EQ(Int64(filter.UserID))
|
||||
if filter.Status != nil {
|
||||
cond = cond.AND(tbl.Status.EQ(Int16(int16(*filter.Status))))
|
||||
}
|
||||
|
||||
if filter.Type != nil {
|
||||
cond = cond.AND(tbl.Type.EQ(Int16(int16(*filter.Type))))
|
||||
}
|
||||
|
||||
if filter.CreatedAt != nil {
|
||||
cond = cond.AND(tbl.CreatedAt.LT_EQ(TimestampT(*filter.CreatedAt)))
|
||||
}
|
||||
|
||||
cntStmt := tbl.SELECT(COUNT(tbl.ID).AS("cnt")).WHERE(cond)
|
||||
var count struct {
|
||||
Cnt int64
|
||||
}
|
||||
|
||||
if err := cntStmt.QueryContext(ctx, svc.db, &count); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(cond).
|
||||
ORDER_BY(tbl.CreatedAt.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset())
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var orders []model.Orders
|
||||
if err := stmt.QueryContext(ctx, svc.db, &orders); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, count.Cnt, nil
|
||||
}
|
||||
Reference in New Issue
Block a user