Skip to content

Commit

Permalink
feat: support duplicate one existing Route
Browse files Browse the repository at this point in the history
  • Loading branch information
ezioruan committed Mar 5, 2021
1 parent 938b2b9 commit fb85518
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
48 changes: 30 additions & 18 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ func generateLuaCode(script map[string]interface{}) (string, error) {

func (h *Handler) Create(c droplet.Context) (interface{}, error) {
input := c.Input().(*entity.Route)
// check duplicate name
if err := h.checkDuplicateName(c, input.Name, ""); err != nil {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
consts.InvalidParam(err.Error())
}

//check depend
if input.ServiceID != nil {
serviceID := utils.InterfaceToString(input.ServiceID)
Expand Down Expand Up @@ -521,6 +527,28 @@ func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
return nil, nil
}

func (h *Handler) checkDuplicateName(c droplet.Context, name, exclude string) error {
ret, err := h.routeStore.List(c.Context(), store.ListInput{
Predicate: func(obj interface{}) bool {
r := obj.(*entity.Route)
if r.Name == name && r.ID != exclude {
return true
}

return false
},
PageSize: 0,
PageNumber: 0,
})
if err != nil {
return err
}
if ret.TotalSize > 0 {
return fmt.Errorf("Route name %s is reduplicate", name)
}
return nil
}

type ExistCheckInput struct {
Name string `auto_read:"name,query"`
Exclude string `auto_read:"exclude,query"`
Expand Down Expand Up @@ -558,25 +586,9 @@ func (h *Handler) Exist(c droplet.Context) (interface{}, error) {
name := input.Name
exclude := input.Exclude

ret, err := h.routeStore.List(c.Context(), store.ListInput{
Predicate: func(obj interface{}) bool {
r := obj.(*entity.Route)
if r.Name == name && r.ID != exclude {
return true
}

return false
},
PageSize: 0,
PageNumber: 0,
})
if err != nil {
return nil, err
}

if ret.TotalSize > 0 {
if err := h.checkDuplicateName(c, name, exclude); err != nil {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
consts.InvalidParam("Route name is reduplicate")
consts.InvalidParam(err.Error())
}

return nil, nil
Expand Down
4 changes: 4 additions & 0 deletions web/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const routes = [
path: '/routes/:rid/edit',
component: './Route/Create',
},
{
path: '/routes/:rid/duplicate',
component: './Route/Create',
},
{
path: '/ssl/:id/edit',
component: './SSL/Create',
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/en-US/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default {
'component.global.save': 'Save',
'component.global.edit': 'Edit',
'component.global.view': 'View',
'component.global.duplicate': 'Duplicate',
'component.global.manage': 'Manage',
'component.global.update': 'Update',
'component.global.get': 'Get',
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh-CN/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default {
'component.global.save': '保存',
'component.global.edit': '编辑',
'component.global.view': '查看',
'component.global.duplicate': '复制',
'component.global.manage': '管理',
'component.global.update': '更新',
'component.global.get': '获取',
Expand Down
11 changes: 5 additions & 6 deletions web/src/pages/Route/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const Page: React.FC<Props> = (props) => {
};

useEffect(() => {
if (props.route.path.indexOf('edit') !== -1) {
if (props.route.path.indexOf('edit') !== -1 || props.route.path.indexOf('duplicate') !== -1) {
setupRoute(props.match.params.rid);
} else {
onReset();
Expand Down Expand Up @@ -226,7 +226,7 @@ const Page: React.FC<Props> = (props) => {
redirectOption === 'forceHttps' && filterHosts.length !== 0
? checkHostWithSSL(hosts)
: Promise.resolve(),
checkUniqueName(value.name, (props as any).match.params.rid || ''),
checkUniqueName(value.name, props.route.path.indexOf('edit') > 0 ? (props as any).match.params.rid : ''),
]).then(() => {
setStep(nextStep);
});
Expand Down Expand Up @@ -262,10 +262,9 @@ const Page: React.FC<Props> = (props) => {
return (
<>
<PageHeaderWrapper
title={`${(props as any).match.params.rid
? formatMessage({ id: 'component.global.edit' })
: formatMessage({ id: 'component.global.create' })
} ${formatMessage({ id: 'menu.routes' })}`}
title={`${
formatMessage({ id: `component.global.${props.route.path.split('/').slice(-1)[0]}`})
} ${formatMessage({ id: 'menu.routes' })}`}
>
<Card bordered={false}>
<Steps current={step - 1} className={styles.steps}>
Expand Down
3 changes: 3 additions & 0 deletions web/src/pages/Route/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ const Page: React.FC = () => {
}}>
{formatMessage({ id: 'component.global.view' })}
</Button>
<Button type="primary" onClick={() => history.push(`/routes/${record.id}/duplicate`)}>
{formatMessage({ id: 'component.global.duplicate' })}
</Button>
<Popconfirm
title={formatMessage({ id: 'component.global.popconfirm.title.delete' })}
onConfirm={() => {
Expand Down

0 comments on commit fb85518

Please sign in to comment.