2022-11-10 17:44:38 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-12-14 15:26:51 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2022-11-10 17:44:38 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-12-05 15:50:53 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-11-10 17:44:38 +08:00
|
|
|
"gopkg.in/square/go-jose.v2/json"
|
|
|
|
)
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
type WebsiteDnsAccountService struct {
|
2022-11-10 17:44:38 +08:00
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteDnsAccountService) Page(search dto.PageInfo) (int64, []response.WebsiteDnsAccountDTO, error) {
|
2022-11-10 17:44:38 +08:00
|
|
|
total, accounts, err := websiteDnsRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
|
2022-12-14 15:26:51 +08:00
|
|
|
var accountDTOs []response.WebsiteDnsAccountDTO
|
2022-11-10 17:44:38 +08:00
|
|
|
for _, account := range accounts {
|
|
|
|
auth := make(map[string]string)
|
|
|
|
_ = json.Unmarshal([]byte(account.Authorization), &auth)
|
2022-12-14 15:26:51 +08:00
|
|
|
accountDTOs = append(accountDTOs, response.WebsiteDnsAccountDTO{
|
2022-11-10 17:44:38 +08:00
|
|
|
WebsiteDnsAccount: account,
|
|
|
|
Authorization: auth,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return total, accountDTOs, err
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteDnsAccountService) Create(create request.WebsiteDnsAccountCreate) (request.WebsiteDnsAccountCreate, error) {
|
2023-02-20 17:09:40 +08:00
|
|
|
exist, _ := websiteDnsRepo.GetFirst(commonRepo.WithByName(create.Name))
|
|
|
|
if exist != nil {
|
|
|
|
return request.WebsiteDnsAccountCreate{}, buserr.New(constant.ErrNameIsExist)
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:44:38 +08:00
|
|
|
authorization, err := json.Marshal(create.Authorization)
|
|
|
|
if err != nil {
|
2022-12-14 15:26:51 +08:00
|
|
|
return request.WebsiteDnsAccountCreate{}, err
|
2022-11-10 17:44:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := websiteDnsRepo.Create(model.WebsiteDnsAccount{
|
|
|
|
Name: create.Name,
|
|
|
|
Type: create.Type,
|
|
|
|
Authorization: string(authorization),
|
|
|
|
}); err != nil {
|
2022-12-14 15:26:51 +08:00
|
|
|
return request.WebsiteDnsAccountCreate{}, err
|
2022-11-10 17:44:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return create, nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteDnsAccountService) Update(update request.WebsiteDnsAccountUpdate) (request.WebsiteDnsAccountUpdate, error) {
|
2022-11-10 17:44:38 +08:00
|
|
|
authorization, err := json.Marshal(update.Authorization)
|
|
|
|
if err != nil {
|
2022-12-14 15:26:51 +08:00
|
|
|
return request.WebsiteDnsAccountUpdate{}, err
|
2022-11-10 17:44:38 +08:00
|
|
|
}
|
2023-02-20 17:24:26 +08:00
|
|
|
exists, _ := websiteDnsRepo.List(commonRepo.WithByName(update.Name))
|
|
|
|
for _, exist := range exists {
|
|
|
|
if exist.ID != update.ID {
|
|
|
|
return request.WebsiteDnsAccountUpdate{}, buserr.New(constant.ErrNameIsExist)
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 17:44:38 +08:00
|
|
|
if err := websiteDnsRepo.Save(model.WebsiteDnsAccount{
|
|
|
|
BaseModel: model.BaseModel{
|
|
|
|
ID: update.ID,
|
|
|
|
},
|
|
|
|
Name: update.Name,
|
|
|
|
Type: update.Type,
|
|
|
|
Authorization: string(authorization),
|
|
|
|
}); err != nil {
|
2022-12-14 15:26:51 +08:00
|
|
|
return request.WebsiteDnsAccountUpdate{}, err
|
2022-11-10 17:44:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return update, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteDnsAccountService) Delete(id uint) error {
|
2022-12-05 15:50:53 +08:00
|
|
|
if ssls, _ := websiteSSLRepo.List(websiteSSLRepo.WithByDnsAccountId(id)); len(ssls) > 0 {
|
|
|
|
return buserr.New(constant.ErrAccountCannotDelete)
|
|
|
|
}
|
2022-11-10 17:44:38 +08:00
|
|
|
return websiteDnsRepo.DeleteBy(commonRepo.WithByID(id))
|
|
|
|
}
|