Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Application/Folders/Queries/GetFolderById.cs
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Folders.Queries;

Expand All@@ -9,4 +12,28 @@ public record Query : IRequest<FolderDto>
{
public Guid FolderId { get; init; }
}

public class QueryHandler : IRequestHandler<Query, FolderDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

public QueryHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<FolderDto> Handle(Query request, CancellationToken cancellationToken)
{
var folder = await _context.Folders.FirstOrDefaultAsync(x => x.Id.Equals(request.FolderId), cancellationToken);

if (folder is null)
{
throw new KeyNotFoundException("Folder does not exist.");
}

return _mapper.Map<FolderDto>(folder);
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
using Application.Folders.Queries;
using Domain.Entities;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Folders.Queries;

public class GetFolderByIdTests : BaseClassFixture
{
public GetFolderByIdTests(CustomApiFactory apiFactory) : base(apiFactory)
{
}

[Fact]
public async Task ShouldReturnFolder_WhenThatFolderExists()
{
// Arrange
var department = CreateDepartment();
var folder = CreateFolder();
var locker = CreateLocker(folder);
var room = CreateRoom(department, locker);
await AddAsync(room);

var query = new GetFolderById.Query()
{
FolderId = folder.Id,
};

// Act
var result = await SendAsync(query);

// Assert
result.Id.Should().Be(folder.Id);
result.Name.Should().Be(folder.Name);

// Cleanup
Remove(folder);
Remove(locker);
Remove(room);
Remove(await FindAsync<Department>(department.Id));
}

[Fact]
public async Task ShouldThrowKeyNotFoundException_WhenThatFolderDoesNotExist()
{
// Arrange
var query = new GetFolderById.Query()
{
FolderId = Guid.NewGuid(),
};

// Act
var action = async () => await SendAsync(query);

// Assert
await action.Should().ThrowAsync<KeyNotFoundException>()
.WithMessage("Folder does not exist.");
}
}