Skip to content

Commit

Permalink
(#87) add endpoint for getting all suborganizations ids
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed May 16, 2024
1 parent 47f4aa2 commit 015e2f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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")
.Get<GetAllChildrenOrganizations, IEnumerable<Guid>>("organizations/{organizationId}/children/all")
.Post<CreateOrganization>("organizations",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/root"))
.Post<AddOrganizerToOrganization>("organizations/{organizationId}/organizer")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Convey.CQRS.Queries;
using MiniSpace.Services.Organizations.Application.DTO;

namespace MiniSpace.Services.Organizations.Application.Queries
{
public class GetAllChildrenOrganizations: IQuery<IEnumerable<Guid>>
{
public Guid ParentId { get; set; }
public Guid RootId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,23 @@ private static void FindOrganizationsRecursive(Guid targetOrganizerId, Organizat
FindOrganizationsRecursive(targetOrganizerId, subOrg, organizations);
}
}

public static List<Guid> FindAllChildrenOrganizations(Organization rootOrganization)
{
var organizations = new List<Guid>();
FindAllChildrenOrganizationsRecursive(rootOrganization, organizations);
return organizations;
}

private static void FindAllChildrenOrganizationsRecursive(Organization currentOrganization,
ICollection<Guid> organizations)
{
organizations.Add(currentOrganization.Id);

foreach (var subOrg in currentOrganization.SubOrganizations)
{
FindAllChildrenOrganizationsRecursive(subOrg, organizations);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Convey.CQRS.Queries;
using Convey.Persistence.MongoDB;
using MiniSpace.Services.Organizations.Application.Queries;
using MiniSpace.Services.Organizations.Core.Entities;
using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents;

namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers
{
public class GetAllChildrenOrganizationsHandler : IQueryHandler<GetAllChildrenOrganizations, IEnumerable<Guid>>
{
private readonly IMongoRepository<OrganizationDocument, Guid> _repository;

public GetAllChildrenOrganizationsHandler(IMongoRepository<OrganizationDocument, Guid> repository)
=> _repository = repository;

public async Task<IEnumerable<Guid>> HandleAsync(GetAllChildrenOrganizations query, CancellationToken cancellationToken)
{
var root = await _repository.GetAsync(o => o.Id == query.RootId);
var organization = root?.AsEntity().GetSubOrganization(query.ParentId);
var result = new List<Guid>();
if (organization != null)
{
result.AddRange(Organization.FindAllChildrenOrganizations(organization));
}

return result;
}
}
}

0 comments on commit 015e2f4

Please sign in to comment.