feat: add wechat pay
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"backend/app/http/tenants"
|
||||
"backend/app/requests"
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/providers/jwt"
|
||||
@@ -13,8 +14,9 @@ import (
|
||||
|
||||
// @provider
|
||||
type Controller struct {
|
||||
svc *Service
|
||||
log *log.Entry `inject:"false"`
|
||||
tenantSvc *tenants.Service
|
||||
svc *Service
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (c *Controller) Prepare() error {
|
||||
@@ -24,16 +26,22 @@ func (c *Controller) Prepare() error {
|
||||
|
||||
// List show posts list
|
||||
// @Router /api/v1/posts [get]
|
||||
// @Bind tenantSlug cookie key(tenant)
|
||||
// @Bind claim local
|
||||
// @Bind pagination query
|
||||
// @Bind filter query
|
||||
func (c *Controller) List(ctx fiber.Ctx, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
||||
func (c *Controller) List(ctx fiber.Ctx, tenantSlug string, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
||||
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pagination.Format()
|
||||
pager := &requests.Pager{
|
||||
Pagination: *pagination,
|
||||
}
|
||||
|
||||
filter.TenantID = *claim.TenantID
|
||||
filter.TenantID = tenant.ID
|
||||
filter.UserID = claim.UserID
|
||||
orders, total, err := c.svc.GetPosts(ctx.Context(), pagination, filter)
|
||||
if err != nil {
|
||||
@@ -51,3 +59,64 @@ func (c *Controller) List(ctx fiber.Ctx, claim *jwt.Claims, pagination *requests
|
||||
|
||||
return pager, nil
|
||||
}
|
||||
|
||||
// ListBought show user bought posts list
|
||||
// @Router /api/v1/bought-posts [get]
|
||||
// @Bind tenantSlug cookie key(tenant)
|
||||
// @Bind claim local
|
||||
// @Bind pagination query
|
||||
// @Bind filter query
|
||||
func (c *Controller) ListBought(ctx fiber.Ctx, tenantSlug string, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
||||
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pagination.Format()
|
||||
pager := &requests.Pager{
|
||||
Pagination: *pagination,
|
||||
}
|
||||
|
||||
filter.TenantID = tenant.ID
|
||||
filter.UserID = claim.UserID
|
||||
orders, total, err := c.svc.GetBoughtPosts(ctx.Context(), pagination, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pager.Total = total
|
||||
|
||||
pager.Items = lo.FilterMap(orders, func(item model.Posts, _ int) (UserPost, bool) {
|
||||
var o UserPost
|
||||
if err := copier.Copy(&o, item); err != nil {
|
||||
return o, false
|
||||
}
|
||||
return o, true
|
||||
})
|
||||
|
||||
return pager, nil
|
||||
}
|
||||
|
||||
// Show show posts detail
|
||||
// @Router /api/v1/show/:hash [get]
|
||||
// @Bind claim local
|
||||
// @Bind tenantSlug cookie key(tenant)
|
||||
// @Bind hash path
|
||||
func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash string) (*UserPost, error) {
|
||||
userPost := &UserPost{}
|
||||
|
||||
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post, err := c.svc.GetPostByHash(ctx.Context(), tenant.ID, hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := copier.Copy(userPost, post); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return userPost, nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package posts
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"backend/app/http/tenants"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
@@ -12,9 +14,11 @@ import (
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
svc *Service,
|
||||
tenantSvc *tenants.Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
svc: svc,
|
||||
svc: svc,
|
||||
tenantSvc: tenantSvc,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -30,11 +30,27 @@ func (r *Routes) Name() string {
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: Controller
|
||||
router.Get("/api/v1/posts", DataFunc3(
|
||||
router.Get("/api/v1/posts", DataFunc4(
|
||||
r.controller.List,
|
||||
CookieParam("tenant"),
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserPostFilter]("filter"),
|
||||
))
|
||||
|
||||
router.Get("/api/v1/bought-posts", DataFunc4(
|
||||
r.controller.ListBought,
|
||||
CookieParam("tenant"),
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserPostFilter]("filter"),
|
||||
))
|
||||
|
||||
router.Get("/api/v1/show/:hash", DataFunc3(
|
||||
r.controller.Show,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
CookieParam("tenant"),
|
||||
PathParam[string]("hash"),
|
||||
))
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"backend/providers/otel"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
@@ -28,6 +29,78 @@ func (svc *Service) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBoughtPosts
|
||||
func (svc *Service) GetBoughtPosts(ctx context.Context, pagination *requests.Pagination, filter *UserPostFilter) ([]model.Posts, int64, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetBoughtPosts")
|
||||
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.Posts
|
||||
|
||||
boughtIds, err := svc.GetUserBoughtIDs(ctx, filter.TenantID, filter.UserID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if len(boughtIds) == 0 {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
idExprs := lo.Map(boughtIds, func(id int64, _ int) Expression { return Int64(id) })
|
||||
|
||||
cond := tbl.ID.IN(
|
||||
idExprs...,
|
||||
).AND(
|
||||
tbl.TenantID.EQ(Int64(filter.TenantID)),
|
||||
).AND(
|
||||
tbl.UserID.EQ(Int64(filter.UserID)),
|
||||
)
|
||||
|
||||
if filter.CreatedAt != nil {
|
||||
cond = cond.AND(tbl.CreatedAt.LT_EQ(TimestampT(*filter.CreatedAt)))
|
||||
}
|
||||
|
||||
if filter.Keyword != nil {
|
||||
cond = cond.AND(
|
||||
tbl.Title.
|
||||
LIKE(String(database.WrapLike(*filter.Keyword))).
|
||||
OR(
|
||||
tbl.Description.LIKE(String(database.WrapLike(*filter.Keyword))),
|
||||
).
|
||||
OR(
|
||||
tbl.Content.LIKE(String(database.WrapLike(*filter.Keyword))),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
cntStmt := tbl.SELECT(COUNT(tbl.ID).AS("cnt")).WHERE(cond)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(cntStmt.DebugSql()))
|
||||
|
||||
var count struct {
|
||||
Cnt int64
|
||||
}
|
||||
if err := cntStmt.QueryContext(ctx, svc.db, &count); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset())
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var posts []model.Posts
|
||||
if err := stmt.QueryContext(ctx, svc.db, &posts); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return posts, count.Cnt, nil
|
||||
}
|
||||
|
||||
// GetPosts
|
||||
func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Pagination, filter *UserPostFilter) ([]model.Posts, int64, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetPosts")
|
||||
@@ -89,3 +162,61 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
|
||||
|
||||
return posts, count.Cnt, nil
|
||||
}
|
||||
|
||||
// GetPostByHash
|
||||
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetPostByHash")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.String("hash", hash),
|
||||
)
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.Hash.EQ(String(hash)).AND(
|
||||
tbl.TenantID.EQ(Int64(tenantID)),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var post model.Posts
|
||||
if err := stmt.QueryContext(ctx, svc.db, &post); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
// GetUserBoughtPosts
|
||||
func (svc *Service) GetUserBoughtIDs(ctx context.Context, tenantID, userID int64) ([]int64, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetUserBoughtIDs")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("tenant.id", tenantID),
|
||||
attribute.Int64("user.id", userID),
|
||||
)
|
||||
tbl := table.UserBoughtPosts
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.PostID.AS("post_id")).
|
||||
WHERE(
|
||||
tbl.TenantID.EQ(Int64(tenantID)).AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
type tmp struct {
|
||||
PostID int64
|
||||
}
|
||||
|
||||
var results []tmp
|
||||
if err := stmt.QueryContext(ctx, svc.db, &results); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return lo.Map(results, func(item tmp, _ int) int64 {
|
||||
return item.PostID
|
||||
}), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user