diff --git a/src/PersonalData/PersonalData/Boundaries/GraphQl/InputObjectTypes/GetWorkspaceByIdInputType.cs b/src/PersonalData/PersonalData/Boundaries/GraphQl/InputObjectTypes/GetWorkspaceByIdInputType.cs new file mode 100644 index 0000000..e65d795 --- /dev/null +++ b/src/PersonalData/PersonalData/Boundaries/GraphQl/InputObjectTypes/GetWorkspaceByIdInputType.cs @@ -0,0 +1,15 @@ +using HotChocolate.Types; +using PersonalData.UseCases.Queries; +using Shared.Common.Helpers; + +namespace PersonalData.Boundaries.GraphQl.InputObjectTypes; + +public class GetWorkspaceByIdInputType : InputObjectType +{ + private static readonly string _inputName = nameof(GetWorkspaceByIdInputType).RemoveSuffix("Type"); + + protected override void Configure(IInputObjectTypeDescriptor descriptor) + { + descriptor.Name(_inputName); + } +} \ No newline at end of file diff --git a/src/PersonalData/PersonalData/Boundaries/GraphQl/Query.cs b/src/PersonalData/PersonalData/Boundaries/GraphQl/Query.cs index b3de317..06073ba 100644 --- a/src/PersonalData/PersonalData/Boundaries/GraphQl/Query.cs +++ b/src/PersonalData/PersonalData/Boundaries/GraphQl/Query.cs @@ -11,6 +11,7 @@ using Newtonsoft.Json; using PersonalData.Boundaries.GraphQl.Dtos; using PersonalData.Boundaries.GraphQl.Filters; +using PersonalData.Boundaries.GraphQl.InputObjectTypes; using PersonalData.Boundaries.GraphQl.ObjectTypes; using PersonalData.Common.Enums; using PersonalData.Services; @@ -159,4 +160,13 @@ public async Task GetWorkspaces( { return await mediator.Send(new GetWorkspacesQuery()); } + + [GraphQLName("GetWorkspaceById")] + [Authorize(AuthorizationPolicy.ADMIN_MEMBER_ACCESS)] + public async Task GetWorkspaceById( + [GraphQLType(typeof(GetWorkspaceByIdInputType))] GetWorkspaceByIdQuery query, + [Service] ISender mediator) + { + return await mediator.Send(query); + } } \ No newline at end of file diff --git a/src/PersonalData/PersonalData/UseCases/Queries/GetWorkspaceByIdQuery.cs b/src/PersonalData/PersonalData/UseCases/Queries/GetWorkspaceByIdQuery.cs new file mode 100644 index 0000000..034efd3 --- /dev/null +++ b/src/PersonalData/PersonalData/UseCases/Queries/GetWorkspaceByIdQuery.cs @@ -0,0 +1,9 @@ +using MediatR; +using PersonalData.Boundaries.GraphQl.Dtos; + +namespace PersonalData.UseCases.Queries; + +public class GetWorkspaceByIdQuery : IRequest +{ + public string WorkspaceId { get; set; } = default!; +} \ No newline at end of file diff --git a/src/PersonalData/PersonalData/UseCases/Queries/Handlers/GetWorkspaceByIdHandler.cs b/src/PersonalData/PersonalData/UseCases/Queries/Handlers/GetWorkspaceByIdHandler.cs new file mode 100644 index 0000000..08faf4e --- /dev/null +++ b/src/PersonalData/PersonalData/UseCases/Queries/Handlers/GetWorkspaceByIdHandler.cs @@ -0,0 +1,59 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PersonalData.Boundaries.GraphQl.Dtos; +using PersonalData.Common.Converters; +using PersonalData.Data; +using Shared.SecurityContext; + +namespace PersonalData.UseCases.Queries.Handlers; + +public class GetWorkspaceByIdHandler : IRequestHandler +{ + private readonly ILogger _logger; + private readonly PersonalContext _personalContext; + private readonly ISecurityContextAccessor _securityContext; + + public GetWorkspaceByIdHandler(ILogger logger, PersonalContext personalContext, ISecurityContextAccessor securityContext) + { + _logger = logger; + _personalContext = personalContext; + _securityContext = securityContext; + } + + public async Task Handle(GetWorkspaceByIdQuery request, CancellationToken cancellationToken) + { + _logger.LogInformation("{HandlerName} - Start", nameof(GetWorkspaceByIdHandler)); + + if (!Guid.TryParse(_securityContext.UserId, out var userId)) + { + _logger.LogError("{HandlerName} - UserId is not a valid Guid", nameof(GetWorkspaceByIdHandler)); + + return null; + } + + var workspace = await _personalContext.Workspaces + .Include(w => w.Members) + .Include(w => w.Invitations) + .ThenInclude(w => w.InvitedPerson) + .SingleOrDefaultAsync(w => w.Id == Guid.Parse(request.WorkspaceId!) && w.DeletedOn == null, cancellationToken); + + if (workspace == null) + { + _logger.LogError("{HandlerName} - Workspace not found", nameof(GetWorkspaceByIdHandler)); + + return null; + } + + if (workspace.Members.All(m => m.Id != userId)) + { + _logger.LogError("{HandlerName} - User is not a member of the workspace", nameof(GetWorkspaceByIdHandler)); + + return null; + } + + _logger.LogInformation("{HandlerName} - Finish", nameof(GetWorkspaceByIdHandler)); + + return workspace.ToWorkspaceDto(); + } +} \ No newline at end of file