-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f76b858
commit 95027ce
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...ersonalData/PersonalData/Boundaries/GraphQl/InputObjectTypes/GetWorkspaceByIdInputType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/PersonalData/PersonalData/UseCases/Queries/GetWorkspaceByIdQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/PersonalData/PersonalData/UseCases/Queries/Handlers/GetWorkspaceByIdHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |