-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#87) add endpoint for removing organization
- Loading branch information
Showing
8 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...nizations/src/MiniSpace.Services.Organizations.Application/Commands/DeleteOrganization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Convey.CQRS.Commands; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Commands | ||
{ | ||
public class DeleteOrganization: ICommand | ||
{ | ||
public Guid OrganizationId { get; set; } | ||
public Guid RootId { get; } | ||
|
||
public DeleteOrganization(Guid organizationId, Guid rootId) | ||
{ | ||
OrganizationId = organizationId; | ||
RootId = rootId; | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...niSpace.Services.Organizations.Application/Commands/Handlers/DeleteOrganizationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Convey.CQRS.Commands; | ||
using MiniSpace.Services.Organizations.Application.Events; | ||
using MiniSpace.Services.Organizations.Application.Exceptions; | ||
using MiniSpace.Services.Organizations.Application.Services; | ||
using MiniSpace.Services.Organizations.Core.Repositories; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Commands.Handlers | ||
{ | ||
public class DeleteOrganizationHandler:ICommandHandler<DeleteOrganization> | ||
{ | ||
private readonly IOrganizationRepository _organizationRepository; | ||
private readonly IAppContext _appContext; | ||
private readonly IMessageBroker _messageBroker; | ||
|
||
public DeleteOrganizationHandler(IOrganizationRepository organizationRepository, IAppContext appContext, IMessageBroker messageBroker) | ||
{ | ||
_organizationRepository = organizationRepository; | ||
_appContext = appContext; | ||
_messageBroker = messageBroker; | ||
} | ||
|
||
public async Task HandleAsync(DeleteOrganization command, CancellationToken cancellationToken) | ||
{ | ||
var identity = _appContext.Identity; | ||
if(identity.IsAuthenticated && !identity.IsAdmin) | ||
{ | ||
throw new Exceptions.UnauthorizedAccessException("admin"); | ||
} | ||
|
||
var root = await _organizationRepository.GetAsync(command.RootId); | ||
if(root is null) | ||
{ | ||
throw new RootOrganizationNotFoundException(command.RootId); | ||
} | ||
|
||
var organization = root.GetSubOrganization(command.OrganizationId); | ||
if(organization is null) | ||
{ | ||
throw new OrganizationNotFoundException(command.OrganizationId); | ||
} | ||
|
||
if (root.Id.Equals(organization.Id)) | ||
{ | ||
await _organizationRepository.DeleteAsync(root.Id); | ||
} | ||
else | ||
{ | ||
root.RemoveChildOrganization(organization); | ||
await _organizationRepository.UpdateAsync(root); | ||
} | ||
|
||
await _messageBroker.PublishAsync(new OrganizationDeleted(organization.Id)); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...anizations/src/MiniSpace.Services.Organizations.Application/Events/OrganizationDeleted.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Convey.CQRS.Events; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Events | ||
{ | ||
public class OrganizationDeleted:IEvent | ||
{ | ||
public Guid OrganizationId { get; } | ||
|
||
public OrganizationDeleted(Guid organizationId) | ||
{ | ||
OrganizationId = organizationId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...MiniSpace.Services.Organizations.Core/Exceptions/ParentOfOrganizationNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace MiniSpace.Services.Organizations.Core.Exceptions | ||
{ | ||
public class ParentOfOrganizationNotFoundException : DomainException | ||
{ | ||
public override string Code { get; } = "parent_of_organization_not_found"; | ||
public Guid ChildId { get; } | ||
|
||
public ParentOfOrganizationNotFoundException(Guid childId) : base( | ||
$"Parent organization was not found for child organization with ID: '{childId}'.") | ||
{ | ||
ChildId = childId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters