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

83 lines
2.6 KiB
Go
Raw Normal View History

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