From c2dec1006747b83c762170b40cd91fe6d02b4683 Mon Sep 17 00:00:00 2001 From: Filippov Alex Date: Fri, 5 Jan 2024 12:33:59 +0530 Subject: [PATCH] technical debt --- adaptors/entity.go | 17 +++++++++++++++ api/dto/script.go | 11 ++++++++-- db/entity.go | 54 ++++++++++++++++++++++++++++++++++++++++------ db/script.go | 14 ++++++------ endpoint/entity.go | 2 +- 5 files changed, 81 insertions(+), 17 deletions(-) diff --git a/adaptors/entity.go b/adaptors/entity.go index bb9873353..4dc7c1dfb 100644 --- a/adaptors/entity.go +++ b/adaptors/entity.go @@ -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) @@ -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) { diff --git a/api/dto/script.go b/api/dto/script.go index 1f2b2bfee..fdd641a10 100644 --- a/api/dto/script.go +++ b/api/dto/script.go @@ -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 diff --git a/db/entity.go b/db/entity.go index 9adca47e4..9c82e027c 100644 --- a/db/entity.go +++ b/db/entity.go @@ -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) } @@ -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"). @@ -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) { diff --git a/db/script.go b/db/script.go index db5d65211..70fbaedf5 100644 --- a/db/script.go +++ b/db/script.go @@ -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). diff --git a/endpoint/entity.go b/endpoint/entity.go index a1f4c901d..aaa879802 100644 --- a/endpoint/entity.go +++ b/endpoint/entity.go @@ -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