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/Lockers/Queries/GetLockerById.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.Lockers.Queries;

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

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

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

public async Task<LockerDto> Handle(Query request, CancellationToken cancellationToken)
{
var locker = await _context.Lockers.FirstOrDefaultAsync(x => x.Id.Equals(request.LockerId), cancellationToken);

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

return _mapper.Map<LockerDto>(locker);
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
using Application.Lockers.Queries;
using Domain.Entities;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Lockers.Queries;

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

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

var query = new GetLockerById.Query()
{
LockerId = locker.Id,
};

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

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

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

[Fact]
public async Task ShouldThrowKeyNotFoundException_WhenThatLockerDoesNotExist()
{
// Arrange
var query = new GetLockerById.Query()
{
LockerId = Guid.NewGuid(),
};

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

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