Skip to content

Commit

Permalink
Merge pull request #153 from SaintAngeLs/organizations_service
Browse files Browse the repository at this point in the history
Organizations service - updated implementation
  • Loading branch information
eggwhat authored May 17, 2024
2 parents 466ed73 + 6b90192 commit 4b9329f
Show file tree
Hide file tree
Showing 34 changed files with 373 additions and 76 deletions.
20 changes: 13 additions & 7 deletions MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -609,23 +609,24 @@ modules:
use: downstream
downstream: organizations-service/organizations
auth: true

- upstream: /{organizationId}/children
method: POST
use: downstream
downstream: organizations-service/organizations/{organizationId}/children
auth: true

- upstream: /organizer/{organizationId}/organizer
- upstream: /{organizationId}/organizer
method: POST
use: downstream
downstream: organizations-service/organizations/{organizationId}/organizer
auth: true

- upstream: /organizer/{organizationId}/organizer/{organizerId}
- upstream: /{organizationId}/organizer/{organizerId}
method: DELETE
use: downstream
downstream: organizations-service/organizations/{organizationId}/organizer/{organizerId}
auth: true

- upstream: /
method: GET
use: downstream
downstream: organizations-service/organizations

- upstream: /{organizationId}
method: GET
Expand All @@ -647,6 +648,11 @@ modules:
use: downstream
downstream: organizations-service/organizations/{organizationId}/children

- upstream: /{organizationId}/children/all
method: GET
use: downstream
downstream: organizations-service/organizations/{organizationId}/children/all

- upstream: /organizer/{organizerId}
method: GET
use: downstream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ private void ChangeState(State state)
public bool IsOrganizer(Guid organizerId)
=> Organizer.Id == organizerId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ public static async Task Main(string[] args)
.Get<GetOrganizerOrganizations, IEnumerable<OrganizationDto>>("organizations/organizer/{organizerId}")
.Get<GetRootOrganizations, IEnumerable<OrganizationDto>>("organizations/root")
.Get<GetChildrenOrganizations, IEnumerable<OrganizationDto>>("organizations/{organizationId}/children")
.Post<AddOrganization>("organizations",
.Get<GetAllChildrenOrganizations, IEnumerable<Guid>>("organizations/{organizationId}/children/all")
.Post<CreateRootOrganization>("organizations",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/root"))
.Post<CreateOrganization>("organizations/{organizationId}/children",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/{cmd.OrganizationId}"))
.Post<AddOrganizerToOrganization>("organizations/{organizationId}/organizer")
.Delete<RemoveOrganizerFromOrganization>("organizations/{organizationId}/organizer/{organizerId}")
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ namespace MiniSpace.Services.Organizations.Application.Commands
{
public class AddOrganizerToOrganization: ICommand
{
public Guid RootOrganizationId { get; set; }
public Guid OrganizationId { get; set; }
public Guid OrganizerId { get; set; }

public AddOrganizerToOrganization(Guid organizationId, Guid organizerId)
public AddOrganizerToOrganization(Guid rootOrganizationId, Guid organizationId, Guid organizerId)
{
RootOrganizationId = rootOrganizationId;
OrganizationId = organizationId;
OrganizerId = organizerId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace MiniSpace.Services.Organizations.Application.Commands
{
public class AddOrganization: ICommand
public class CreateOrganization: ICommand
{
public Guid OrganizationId { get; }
public string Name { get; }
public Guid RootId { get; }
public Guid ParentId { get; }

public AddOrganization(Guid organizationId, string name, Guid parentId)
public CreateOrganization(Guid organizationId, string name, Guid rootId, Guid parentId)
{
OrganizationId = organizationId == Guid.Empty ? Guid.NewGuid() : organizationId;
Name = name;
RootId = rootId;
ParentId = parentId;
}
}
Expand Down
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ public async Task HandleAsync(AddOrganizerToOrganization command, CancellationTo
throw new Exceptions.UnauthorizedAccessException("admin");
}

var organization = await _organizationRepository.GetAsync(command.OrganizationId);
if (organization is null)
var root = await _organizationRepository.GetAsync(command.RootOrganizationId);
if (root is null)
{
throw new RootOrganizationNotFoundException(command.RootOrganizationId);
}

var organization = root.GetSubOrganization(command.OrganizationId);
if (organization == null)
{
throw new OrganizationNotFoundException(command.OrganizationId);
}
Expand All @@ -44,7 +50,7 @@ public async Task HandleAsync(AddOrganizerToOrganization command, CancellationTo
}

organization.AddOrganizer(command.OrganizerId);
await _organizationRepository.UpdateAsync(organization);
await _organizationRepository.UpdateAsync(root);
await _messageBroker.PublishAsync(new OrganizerAddedToOrganization(organization.Id, organizer.Id));
}
}
Expand Down
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));
}
}
}
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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ public async Task HandleAsync(RemoveOrganizerFromOrganization command, Cancellat
throw new Exceptions.UnauthorizedAccessException("admin");
}

var organization = await _organizationRepository.GetAsync(command.OrganizationId);
if(organization is null)
var root = await _organizationRepository.GetAsync(command.RootOrganizationId);
if (root is null)
{
throw new RootOrganizationNotFoundException(command.RootOrganizationId);
}

var organization = root.GetSubOrganization(command.OrganizationId);
if (organization == null)
{
throw new OrganizationNotFoundException(command.OrganizationId);
}
Expand All @@ -42,7 +48,7 @@ public async Task HandleAsync(RemoveOrganizerFromOrganization command, Cancellat
}

organization.RemoveOrganizer(organizer.Id);
await _organizationRepository.UpdateAsync(organization);
await _organizationRepository.UpdateAsync(root);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ namespace MiniSpace.Services.Organizations.Application.Commands
{
public class RemoveOrganizerFromOrganization : ICommand
{
public Guid RootOrganizationId { get; set; }
public Guid OrganizationId { get; set; }
public Guid OrganizerId { get; set; }

public RemoveOrganizerFromOrganization(Guid organizationId, Guid organizerId)
public RemoveOrganizerFromOrganization(Guid rootOrganizationId, Guid organizationId, Guid organizerId)
{
RootOrganizationId = rootOrganizationId;
OrganizationId = organizationId;
OrganizerId = organizerId;
}
Expand Down
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);
}
}
}
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;
}
}
}

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;
}
}
}
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;
}
}
}
Loading

0 comments on commit 4b9329f

Please sign in to comment.