2022-10-28 09:04:57 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-12-13 09:20:13 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
2022-10-28 09:04:57 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-02-20 09:24:26 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-28 09:04:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebsiteGroupService struct {
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteGroupService) CreateGroup(create request.WebsiteGroupCreate) error {
|
2023-02-20 09:24:26 +00:00
|
|
|
groups, _ := websiteGroupRepo.GetBy(commonRepo.WithByName(create.Name))
|
|
|
|
if len(groups) > 0 {
|
|
|
|
return buserr.New(constant.ErrNameIsExist)
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
return websiteGroupRepo.Create(&model.WebsiteGroup{
|
2022-10-28 09:04:57 +00:00
|
|
|
Name: create.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteGroupService) GetGroups() ([]model.WebsiteGroup, error) {
|
2022-10-28 09:04:57 +00:00
|
|
|
return websiteGroupRepo.GetBy()
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteGroupService) UpdateGroup(update request.WebsiteGroupUpdate) error {
|
2022-11-25 10:59:49 +00:00
|
|
|
if update.Default {
|
|
|
|
if err := websiteGroupRepo.CancelDefault(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
return websiteGroupRepo.Save(&model.WebsiteGroup{
|
2022-11-25 10:59:49 +00:00
|
|
|
BaseModel: model.BaseModel{
|
|
|
|
ID: update.ID,
|
|
|
|
},
|
|
|
|
Name: update.Name,
|
|
|
|
Default: true,
|
|
|
|
})
|
|
|
|
} else {
|
2023-02-20 09:24:26 +00:00
|
|
|
exists, _ := websiteGroupRepo.GetBy(commonRepo.WithByName(update.Name))
|
|
|
|
for _, exist := range exists {
|
|
|
|
if exist.ID != update.ID {
|
|
|
|
return buserr.New(constant.ErrNameIsExist)
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
return websiteGroupRepo.Save(&model.WebsiteGroup{
|
2022-11-25 10:59:49 +00:00
|
|
|
BaseModel: model.BaseModel{
|
|
|
|
ID: update.ID,
|
|
|
|
},
|
|
|
|
Name: update.Name,
|
|
|
|
})
|
|
|
|
}
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteGroupService) DeleteGroup(id uint) error {
|
|
|
|
return websiteGroupRepo.DeleteBy(commonRepo.WithByID(id))
|
|
|
|
}
|