-
-
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.
Merge pull request #153 from SaintAngeLs/organizations_service
Organizations service - updated implementation
- Loading branch information
Showing
34 changed files
with
373 additions
and
76 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
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
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
16 changes: 16 additions & 0 deletions
16
...tions/src/MiniSpace.Services.Organizations.Application/Commands/CreateRootOrganization.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 CreateRootOrganization: ICommand | ||
{ | ||
public Guid OrganizationId { get; } | ||
public string Name { get; } | ||
|
||
public CreateRootOrganization(Guid organizationId, string name) | ||
{ | ||
OrganizationId = organizationId == Guid.Empty ? Guid.NewGuid() : organizationId; | ||
Name = name; | ||
} | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...niSpace.Services.Organizations.Application/Commands/Handlers/CreateOrganizationHandler.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.Entities; | ||
using MiniSpace.Services.Organizations.Core.Repositories; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Commands.Handlers | ||
{ | ||
public class CreateOrganizationHandler : ICommandHandler<CreateOrganization> | ||
{ | ||
private readonly IOrganizationRepository _organizationRepository; | ||
private readonly IAppContext _appContext; | ||
private readonly IMessageBroker _messageBroker; | ||
|
||
public CreateOrganizationHandler(IOrganizationRepository organizationRepository, IAppContext appContext, | ||
IMessageBroker messageBroker) | ||
{ | ||
_organizationRepository = organizationRepository; | ||
_appContext = appContext; | ||
_messageBroker = messageBroker; | ||
} | ||
|
||
public async Task HandleAsync(CreateOrganization 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 parent = root.GetSubOrganization(command.ParentId); | ||
if(parent is null) | ||
{ | ||
throw new ParentOrganizationNotFoundException(command.ParentId); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(command.Name)) | ||
{ | ||
throw new InvalidOrganizationNameException(command.Name); | ||
} | ||
|
||
var organization = new Organization(command.OrganizationId, command.Name); | ||
parent.AddSubOrganization(organization); | ||
await _organizationRepository.UpdateAsync(root); | ||
await _messageBroker.PublishAsync(new OrganizationCreated(organization.Id, organization.Name, parent.Id)); | ||
} | ||
} | ||
} |
27 changes: 14 additions & 13 deletions
27
...mmands/Handlers/AddOrganizationHandler.cs → ...Handlers/CreateRootOrganizationHandler.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 |
---|---|---|
@@ -1,41 +1,42 @@ | ||
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.Entities; | ||
using MiniSpace.Services.Organizations.Core.Repositories; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Commands.Handlers | ||
{ | ||
public class AddOrganizationHandler : ICommandHandler<AddOrganization> | ||
public class CreateRootOrganizationHandler : ICommandHandler<CreateRootOrganization> | ||
{ | ||
private readonly IOrganizationRepository _organizationRepository; | ||
private readonly IAppContext _appContext; | ||
private readonly IMessageBroker _messageBroker; | ||
|
||
public AddOrganizationHandler(IOrganizationRepository organizationRepository, IAppContext appContext) | ||
public CreateRootOrganizationHandler(IOrganizationRepository organizationRepository, IAppContext appContext, | ||
IMessageBroker messageBroker) | ||
{ | ||
_organizationRepository = organizationRepository; | ||
_appContext = appContext; | ||
_messageBroker = messageBroker; | ||
} | ||
|
||
public async Task HandleAsync(AddOrganization command, CancellationToken cancellationToken) | ||
public async Task HandleAsync(CreateRootOrganization command, CancellationToken cancellationToken) | ||
{ | ||
var identity = _appContext.Identity; | ||
if(identity.IsAuthenticated && !identity.IsAdmin) | ||
{ | ||
throw new Exceptions.UnauthorizedAccessException("admin"); | ||
} | ||
|
||
var organization = new Organization(command.OrganizationId, command.Name, command.ParentId); | ||
if(command.ParentId != Guid.Empty) | ||
|
||
if (string.IsNullOrWhiteSpace(command.Name)) | ||
{ | ||
var parent = await _organizationRepository.GetAsync(command.ParentId); | ||
if(parent is null) | ||
{ | ||
throw new ParentOrganizationNotFoundException(command.ParentId); | ||
} | ||
parent.MakeParent(); | ||
await _organizationRepository.UpdateAsync(parent); | ||
throw new InvalidOrganizationNameException(command.Name); | ||
} | ||
|
||
var organization = new Organization(command.OrganizationId, command.Name); | ||
await _organizationRepository.AddAsync(organization); | ||
await _messageBroker.PublishAsync(new RootOrganizationCreated(organization.Id, organization.Name)); | ||
} | ||
} | ||
} |
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
15 changes: 13 additions & 2 deletions
15
...anizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDetailsDto.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 |
---|---|---|
@@ -1,13 +1,24 @@ | ||
using System.Collections; | ||
using MiniSpace.Services.Organizations.Core.Entities; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.DTO | ||
{ | ||
public class OrganizationDetailsDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public Guid ParentId { get; set; } | ||
public bool IsLeaf { get; set; } | ||
public IEnumerable<Guid> Organizers { get; set; } | ||
|
||
public OrganizationDetailsDto() | ||
{ | ||
|
||
} | ||
|
||
public OrganizationDetailsDto(Organization organization) | ||
{ | ||
Id = organization.Id; | ||
Name = organization.Name; | ||
Organizers = organization.Organizers.Select(o => o.Id); | ||
} | ||
} | ||
} |
15 changes: 13 additions & 2 deletions
15
...ces.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDto.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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
using MiniSpace.Services.Organizations.Core.Entities; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.DTO | ||
{ | ||
public class OrganizationDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public Guid ParentId { get; set; } | ||
public bool IsLeaf { get; set; } | ||
|
||
public OrganizationDto() | ||
{ | ||
|
||
} | ||
|
||
public OrganizationDto (Organization organization) | ||
{ | ||
Id = organization.Id; | ||
Name = organization.Name; | ||
} | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
...anizations/src/MiniSpace.Services.Organizations.Application/Events/OrganizationCreated.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,18 @@ | ||
using Convey.CQRS.Events; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Events | ||
{ | ||
public class OrganizationCreated: IEvent | ||
{ | ||
public Guid OrganizationId { get; } | ||
public string Name { get; } | ||
public Guid ParentId { get; } | ||
|
||
public OrganizationCreated(Guid organizationId, string name, Guid parentId) | ||
{ | ||
OrganizationId = organizationId; | ||
Name = name; | ||
ParentId = parentId; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ations/src/MiniSpace.Services.Organizations.Application/Events/RootOrganizationCreated.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.Events; | ||
|
||
namespace MiniSpace.Services.Organizations.Application.Events | ||
{ | ||
public class RootOrganizationCreated: IEvent | ||
{ | ||
public Guid OrganizationId { get; } | ||
public string Name { get; } | ||
|
||
public RootOrganizationCreated(Guid organizationId, string name) | ||
{ | ||
OrganizationId = organizationId; | ||
Name = name; | ||
} | ||
} | ||
} |
Oops, something went wrong.