Skip to content

Commit

Permalink
(#87) add endpoint for removing an organizer from organization
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed Apr 26, 2024
1 parent 08a711a commit ac71fd0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public static async Task Main(string[] args)
.UseDispatcherEndpoints(endpoints => endpoints
.Get("", ctx => ctx.Response.WriteAsync(ctx.RequestServices.GetService<AppOptions>().Name))
.Get<GetOrganization, OrganizationDto>("organizations/{organizationId}")
.Get<GetOrganizerOrganizations, IEnumerable<OrganizationDto>>("organizations/user")
.Get<GetOrganizerOrganizations, IEnumerable<OrganizationDto>>("organizations/organizer/{organizerId}")
.Get<GetRootOrganizations, IEnumerable<OrganizationDto>>("organizations/root")
.Get<GetChildrenOrganizations, IEnumerable<OrganizationDto>>("organizations/{organizationId}/children")
.Post<AddOrganization>("organizations",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/root"))
.Post<AddOrganizerToOrganization>("organizations/user")
.Delete<RemoveUserFromOrganization>("organizations/user")
.Post<AddOrganizerToOrganization>("organizations/organizer")
.Delete<RemoveOrganizerFromOrganization>("organizations/organizer")
))
.UseLogging()
.Build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public AddOrganizerToOrganizationHandler(IOrganizationRepository organizationRep

public async Task HandleAsync(AddOrganizerToOrganization command, CancellationToken cancellationToken)
{
var identity = _appContext.Identity;
if (identity.IsAuthenticated && !identity.IsAdmin)
{
throw new Exceptions.UnauthorizedAccessException("admin");
}

var organization = await _organizationRepository.GetAsync(command.OrganizationId);
if (organization is null)
{
Expand All @@ -37,12 +43,6 @@ public async Task HandleAsync(AddOrganizerToOrganization command, CancellationTo
throw new OrganizerNotFoundException(command.OrganizerId);
}

var identity = _appContext.Identity;
if (identity.IsAuthenticated && !identity.IsAdmin)
{
throw new Exceptions.UnauthorizedAccessException(identity.Role);
}

organization.AddOrganizer(organizer);
await _organizationRepository.UpdateAsync(organization);
await _messageBroker.PublishAsync(new OrganizerAddedToOrganization(organization.Id, organizer.Id));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Convey.CQRS.Commands;
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 RemoveOrganizerFromOrganizationHandler : ICommandHandler<RemoveOrganizerFromOrganization>
{
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizerRepository _organizerRepository;
private readonly IAppContext _appContext;
private readonly IMessageBroker _messageBroker;

public RemoveOrganizerFromOrganizationHandler(IOrganizationRepository organizationRepository,
IOrganizerRepository organizerRepository, IAppContext appContext, IMessageBroker messageBroker)
{
_organizationRepository = organizationRepository;
_organizerRepository = organizerRepository;
_appContext = appContext;
_messageBroker = messageBroker;
}

public async Task HandleAsync(RemoveOrganizerFromOrganization command, CancellationToken cancellationToken)
{
var identity = _appContext.Identity;
if(identity.IsAuthenticated && !identity.IsAdmin)
{
throw new Exceptions.UnauthorizedAccessException("admin");
}

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

var organizer = await _organizerRepository.GetAsync(command.OrganizerId);
if(organizer is null)
{
throw new OrganizerNotFoundException(command.OrganizerId);
}

organization.RemoveOrganizer(organizer);
await _organizationRepository.UpdateAsync(organization);
}
}
}
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 RemoveOrganizerFromOrganization : ICommand
{
public Guid OrganizationId { get; }
public Guid OrganizerId { get; }

public RemoveOrganizerFromOrganization(Guid organizationId, Guid organizerId)
{
OrganizationId = organizationId;
OrganizerId = organizerId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public Organization(Guid id, string name, Guid parentId, bool isLeaf = true, IEn

public void RemoveOrganizer(Organizer organizer)
=> _organizers.Remove(organizer);

public void RemoveOrganizer(Guid organizerId)
{
var organizer = _organizers.SingleOrDefault(x => x.Id == organizerId);
if(organizer is null)
{
throw new OrganizerIsNotInOrganization(organizerId, Id);
}
RemoveOrganizer(organizer);
}

public void AddOrganizer(Organizer organizer)
{
Expand All @@ -36,6 +46,8 @@ public void AddOrganizer(Organizer organizer)
_organizers.Add(organizer);
}



public void MakeParent()
=> IsLeaf = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace MiniSpace.Services.Organizations.Core.Exceptions
{
public class OrganizerIsNotInOrganization : DomainException
{
public override string Code { get; } = "organizer_is_not_in_organization";
public Guid OrganizerId { get; }
public Guid OrganizationId { get; }

public OrganizerIsNotInOrganization(Guid organizerId, Guid organizationId)
: base($"Organizer with ID: '{organizerId}' is not in organization with ID: '{organizationId}'.")
{
OrganizerId = organizerId;
OrganizationId = organizationId;
}
}
}

0 comments on commit ac71fd0

Please sign in to comment.