Skip to content

Commit

Permalink
technical debt
Browse files Browse the repository at this point in the history
  • Loading branch information
e154 committed Jan 5, 2024
1 parent c737762 commit c2dec10
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
17 changes: 17 additions & 0 deletions adaptors/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type IEntity interface {
GetByIdsSimple(ctx context.Context, ids []common.EntityId) (list []*m.Entity, err error)
Delete(ctx context.Context, id common.EntityId) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, autoLoad bool, query, plugin *string, areaId *int64) (list []*m.Entity, total int64, err error)
ListPlain(ctx context.Context, limit, offset int64, orderBy, sort string, autoLoad bool, query, plugin *string, areaId *int64) (list []*m.Entity, total int64, err error)
GetByType(ctx context.Context, t string, limit, offset int64) (list []*m.Entity, err error)
Update(ctx context.Context, ver *m.Entity) (err error)
Search(ctx context.Context, query string, limit, offset int64) (list []*m.Entity, total int64, err error)
Expand Down Expand Up @@ -270,6 +271,22 @@ func (n *Entity) List(ctx context.Context, limit, offset int64, orderBy, sort st
return
}

// ListPlain ...
func (n *Entity) ListPlain(ctx context.Context, limit, offset int64, orderBy, sort string, autoLoad bool, query, plugin *string, areaId *int64) (list []*m.Entity, total int64, err error) {

var dbList []*db.Entity
if dbList, total, err = n.table.ListPlain(ctx, int(limit), int(offset), orderBy, sort, autoLoad, query, plugin, areaId); err != nil {
return
}

list = make([]*m.Entity, len(dbList))
for i, dbVer := range dbList {
list[i] = n.fromDb(dbVer)
}

return
}

// GetByType ...
func (n *Entity) GetByType(ctx context.Context, t string, limit, offset int64) (list []*m.Entity, err error) {

Expand Down
11 changes: 9 additions & 2 deletions api/dto/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,15 @@ func (s Script) ToListResult(list []*m.Script) []*stub.ApiScript {

items := make([]*stub.ApiScript, 0, len(list))

for _, i := range list {
items = append(items, s.GetStubScript(i))
for _, script := range list {
items = append(items, &stub.ApiScript{
Id: script.Id,
Lang: string(script.Lang),
Name: script.Name,
Description: script.Description,
CreatedAt: script.CreatedAt,
UpdatedAt: script.UpdatedAt,
})
}

return items
Expand Down
54 changes: 48 additions & 6 deletions db/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,8 @@ func (n Entities) Delete(ctx context.Context, id common.EntityId) (err error) {
func (n *Entities) List(ctx context.Context, limit, offset int, orderBy, sort string, autoLoad bool,
query, plugin *string, areaId *int64) (list []*Entity, total int64, err error) {

if err = n.Db.WithContext(ctx).Model(Entity{}).Count(&total).Error; err != nil {
err = errors.Wrap(apperr.ErrEntityList, err.Error())
return
}

list = make([]*Entity, 0)
q := n.Db
q := n.Db.WithContext(ctx).Model(Entity{})
if autoLoad {
q = q.Where("auto_load = ?", true)
}
Expand All @@ -217,6 +212,10 @@ func (n *Entities) List(ctx context.Context, limit, offset int, orderBy, sort st
if areaId != nil {
q = q.Where("area_id = ?", *areaId)
}
if err = q.Count(&total).Error; err != nil {
err = errors.Wrap(apperr.ErrEntityList, err.Error())
return
}
q = q.
Preload("Image").
Preload("States").
Expand Down Expand Up @@ -255,6 +254,49 @@ func (n *Entities) List(ctx context.Context, limit, offset int, orderBy, sort st
return
}

// ListPlain ...
func (n *Entities) ListPlain(ctx context.Context, limit, offset int, orderBy, sort string, autoLoad bool,
query, plugin *string, areaId *int64) (list []*Entity, total int64, err error) {

list = make([]*Entity, 0)
q := n.Db.WithContext(ctx).Model(Entity{})
if autoLoad {
q = q.Where("auto_load = ?", true)
}
if query != nil {
q = q.Where("id LIKE ?", "%"+*query+"%")
}
if plugin != nil {
q = q.Where("plugin_name = ?", *plugin)
}
if areaId != nil {
q = q.Where("area_id = ?", *areaId)
}
if err = q.Count(&total).Error; err != nil {
err = errors.Wrap(apperr.ErrEntityList, err.Error())
return
}
q = q.
Limit(limit).
Offset(offset)

if sort != "" && orderBy != "" {
q = q.Order(fmt.Sprintf("%s %s", sort, orderBy))
}

err = q.
WithContext(ctx).
Find(&list).
Error

if err != nil {
err = errors.Wrap(apperr.ErrEntityList, err.Error())
return
}

return
}

// GetByType ...
func (n *Entities) GetByType(ctx context.Context, t string, limit, offset int) (list []*Entity, err error) {

Expand Down
14 changes: 6 additions & 8 deletions db/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,15 @@ func (n Scripts) Delete(ctx context.Context, scriptId int64) (err error) {
// List ...
func (n *Scripts) List(ctx context.Context, limit, offset int, orderBy, sort string, query *string) (list []*Script, total int64, err error) {

if err = n.Db.WithContext(ctx).Model(Script{}).Count(&total).Error; err != nil {
err = errors.Wrap(apperr.ErrScriptList, err.Error())
return
}

list = make([]*Script, 0)
q := n.Db
q := n.Db.WithContext(ctx).Model(Script{})
if query != nil {
q = q.Where("name LIKE ?", "%"+*query+"%")
q = q.Where("name LIKE ? or source LIKE ?", "%"+*query+"%", "%"+*query+"%")
}
if err = q.Count(&total).Error; err != nil {
err = errors.Wrap(apperr.ErrScriptList, err.Error())
return
}

err = q.
Limit(limit).
Offset(offset).
Expand Down
2 changes: 1 addition & 1 deletion endpoint/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (n *EntityEndpoint) Update(ctx context.Context, params *m.Entity) (result *

// List ...
func (n *EntityEndpoint) List(ctx context.Context, pagination common.PageParams, query, plugin *string, areaId *int64) (entities []*m.Entity, total int64, err error) {
entities, total, err = n.adaptors.Entity.List(ctx, pagination.Limit, pagination.Offset, pagination.Order,
entities, total, err = n.adaptors.Entity.ListPlain(ctx, pagination.Limit, pagination.Offset, pagination.Order,
pagination.SortBy, false, query, plugin, areaId)
if err != nil {
return
Expand Down

0 comments on commit c2dec10

Please sign in to comment.