补充Querylog
This commit is contained in:
parent
0f1c51f1b2
commit
e0e7ff5b7c
@ -2,7 +2,9 @@ package impl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/infraboard/mcube/v2/ioc/config/datasource"
|
||||
"gitlab.com/go-course-project/go17/vblog/apps/blog"
|
||||
)
|
||||
|
||||
@ -14,8 +16,51 @@ type BlogServiceImpl struct {
|
||||
}
|
||||
|
||||
// CreateBlog implements blog.Service.
|
||||
func (b *BlogServiceImpl) CreateBlog(context.Context, *blog.CreateBlogRequest) (*blog.Blog, error) {
|
||||
panic("unimplemented")
|
||||
func (b *BlogServiceImpl) CreateBlog(ctx context.Context, in *blog.CreateBlogRequest) (*blog.Blog, error) {
|
||||
ins, err := blog.NewBook(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = datasource.DBFromCtx(ctx).Create(ins).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ins, nil
|
||||
}
|
||||
|
||||
// QueryBlog implements blog.Service.
|
||||
func (b *BlogServiceImpl) QueryBlog(ctx context.Context, in *blog.QueryBlogRequest) (*blog.BlogSet, error) {
|
||||
query := datasource.DBFromCtx(ctx).Model(&blog.Blog{})
|
||||
if in.Keywords != "" {
|
||||
query = query.Where("title LIKE ?", "%"+in.Keywords+"%")
|
||||
}
|
||||
if in.Stage != nil {
|
||||
query = query.Where("stage = ?", in.Stage)
|
||||
}
|
||||
if in.CreateBy != "" {
|
||||
query = query.Where("create_by = ?", in.CreateBy)
|
||||
}
|
||||
if in.Category != "" {
|
||||
query = query.Where("category = ?", in.Category)
|
||||
}
|
||||
for k, v := range in.Tags {
|
||||
query = query.Where(fmt.Sprintf("tags->>'$.%s' = '?'", k), v)
|
||||
}
|
||||
set := blog.NewBlogSet()
|
||||
|
||||
// COUNT
|
||||
if err := query.Count(&set.Total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 处理分页
|
||||
err := query.Order("created_at DESC").Offset(int(in.Offset())).Limit(int(in.PageSize)).Find(&set.Items).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
// DeleteBlog implements blog.Service.
|
||||
@ -33,11 +78,6 @@ func (b *BlogServiceImpl) PublishBlog(context.Context, *blog.PublishBlogRequest)
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// QueryBlog implements blog.Service.
|
||||
func (b *BlogServiceImpl) QueryBlog(context.Context, *blog.QueryBlogRequest) (*blog.BlogSet, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// UpdateBlog implements blog.Service.
|
||||
func (b *BlogServiceImpl) UpdateBlog(context.Context, *blog.UpdateBlogRequest) (*blog.Blog, error) {
|
||||
panic("unimplemented")
|
||||
|
38
vblog/apps/blog/impl_test.go
Normal file
38
vblog/apps/blog/impl_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package blog_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitlab.com/go-course-project/go17/vblog/apps/blog"
|
||||
"gitlab.com/go-course-project/go17/vblog/apps/blog/impl"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
||||
// Required("admin")
|
||||
func TestCreateBlog(t *testing.T) {
|
||||
req := &blog.CreateBlogRequest{
|
||||
Title: "Go项目课",
|
||||
Summary: "全栈项目",
|
||||
Content: "GORM + GIN",
|
||||
Category: "软件开发",
|
||||
}
|
||||
ins, err := impl.BlogService.CreateBlog(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(ins)
|
||||
}
|
||||
|
||||
// SELECT * FROM `blogs` ORDER BY created_at DESC LIMIT 20
|
||||
func TestQueryBlog(t *testing.T) {
|
||||
req := blog.NewQueryBlogRequest()
|
||||
ins, err := impl.BlogService.QueryBlog(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(ins)
|
||||
}
|
@ -39,6 +39,13 @@ type DescribeBlogRequest struct {
|
||||
utils.GetRequest
|
||||
}
|
||||
|
||||
func NewQueryBlogRequest() *QueryBlogRequest {
|
||||
return &QueryBlogRequest{
|
||||
PageRequest: *utils.NewPageRequest(),
|
||||
Tags: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
type QueryBlogRequest struct {
|
||||
// 分页参数
|
||||
utils.PageRequest
|
||||
@ -47,9 +54,12 @@ type QueryBlogRequest struct {
|
||||
// 状态过滤参数, 作者:nil, 访客: STAGE_PUBLISHED
|
||||
Stage *STAGE `json:"stage"`
|
||||
// 查询某个用户具体的文章: 给作者用的
|
||||
Username string `json:"username"`
|
||||
CreateBy string `json:"createBy"`
|
||||
// 分类
|
||||
Category string `json:"category"`
|
||||
// 查询Tag相关的文章
|
||||
// SELECT *
|
||||
// FROM my_table
|
||||
// WHERE data->>'$.name' = '某个名字';
|
||||
Tags map[string]string `json:"tag"`
|
||||
}
|
||||
|
@ -3,14 +3,33 @@ package blog
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/infraboard/mcube/v2/exception"
|
||||
"github.com/infraboard/mcube/v2/ioc/config/validator"
|
||||
"github.com/infraboard/mcube/v2/tools/pretty"
|
||||
"gitlab.com/go-course-project/go17/vblog/utils"
|
||||
)
|
||||
|
||||
func NewBlogSet() *BlogSet {
|
||||
return &BlogSet{
|
||||
Items: []*Blog{},
|
||||
}
|
||||
}
|
||||
|
||||
type BlogSet struct {
|
||||
Total int32 `json:"total"`
|
||||
Total int64 `json:"total"`
|
||||
Items []*Blog `json:"items"`
|
||||
}
|
||||
|
||||
func NewBook(in *CreateBlogRequest) (*Blog, error) {
|
||||
if err := in.Validate(); err != nil {
|
||||
return nil, exception.NewBadRequest("参数异常: %s", err)
|
||||
}
|
||||
return &Blog{
|
||||
ResourceMeta: *utils.NewResourceMeta(),
|
||||
CreateBlogRequest: *in,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Blog struct {
|
||||
// 存放到数据里的对象的远数据信息
|
||||
utils.ResourceMeta
|
||||
@ -20,23 +39,31 @@ type Blog struct {
|
||||
Status
|
||||
}
|
||||
|
||||
func (t *Blog) String() string {
|
||||
return pretty.ToJSON(t)
|
||||
}
|
||||
|
||||
func (t *Blog) TableName() string {
|
||||
return "blogs"
|
||||
}
|
||||
|
||||
type CreateBlogRequest struct {
|
||||
// 标题
|
||||
Title string `json:"title" gorm:"column:title;type:varchar(200)"`
|
||||
Title string `json:"title" gorm:"column:title;type:varchar(200)" validate:"required"`
|
||||
// 摘要
|
||||
Summary string `json:"summary" gorm:"column:summary;type:text"`
|
||||
Summary string `json:"summary" gorm:"column:summary;type:text" validate:"required"`
|
||||
// 内容
|
||||
Content string `json:"content" gorm:"column:content;type:text"`
|
||||
Content string `json:"content" gorm:"column:content;type:text" validate:"required"`
|
||||
// 分类
|
||||
Category string `json:"category" gorm:"column:category;type:varchar(200);index"`
|
||||
Category string `json:"category" gorm:"column:category;type:varchar(200);index" validate:"required"`
|
||||
// 标签
|
||||
Tags map[string]string `json:"tags" gorm:"column:tags;serializer:json"`
|
||||
}
|
||||
|
||||
func (r *CreateBlogRequest) Validate() error {
|
||||
return validator.Validate(r)
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
StatusSpec
|
||||
// 状态变化的时间, 拿发布时间
|
||||
|
@ -1,8 +1,19 @@
|
||||
package utils
|
||||
|
||||
func NewPageRequest() *PageRequest {
|
||||
return &PageRequest{
|
||||
PageSize: 20,
|
||||
PageNumber: 1,
|
||||
}
|
||||
}
|
||||
|
||||
type PageRequest struct {
|
||||
// 分页大小
|
||||
PageSize uint `json:"page_size"`
|
||||
// 当前是多少页面
|
||||
PageNumber uint `json:"page_number"`
|
||||
}
|
||||
|
||||
func (r *PageRequest) Offset() uint {
|
||||
return ((r.PageNumber) - 1) * (r.PageSize)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user