feat: add posts
This commit is contained in:
@@ -30,7 +30,7 @@ func (r *Routes) Name() string {
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: Controller
|
||||
router.Get("/users/orders", DataFunc3(
|
||||
router.Get("/api/v1/orders", DataFunc3(
|
||||
r.controller.List,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
|
||||
53
backend/app/http/posts/controller.go
Normal file
53
backend/app/http/posts/controller.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package posts
|
||||
|
||||
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", "posts.Controller")
|
||||
return nil
|
||||
}
|
||||
|
||||
// List show posts list
|
||||
// @Router /api/v1/posts [get]
|
||||
// @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) {
|
||||
pagination.Format()
|
||||
pager := &requests.Pager{
|
||||
Pagination: *pagination,
|
||||
}
|
||||
|
||||
filter.TenantID = *claim.TenantID
|
||||
filter.UserID = claim.UserID
|
||||
orders, total, err := c.svc.GetPosts(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
|
||||
}
|
||||
32
backend/app/http/posts/dto.go
Normal file
32
backend/app/http/posts/dto.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserPost struct {
|
||||
ID int64 `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
HashID string `json:"hash_id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Poster string `json:"poster"`
|
||||
Content string `json:"content"`
|
||||
Stage int16 `json:"stage"`
|
||||
Status int16 `json:"status"`
|
||||
Price int64 `json:"price"`
|
||||
Discount int16 `json:"discount"`
|
||||
Views int64 `json:"views"`
|
||||
Likes int64 `json:"likes"`
|
||||
Meta *string `json:"meta"`
|
||||
Assets *string `json:"assets"`
|
||||
}
|
||||
|
||||
type UserPostFilter struct {
|
||||
ID *int64 `json:"id"`
|
||||
TenantID int64 `query:"tenant_id"`
|
||||
UserID int64 `query:"user_id"`
|
||||
CreatedAt *time.Time `query:"created_at"`
|
||||
Keyword *string `json:"title"`
|
||||
}
|
||||
56
backend/app/http/posts/provider.gen.go
Executable file
56
backend/app/http/posts/provider.gen.go
Executable file
@@ -0,0 +1,56 @@
|
||||
package posts
|
||||
|
||||
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/posts/routes.gen.go
Normal file
40
backend/app/http/posts/routes.gen.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
|
||||
package posts
|
||||
|
||||
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.posts")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Routes) Name() string {
|
||||
return "posts"
|
||||
}
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: Controller
|
||||
router.Get("/api/v1/posts", DataFunc3(
|
||||
r.controller.List,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserPostFilter]("filter"),
|
||||
))
|
||||
|
||||
}
|
||||
91
backend/app/http/posts/service.go
Normal file
91
backend/app/http/posts/service.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"backend/app/requests"
|
||||
"backend/database"
|
||||
"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", "posts.service")
|
||||
_ = Int(1)
|
||||
return 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")
|
||||
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
|
||||
|
||||
cond := Bool(true)
|
||||
if filter.ID != nil {
|
||||
cond = cond.AND(tbl.ID.EQ(Int64(*filter.ID)))
|
||||
}
|
||||
if filter.UserID != 0 {
|
||||
cond = cond.AND(tbl.UserID.EQ(Int64(filter.UserID)))
|
||||
}
|
||||
if filter.CreatedAt != nil {
|
||||
cond = cond.AND(tbl.CreatedAt.LT_EQ(TimestampT(*filter.CreatedAt)))
|
||||
}
|
||||
if filter.TenantID != 0 {
|
||||
cond = cond.AND(tbl.TenantID.EQ(Int64(filter.TenantID)))
|
||||
}
|
||||
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
|
||||
}
|
||||
37
backend/app/http/posts/service_test.go
Normal file
37
backend/app/http/posts/service_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package posts
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func (r *Routes) Name() string {
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: Controller
|
||||
router.Get("/users/info", DataFunc1(
|
||||
router.Get("/api/v1/users/info", DataFunc1(
|
||||
r.controller.Info,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
))
|
||||
|
||||
@@ -55,7 +55,6 @@ func (s *ServiceTestSuite) Test_GetUserIDByOpenID() {
|
||||
Channel: fields.AuthChannelWeChat,
|
||||
UserID: 1,
|
||||
OpenID: "test_open_id",
|
||||
AccessKey: "test_access_key",
|
||||
AccessToken: "test_access_token",
|
||||
RefreshToken: "test_refresh_token",
|
||||
ExpireAt: time.Now().Add(time.Hour),
|
||||
|
||||
Reference in New Issue
Block a user