1Panel/backend/app/service/website_group.go

62 lines
1.6 KiB
Go
Raw Normal View History

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