2022-08-30 18:49:07 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-08-30 18:49:07 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommandService struct{}
|
|
|
|
|
|
|
|
type ICommandService interface {
|
2023-01-04 22:31:51 +08:00
|
|
|
List() ([]dto.CommandInfo, error)
|
2022-08-30 18:49:07 +08:00
|
|
|
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
|
2022-09-01 10:25:38 +08:00
|
|
|
Create(commandDto dto.CommandOperate) error
|
2022-08-30 18:49:07 +08:00
|
|
|
Update(id uint, upMap map[string]interface{}) error
|
2022-09-01 10:25:38 +08:00
|
|
|
Delete(ids []uint) error
|
2022-08-30 18:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewICommandService() ICommandService {
|
|
|
|
return &CommandService{}
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
func (u *CommandService) List() ([]dto.CommandInfo, error) {
|
2022-08-30 18:49:07 +08:00
|
|
|
commands, err := commandRepo.GetList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, constant.ErrRecordNotFound
|
|
|
|
}
|
2023-01-04 22:31:51 +08:00
|
|
|
var dtoCommands []dto.CommandInfo
|
|
|
|
for _, command := range commands {
|
|
|
|
var item dto.CommandInfo
|
|
|
|
if err := copier.Copy(&item, &command); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoCommands = append(dtoCommands, item)
|
|
|
|
}
|
|
|
|
return dtoCommands, err
|
2022-08-30 18:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CommandService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) {
|
2022-08-31 23:16:10 +08:00
|
|
|
total, commands, err := commandRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info))
|
2022-08-30 18:49:07 +08:00
|
|
|
var dtoCommands []dto.CommandInfo
|
|
|
|
for _, command := range commands {
|
|
|
|
var item dto.CommandInfo
|
|
|
|
if err := copier.Copy(&item, &command); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoCommands = append(dtoCommands, item)
|
|
|
|
}
|
|
|
|
return total, dtoCommands, err
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:25:38 +08:00
|
|
|
func (u *CommandService) Create(commandDto dto.CommandOperate) error {
|
2022-08-30 18:49:07 +08:00
|
|
|
command, _ := commandRepo.Get(commonRepo.WithByName(commandDto.Name))
|
|
|
|
if command.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&command, &commandDto); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
if err := commandRepo.Create(&command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:25:38 +08:00
|
|
|
func (u *CommandService) Delete(ids []uint) error {
|
|
|
|
if len(ids) == 1 {
|
|
|
|
command, _ := commandRepo.Get(commonRepo.WithByID(ids[0]))
|
|
|
|
if command.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
return commandRepo.Delete(commonRepo.WithByID(ids[0]))
|
2022-08-30 18:49:07 +08:00
|
|
|
}
|
2022-09-01 10:25:38 +08:00
|
|
|
return commandRepo.Delete(commonRepo.WithIdsIn(ids))
|
2022-08-30 18:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CommandService) Update(id uint, upMap map[string]interface{}) error {
|
|
|
|
return commandRepo.Update(id, upMap)
|
|
|
|
}
|