refactor: 优化数据库查询构建链式调用
This commit is contained in:
@@ -32,7 +32,7 @@ func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (id i
|
||||
|
||||
// Update 更新分类
|
||||
func (d *category) Update(ctx context.Context, req *dto.UpdateCategoryReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameCategory).Data(&req).OmitEmpty().Where(entity.CategoryCol.Id, req.Id).Update()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameCategory).Data(&req).Where(entity.CategoryCol.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -65,8 +65,7 @@ func (d *category) Count(ctx context.Context, req *dto.ListCategoryReq) (count i
|
||||
|
||||
// List 获取分类列表
|
||||
func (d *category) List(ctx context.Context, req *dto.ListCategoryReq, fields ...string) (res []entity.Category, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.Fields(fields)
|
||||
model := d.buildListFilter(ctx, req).Fields(fields)
|
||||
model.OrderAsc(entity.CategoryCol.Sort)
|
||||
model.OrderDesc(entity.CategoryCol.CreatedAt)
|
||||
if req.Page != nil {
|
||||
@@ -88,6 +87,5 @@ func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq
|
||||
}
|
||||
model.Where(entity.CategoryCol.ParentId, req.ParentId)
|
||||
model.Where(entity.CategoryCol.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user