Skip to content

Commit

Permalink
feat: get workspace by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
cowienduckie committed Jan 11, 2024
1 parent f76b858 commit 95027ce
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using HotChocolate.Types;
using PersonalData.UseCases.Queries;
using Shared.Common.Helpers;

namespace PersonalData.Boundaries.GraphQl.InputObjectTypes;

public class GetWorkspaceByIdInputType : InputObjectType<GetWorkspaceByIdQuery>
{
private static readonly string _inputName = nameof(GetWorkspaceByIdInputType).RemoveSuffix("Type");

protected override void Configure(IInputObjectTypeDescriptor<GetWorkspaceByIdQuery> descriptor)
{
descriptor.Name(_inputName);
}
}
10 changes: 10 additions & 0 deletions src/PersonalData/PersonalData/Boundaries/GraphQl/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,4 +160,13 @@ public async Task<GetWorkspacesResponse> GetWorkspaces(
{
return await mediator.Send(new GetWorkspacesQuery());
}

[GraphQLName("GetWorkspaceById")]
[Authorize(AuthorizationPolicy.ADMIN_MEMBER_ACCESS)]
public async Task<WorkspaceDto?> GetWorkspaceById(
[GraphQLType(typeof(GetWorkspaceByIdInputType))] GetWorkspaceByIdQuery query,
[Service] ISender mediator)
{
return await mediator.Send(query);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MediatR;
using PersonalData.Boundaries.GraphQl.Dtos;

namespace PersonalData.UseCases.Queries;

public class GetWorkspaceByIdQuery : IRequest<WorkspaceDto?>
{
public string WorkspaceId { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -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<GetWorkspaceByIdQuery, WorkspaceDto?>
{
private readonly ILogger<GetWorkspaceByIdHandler> _logger;
private readonly PersonalContext _personalContext;
private readonly ISecurityContextAccessor _securityContext;

public GetWorkspaceByIdHandler(ILogger<GetWorkspaceByIdHandler> logger, PersonalContext personalContext, ISecurityContextAccessor securityContext)
{
_logger = logger;
_personalContext = personalContext;
_securityContext = securityContext;
}

public async Task<WorkspaceDto?> 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();
}
}

0 comments on commit 95027ce

Please sign in to comment.