feat: add order module
This commit is contained in:
52
backend/app/http/orders/controller.go
Normal file
52
backend/app/http/orders/controller.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"backend/app/requests"
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type Controller struct {
|
||||
svc *Service
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (c *Controller) Prepare() error {
|
||||
c.log = log.WithField("module", "orders.Controller")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Orders show user orders
|
||||
// @Router /api/v1/orders [get]
|
||||
// @Bind claim local
|
||||
// @Bind pagination query
|
||||
// @Bind filter query
|
||||
func (c *Controller) List(ctx fiber.Ctx, claim *jwt.Claims, pagination *requests.Pagination, filter *UserOrderFilter) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
pager := &requests.Pager{
|
||||
Pagination: *pagination,
|
||||
}
|
||||
|
||||
filter.UserID = claim.UserID
|
||||
orders, total, err := c.svc.GetOrders(ctx.Context(), pagination, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pager.Total = total
|
||||
|
||||
pager.Items = lo.FilterMap(orders, func(item model.Orders, _ int) (UserOrder, bool) {
|
||||
var o UserOrder
|
||||
if err := copier.Copy(&o, item); err != nil {
|
||||
return o, false
|
||||
}
|
||||
return o, true
|
||||
})
|
||||
|
||||
return pager, nil
|
||||
}
|
||||
26
backend/app/http/orders/dto.go
Normal file
26
backend/app/http/orders/dto.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package orders
|
||||
|
||||
import "time"
|
||||
|
||||
type UserOrder struct {
|
||||
ID int64 `sql:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Type int16 `json:"type"`
|
||||
Status int16 `json:"status"`
|
||||
OrderSerial string `json:"order_serial"`
|
||||
RemoteOrderSerial string `json:"remote_order_serial"`
|
||||
RefundSerial string `json:"refund_serial"`
|
||||
RemoteRefundSerial string `json:"remote_refund_serial"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
Description *string `json:"description"`
|
||||
Meta *string `json:"meta"`
|
||||
}
|
||||
|
||||
type UserOrderFilter struct {
|
||||
UserID int64
|
||||
CreatedAt *time.Time `query:"created_at"`
|
||||
Type *int16 `query:"type"`
|
||||
Status *int16 `query:"status"`
|
||||
Description *string `query:"description"`
|
||||
}
|
||||
56
backend/app/http/orders/provider.gen.go
Executable file
56
backend/app/http/orders/provider.gen.go
Executable file
@@ -0,0 +1,56 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
svc *Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
svc: svc,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
controller *Controller,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
controller: controller,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
db *sql.DB,
|
||||
) (*Service, error) {
|
||||
obj := &Service{
|
||||
db: db,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
40
backend/app/http/orders/routes.gen.go
Normal file
40
backend/app/http/orders/routes.gen.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
|
||||
package orders
|
||||
|
||||
import (
|
||||
"backend/app/requests"
|
||||
. "backend/pkg/f"
|
||||
"backend/providers/jwt"
|
||||
|
||||
_ "git.ipao.vip/rogeecn/atom"
|
||||
_ "git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
controller *Controller
|
||||
}
|
||||
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.orders")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Routes) Name() string {
|
||||
return "orders"
|
||||
}
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: Controller
|
||||
router.Get("/users/orders", DataFunc3(
|
||||
r.controller.List,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserOrderFilter]("filter"),
|
||||
))
|
||||
|
||||
}
|
||||
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
|
||||
}
|
||||
37
backend/app/http/orders/service_test.go
Normal file
37
backend/app/http/orders/service_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"backend/app/service/testx"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type ServiceInjectParams struct {
|
||||
dig.In
|
||||
Svc *Service
|
||||
}
|
||||
|
||||
type ServiceTestSuite struct {
|
||||
suite.Suite
|
||||
ServiceInjectParams
|
||||
}
|
||||
|
||||
func Test_DiscoverMedias(t *testing.T) {
|
||||
providers := testx.Default().With(
|
||||
Provide,
|
||||
)
|
||||
|
||||
testx.Serve(providers, t, func(params ServiceInjectParams) {
|
||||
suite.Run(t, &ServiceTestSuite{ServiceInjectParams: params})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ServiceTestSuite) Test_Service() {
|
||||
Convey("Test Service", s.T(), func() {
|
||||
So(s.Svc, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user