From c5b2eddd3f3056b4da6a910283f4a4ad1468937c Mon Sep 17 00:00:00 2001 From: ssongliu Date: Mon, 29 Aug 2022 18:44:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/host.go | 4 ++-- backend/app/dto/host.go | 5 +++++ backend/app/repo/host.go | 9 +++++++++ backend/app/service/host.go | 6 +++--- frontend/src/api/interface/host.ts | 5 ++++- frontend/src/api/modules/host.ts | 4 ++-- frontend/src/lang/modules/en.ts | 1 + frontend/src/lang/modules/zh.ts | 1 + frontend/src/views/terminal/index.vue | 29 +++++++++++++++++++-------- 9 files changed, 48 insertions(+), 16 deletions(-) diff --git a/backend/app/api/v1/host.go b/backend/app/api/v1/host.go index 92d29586e..73fdf872c 100644 --- a/backend/app/api/v1/host.go +++ b/backend/app/api/v1/host.go @@ -27,13 +27,13 @@ func (b *BaseApi) Create(c *gin.Context) { } func (b *BaseApi) PageHosts(c *gin.Context) { - var req dto.PageInfo + var req dto.SearchWithPage if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - total, list, err := hostService.Page(req) + total, list, err := hostService.Search(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/dto/host.go b/backend/app/dto/host.go index c64017428..8fb5baa30 100644 --- a/backend/app/dto/host.go +++ b/backend/app/dto/host.go @@ -14,6 +14,11 @@ type HostCreate struct { Description string `json:"description"` } +type SearchWithPage struct { + PageInfo + Info string `json:"info"` +} + type HostInfo struct { ID uint `json:"id"` CreatedAt time.Time `json:"createdAt"` diff --git a/backend/app/repo/host.go b/backend/app/repo/host.go index 403a59dde..dec22e81c 100644 --- a/backend/app/repo/host.go +++ b/backend/app/repo/host.go @@ -3,6 +3,7 @@ package repo import ( "github.com/1Panel-dev/1Panel/app/model" "github.com/1Panel-dev/1Panel/global" + "gorm.io/gorm" ) type HostRepo struct{} @@ -10,6 +11,7 @@ type HostRepo struct{} type IHostRepo interface { Get(opts ...DBOption) (model.Host, error) Page(limit, offset int, opts ...DBOption) (int64, []model.Host, error) + WithByInfo(info string) DBOption Create(host *model.Host) error Update(id uint, vars map[string]interface{}) error Delete(opts ...DBOption) error @@ -41,6 +43,13 @@ func (u *HostRepo) Page(page, size int, opts ...DBOption) (int64, []model.Host, return count, hosts, err } +func (c *HostRepo) WithByInfo(info string) DBOption { + return func(g *gorm.DB) *gorm.DB { + infoStr := "%" + info + "%" + return g.Where("name LIKE ? OR addr LIKE ?", infoStr, infoStr) + } +} + func (u *HostRepo) Create(host *model.Host) error { return global.DB.Create(host).Error } diff --git a/backend/app/service/host.go b/backend/app/service/host.go index 2afa1ef2d..b3547c4f5 100644 --- a/backend/app/service/host.go +++ b/backend/app/service/host.go @@ -12,7 +12,7 @@ type HostService struct{} type IHostService interface { GetConnInfo(id uint) (*model.Host, error) - Page(search dto.PageInfo) (int64, interface{}, error) + Search(search dto.SearchWithPage) (int64, interface{}, error) Create(hostDto dto.HostCreate) (*dto.HostInfo, error) Update(id uint, upMap map[string]interface{}) error BatchDelete(ids []uint) error @@ -30,8 +30,8 @@ func (u *HostService) GetConnInfo(id uint) (*model.Host, error) { return &host, err } -func (u *HostService) Page(search dto.PageInfo) (int64, interface{}, error) { - total, hosts, err := hostRepo.Page(search.Page, search.PageSize) +func (u *HostService) Search(search dto.SearchWithPage) (int64, interface{}, error) { + total, hosts, err := hostRepo.Page(search.Page, search.PageSize, hostRepo.WithByInfo(search.Info)) var dtoHosts []dto.HostInfo for _, host := range hosts { var item dto.HostInfo diff --git a/frontend/src/api/interface/host.ts b/frontend/src/api/interface/host.ts index 4ae3cd6d8..c3ccee7fa 100644 --- a/frontend/src/api/interface/host.ts +++ b/frontend/src/api/interface/host.ts @@ -1,4 +1,4 @@ -import { CommonModel } from '.'; +import { CommonModel, ReqPage } from '.'; export namespace Host { export interface Host extends CommonModel { @@ -21,4 +21,7 @@ export namespace Host { description: string; } + export interface ReqSearchWithPage extends ReqPage { + info?: string; + } } diff --git a/frontend/src/api/modules/host.ts b/frontend/src/api/modules/host.ts index 9bc117c6a..66cf601ca 100644 --- a/frontend/src/api/modules/host.ts +++ b/frontend/src/api/modules/host.ts @@ -1,8 +1,8 @@ import http from '@/api'; -import { ResPage, ReqPage } from '../interface'; +import { ResPage } from '../interface'; import { Host } from '../interface/host'; -export const getHostList = (params: ReqPage) => { +export const getHostList = (params: Host.ReqSearchWithPage) => { return http.post>(`/hosts/search`, params); }; diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index b632e0a85..4541178d4 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -2,6 +2,7 @@ export default { commons: { button: { create: 'Create', + add: 'Add', delete: 'Delete', edit: 'Edit', confirm: 'Confirm', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index d6f36e70a..7cb142f60 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -2,6 +2,7 @@ export default { commons: { button: { create: '创建', + add: '添加', delete: '删除', edit: '编辑', confirm: '确认', diff --git a/frontend/src/views/terminal/index.vue b/frontend/src/views/terminal/index.vue index 15500bff8..186cc767a 100644 --- a/frontend/src/views/terminal/index.vue +++ b/frontend/src/views/terminal/index.vue @@ -37,7 +37,12 @@ - {{ $t('terminal.addHost') }} + + + +
- + - + - + - + @@ -107,10 +112,10 @@ - + - +