69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/domain/site"
|
|
"context"
|
|
)
|
|
|
|
type DeleteSiteUseCase struct {
|
|
siteRepository site.Repository
|
|
recorder audit.Recorder
|
|
}
|
|
|
|
type DeleteSiteCommand struct {
|
|
ID uint32
|
|
ReassignTo *uint32
|
|
ReassignInvalid bool
|
|
}
|
|
|
|
func NewDeleteSiteUseCase(siteRepository site.Repository, recorder audit.Recorder) *DeleteSiteUseCase {
|
|
return &DeleteSiteUseCase{siteRepository, recorder}
|
|
}
|
|
|
|
func (u *DeleteSiteUseCase) Delete(ctx context.Context, command DeleteSiteCommand) error {
|
|
target, err := u.siteRepository.Get(command.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
event := audit.NewEvent(audit.ActionSiteDelete, audit.CategorySites)
|
|
event.Target = siteTarget(target)
|
|
if err := u.delete(target, command); err != nil {
|
|
recordFailure(ctx, u.recorder, event, err)
|
|
return err
|
|
}
|
|
u.recorder.Record(ctx, event)
|
|
return nil
|
|
}
|
|
|
|
func (u *DeleteSiteUseCase) delete(target site.Site, command DeleteSiteCommand) error {
|
|
if target.IsDefault {
|
|
return site.ErrDefaultSite
|
|
}
|
|
if target.ArticleCount == 0 {
|
|
return u.siteRepository.DeleteMovingArticles(target.ID, nil)
|
|
}
|
|
destination, err := u.destination(target, command)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return u.siteRepository.DeleteMovingArticles(target.ID, &destination)
|
|
}
|
|
|
|
func (u *DeleteSiteUseCase) destination(target site.Site, command DeleteSiteCommand) (uint32, error) {
|
|
if command.ReassignInvalid {
|
|
return 0, site.ErrInvalidReassign
|
|
}
|
|
if command.ReassignTo == nil {
|
|
return 0, site.ErrSiteHasArticles
|
|
}
|
|
if *command.ReassignTo == 0 || *command.ReassignTo == target.ID {
|
|
return 0, site.ErrInvalidReassign
|
|
}
|
|
if _, err := u.siteRepository.Get(*command.ReassignTo); err != nil {
|
|
return 0, err
|
|
}
|
|
return *command.ReassignTo, nil
|
|
}
|