diff options
Diffstat (limited to 'services/domain/tld.go')
| -rw-r--r-- | services/domain/tld.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/services/domain/tld.go b/services/domain/tld.go index d9140b7..2cb7c05 100644 --- a/services/domain/tld.go +++ b/services/domain/tld.go @@ -5,6 +5,7 @@ import ( domainModel "dove/models/domain" domainRepo "dove/repositories/domain" + mailRepo "dove/repositories/mail" "dove/utils/shortcuts" "dove/utils/validate" ) @@ -13,6 +14,14 @@ type CreateTLDRequest struct { Name string `form:"name"` } +type UpdateTLDRequest struct { + Name string `form:"name"` +} + +type EditTLDFormResponse struct { + TLD domainModel.TLD `json:"tld"` +} + func AllTLDs() []domainModel.TLD { return domainRepo.AllTLDs() } @@ -41,6 +50,58 @@ func CreateTLD(request CreateTLDRequest) *shortcuts.Error { return nil } +func EditTLDFormData(tldID uint) (*EditTLDFormResponse, *shortcuts.Error) { + tld := domainRepo.FindTLDByID(tldID) + if tld == nil { + return nil, shortcuts.ServiceError(shortcuts.NotFound, TLDNotFound) + } + + if tld.IsDefault { + return nil, shortcuts.ServiceError(shortcuts.Forbidden, TLDProtected) + } + + return &EditTLDFormResponse{TLD: *tld}, nil +} + +func UpdateTLD(tldID uint, request UpdateTLDRequest) *shortcuts.Error { + tld := domainRepo.FindTLDByID(tldID) + if tld == nil { + return shortcuts.ServiceError(shortcuts.NotFound, TLDNotFound) + } + + if tld.IsDefault { + return shortcuts.ServiceError(shortcuts.Forbidden, TLDProtected) + } + + newName := strings.TrimSpace(strings.ToLower(request.Name)) + + switch { + case newName == "": + return shortcuts.ServiceError(shortcuts.BadRequest, TLDNameRequired) + case !validate.DNSLabel(newName): + return shortcuts.ServiceError(shortcuts.BadRequest, TLDNameInvalid) + } + + if newName != tld.Name { + if domainRepo.FindTLDByName(newName) != nil { + return shortcuts.ServiceError(shortcuts.Unprocessable, TLDAlreadyExists) + } + } + + tld.Name = newName + + if updateError := domainRepo.UpdateTLD(tld); updateError != nil { + return shortcuts.ServiceError(shortcuts.Internal, TLDUpdateFailed) + } + + domains := domainRepo.FindDomainsByTLDID(tld.ID) + for _, domain := range domains { + mailRepo.RebuildMailboxAddressesByDomainID(domain.ID) + } + + return nil +} + func DeleteTLD(name string) *shortcuts.Error { tld := domainRepo.FindTLDByName(name) |
