feat: demo 增加删除

This commit is contained in:
wangzhengkun 2022-08-12 14:23:41 +08:00
parent 80fb98e20c
commit 74556bcbd7
11 changed files with 64 additions and 42 deletions

View file

@ -67,3 +67,12 @@ func SuccessWithData(ctx *gin.Context, data interface{}) {
ctx.JSON(http.StatusOK, res)
ctx.Abort()
}
func GetParamID(c *gin.Context) (uint, error) {
idParam, ok := c.Params.Get("id")
if !ok {
return 0, errors.New("error name")
}
intNum, _ := strconv.Atoi(idParam)
return uint(intNum), nil
}

View file

@ -1,9 +1,6 @@
package v1
import (
"github.com/pkg/errors"
"strconv"
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/app/dto"
"github.com/1Panel-dev/1Panel/constant"
@ -96,7 +93,7 @@ func (b *BaseApi) PageUsers(c *gin.Context) {
}
func (b *BaseApi) DeleteUser(c *gin.Context) {
var req dto.OperationWithName
var req dto.BatchDeleteReq
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -106,7 +103,7 @@ func (b *BaseApi) DeleteUser(c *gin.Context) {
return
}
if err := userService.Delete(req.Name); err != nil {
if err := userService.BatchDelete(req.Ids); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
@ -123,11 +120,16 @@ func (b *BaseApi) UpdateUser(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
upMap := make(map[string]interface{})
upMap["email"] = req.Email
upMap["name"] = req.Name
if err := userService.Update(req.ID, upMap); err != nil {
if err := userService.Update(id, upMap); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
@ -135,14 +137,12 @@ func (b *BaseApi) UpdateUser(c *gin.Context) {
}
func (b *BaseApi) GetUserInfo(c *gin.Context) {
idParam, ok := c.Params.Get("id")
if !ok {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name"))
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
intNum, _ := strconv.Atoi(idParam)
user, err := userService.Get(uint(intNum))
user, err := userService.Get(id)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return

View file

@ -10,6 +10,10 @@ type OperationWithName struct {
Name string `json:"name" validate:"required"`
}
type BatchDeleteReq struct {
Ids []uint `json:"ids" validate:"required"`
}
type OperationWithNameAndType struct {
Name string `json:"name" validate:"required"`
Type string `json:"type" validate:"required"`

View file

@ -8,6 +8,8 @@ type ICommonRepo interface {
WithByID(id uint) DBOption
WithByName(name string) DBOption
WithOrderBy(orderStr string) DBOption
WithLikeName(name string) DBOption
WithIdsIn(ids []uint) DBOption
}
type CommonRepo struct{}
@ -35,3 +37,9 @@ func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
return g.Order(orderStr)
}
}
func (c *CommonRepo) WithIdsIn(ids []uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
}
}

View file

@ -25,6 +25,7 @@ type IUserService interface {
Delete(name string) error
Save(req model.User) error
Update(id uint, upMap map[string]interface{}) error
BatchDelete(ids []uint) error
}
func NewIUserService() IUserService {
@ -132,6 +133,10 @@ func (u *UserService) Delete(name string) error {
return userRepo.Delete(commonRepo.WithByName(name))
}
func (u *UserService) BatchDelete(ids []uint) error {
return userRepo.Delete(commonRepo.WithIdsIn(ids))
}
func (u *UserService) Save(req model.User) error {
return userRepo.Save(req)
}

View file

@ -16,7 +16,7 @@ func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) {
baseApi := v1.ApiGroupApp.BaseApi
{
withRecordRouter.POST("", baseApi.Register)
withRecordRouter.DELETE("", baseApi.DeleteUser)
withRecordRouter.POST("/del", baseApi.DeleteUser)
userRouter.POST("/search", baseApi.PageUsers)
userRouter.GET(":id", baseApi.GetUserInfo)
userRouter.POST(":id", baseApi.UpdateUser)

View file

@ -27,5 +27,5 @@ export const editUser = (params: User.User) => {
// * 批量删除用户
export const deleteUser = (params: { ids: number[] }) => {
return http.post(`/users/delete`, params);
return http.post(`/users/del`, params);
};

View file

@ -27,7 +27,7 @@ export const useDeleteData = <P = any, R = any>(
if (!res) return reject(false);
ElMessage({
type: 'success',
message: i18n.global.t('commons.msg.deleteSuccss'),
message: i18n.global.t('commons.msg.deleteSuccess'),
});
resolve(true);
});

View file

@ -21,7 +21,7 @@ const demoRouter = {
},
{
path: '/demos/table/:op/:id?',
name: 'DemoCreate',
name: 'DemoOperate',
props: true,
hidden: true,
component: () => import('@/views/demos/table/operate/index.vue'),

View file

@ -59,38 +59,34 @@ const openOperate = (row: User.User | null) => {
params.id = row.id;
}
router.push({ name: 'DemoCreate', params });
router.push({ name: 'DemoOperate', params });
};
const batchDelete = async (row: User.User | null) => {
let ids: Array<number> = [];
if (row === null) {
selects.value.forEach((item: User.User) => {
ids.push(item.id);
});
} else {
ids.push(row.id);
}
await useDeleteData(deleteUser, { ids: ids }, 'commons.msg.delete');
search();
};
const buttons = [
{
label: i18n.global.t('commons.button.edit'),
click: openOperate,
},
// {
// label: '',
// click: buttonClick,
// },
// {
// label: '',
// type: 'danger',
// click: buttonClick,
// },
// {
// label: '',
// click: buttonClick,
// },
// {
// label: '',
// click: buttonClick,
// },
{
label: i18n.global.t('commons.button.delete'),
type: 'danger',
click: batchDelete,
},
];
const batchDelete = async () => {
let ids: Array<number> = [];
selects.value.forEach((item: User.User) => {
ids.push(item.id);
});
await useDeleteData(deleteUser, { ids: ids }, 'commons.msg.delete');
};
const search = async () => {
userSearch.page = paginationConfig.page;

View file

@ -77,10 +77,10 @@ const submitForm = async (formEl: FormInstance | undefined) => {
const getUser = async (id: number) => {
const res = await getUserById(id);
demoForm.value = res.data;
console.log(demoForm);
};
onMounted(() => {
if (props.op == 'edit') {
console.log(props);
getUser(props.id).catch(() => {
router.back();
});