Skip to content

Commit

Permalink
(#87) add events with handlers for creating and deleting organizers
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed Apr 26, 2024
1 parent c5636cb commit 943946c
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using MiniSpace.Services.Organizations.Application;
using MiniSpace.Services.Organizations.Application.Commands;
using MiniSpace.Services.Organizations.Infrastructure;

namespace MiniSpace.Services.Organizations.Api
Expand All @@ -31,9 +32,8 @@ public static async Task Main(string[] args)
//.Get<GetUserOrganizations, IEnumerable<>>("organizations/user")
//.Get<GetRootOrganizations, IEnumerable<>>("organizations/root")
//.Get<GetChildrenOrganizations, IEnumerable<>>("organizations/{organizationId}/children")
//.Get<GetOrganizers, IEnumerable<>>("organizers")
//.Post<AddOrganization>("organizations",
// afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/{cmd.OrganizationId}"))
.Post<AddOrganization>("organizations",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/root"))
//.Post<AddUserToOrganization>("organizations/user")
//.Delete<RemoveUserFromOrganization>("organizations/user")
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class OrganizationDto
public Guid Id { get; set; }
public string Name { get; set; }
public Guid ParentId { get; set; }
public int MemberCount { get; set; }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Convey.CQRS.Events;
using MiniSpace.Services.Organizations.Application.Exceptions;
using MiniSpace.Services.Organizations.Core.Entities;
using MiniSpace.Services.Organizations.Core.Repositories;

namespace MiniSpace.Services.Organizations.Application.Events.External.Handlers
{
public class OrganizerRightsGrantedHandler : IEventHandler<OrganizerRightsGranted>
{
private readonly IOrganizerRepository _organizerRepository;

public OrganizerRightsGrantedHandler(IOrganizerRepository organizerRepository)
{
_organizerRepository = organizerRepository;
}

public async Task HandleAsync(OrganizerRightsGranted @event, CancellationToken cancellationToken)
{
if (await _organizerRepository.ExistsAsync(@event.UserId))
{
throw new OrganizerAlreadyAddedException(@event.UserId);
}

await _organizerRepository.AddAsync(new Organizer(@event.UserId));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Convey.CQRS.Events;
using MiniSpace.Services.Organizations.Application.Exceptions;
using MiniSpace.Services.Organizations.Core.Repositories;

namespace MiniSpace.Services.Organizations.Application.Events.External.Handlers
{
public class OrganizerRightsRevokedHandler : IEventHandler<OrganizerRightsRevoked>
{
private readonly IOrganizerRepository _organizerRepository;
private readonly IOrganizationRepository _organizationRepository;

public OrganizerRightsRevokedHandler(IOrganizerRepository organizerRepository, IOrganizationRepository organizationRepository)
{
_organizerRepository = organizerRepository;
_organizationRepository = organizationRepository;
}

public async Task HandleAsync(OrganizerRightsRevoked @event, CancellationToken cancellationToken)
{
var organizer = await _organizerRepository.GetAsync(@event.UserId);
if (organizer is null)
{
throw new OrganizerNotFoundException(@event.UserId);
}

var organizerOrganizations = await _organizationRepository.GetOrganizerOrganizationsAsync(@event.UserId);
foreach (var organization in organizerOrganizations)
{
organization.RemoveOrganizer(organizer);
await _organizationRepository.UpdateAsync(organization);
}

await _organizerRepository.DeleteAsync(@event.UserId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Convey.CQRS.Events;
using Convey.MessageBrokers;
using MiniSpace.Services.Organizations.Core.Entities;

namespace MiniSpace.Services.Organizations.Application.Events.External
{
[Message("identity") ]
public class OrganizerRightsGranted : IEvent
{
public Guid UserId { get; }

public OrganizerRightsGranted(Guid userId)
{
UserId = userId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Convey.CQRS.Events;
using Convey.MessageBrokers;

namespace MiniSpace.Services.Organizations.Application.Events.External
{
[Message("identity")]
public class OrganizerRightsRevoked: IEvent
{
public Guid UserId { get; }

public OrganizerRightsRevoked(Guid userId)
{
UserId = userId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MiniSpace.Services.Organizations.Application.Exceptions
{
public class OrganizerAlreadyAddedException : AppException
{
public override string Code { get; } = "organizer_already_added";
public Guid OrganizerId { get; }

public OrganizerAlreadyAddedException(Guid organizerId) : base($"Organizer with ID: '{organizerId}' was already added.")
{
OrganizerId = organizerId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MiniSpace.Services.Organizations.Application.Exceptions
{
public class OrganizerNotFoundException : AppException
{
public override string Code { get; } = "organizer_not_found";
public Guid OrganizerId { get; }

public OrganizerNotFoundException(Guid organizerId) : base($"Organizer with ID: {organizerId} was not found.")
{
OrganizerId = organizerId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ public IEnumerable<Organizer> Organizers
private set => _organizers = new HashSet<Organizer>(value);
}

public Organization(Guid id, string name, Guid parentId)
public Organization(Guid id, string name, Guid parentId, IEnumerable<Organizer> organizers = null)
{
Id = id;
Name = name;
ParentId = parentId;
Organizers = organizers ?? Enumerable.Empty<Organizer>();
}

public void RemoveOrganizer(Organizer organizer)
=> _organizers.Remove(organizer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace MiniSpace.Services.Organizations.Core.Repositories
public interface IOrganizationRepository
{
Task<Organization> GetAsync(Guid id);
Task<IEnumerable<Organization>> GetOrganizerOrganizationsAsync(Guid organizerId);
Task AddAsync(Organization organization);
Task UpdateAsync(Organization organization);
Task DeleteAsync(Guid id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents
public static class Extensions
{
public static Organization AsEntity(this OrganizationDocument document)
=> new Organization(document.Id, document.Name, document.ParentId);
=> new Organization(document.Id, document.Name, document.ParentId, document.Organizers);

public static OrganizationDocument AsDocument(this Organization entity)
=> new OrganizationDocument()
{
Id = entity.Id,
Name = entity.Name,
ParentId = entity.ParentId
ParentId = entity.ParentId,
Organizers = entity.Organizers
};

public static OrganizationDto AsDto(this OrganizationDocument document)
=> new OrganizationDto()
{
Id = document.Id,
Name = document.Name,
ParentId = document.ParentId
ParentId = document.ParentId,
MemberCount = document.Organizers.Count()
};

public static Organizer AsEntity(this OrganizerDocument document)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Convey.Types;
using MiniSpace.Services.Organizations.Core.Entities;

namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents
{
public class OrganizationDocument:IIdentifiable<Guid>
public class OrganizationDocument: IIdentifiable<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid ParentId { get; set; }
public IEnumerable<Organizer> Organizers { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public async Task<Organization> GetAsync(Guid id)

return organization?.AsEntity();
}

public async Task<IEnumerable<Organization>> GetOrganizerOrganizationsAsync(Guid organizerId)
{
var organizations = await _repository.FindAsync(o
=> o.Organizers.Any(x => x.Id == organizerId));

return organizations?.Select(o => o.AsEntity());
}

public Task AddAsync(Organization organization)
=> _repository.AddAsync(organization.AsDocument());
Expand Down

0 comments on commit 943946c

Please sign in to comment.