From 05ce3f1e04dd70707ac5dd5ffc089145a605b561 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 3 Jun 2023 18:18:33 +0700 Subject: [PATCH] feat: get staff by id --- .../Staffs/Queries/GetStaffById.cs | 30 +++++++++ .../Staffs/Queries/GetStaffByIdTests.cs | 61 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tests/Application.Tests.Integration/Staffs/Queries/GetStaffByIdTests.cs diff --git a/src/Application/Staffs/Queries/GetStaffById.cs b/src/Application/Staffs/Queries/GetStaffById.cs index 7510235e..6cab8c90 100644 --- a/src/Application/Staffs/Queries/GetStaffById.cs +++ b/src/Application/Staffs/Queries/GetStaffById.cs @@ -1,5 +1,8 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Staffs.Queries; @@ -9,4 +12,31 @@ public record Query : IRequest { public Guid StaffId { get; init; } } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var staff = await _context.Staffs + .Include(x => x.User) + .Include(y => y.Room) + .FirstOrDefaultAsync(x => x.Id.Equals(request.StaffId), cancellationToken: cancellationToken); + + if (staff is null) + { + throw new KeyNotFoundException("Staff does not exist."); + } + + return _mapper.Map(staff); + } + } } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Staffs/Queries/GetStaffByIdTests.cs b/tests/Application.Tests.Integration/Staffs/Queries/GetStaffByIdTests.cs new file mode 100644 index 00000000..ac11a39a --- /dev/null +++ b/tests/Application.Tests.Integration/Staffs/Queries/GetStaffByIdTests.cs @@ -0,0 +1,61 @@ +using Application.Identity; +using Application.Staffs.Queries; +using Domain.Entities; +using Domain.Entities.Physical; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Staffs.Queries; + +public class GetStaffByIdTests : BaseClassFixture +{ + public GetStaffByIdTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldReturnStaff_WhenIdIsValid() + { + // Arrange + var department = CreateDepartment(); + var user = CreateUser(IdentityData.Roles.Staff, "123123"); + var room = CreateRoom(department); + var staff = CreateStaff(user, room); + await AddAsync(staff); + + var query = new GetStaffById.Query() + { + StaffId = staff.Id + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.User.Id.Should().Be(staff.Id); + + // Cleanup + Remove(staff); + Remove(await FindAsync(room.Id)); + Remove(await FindAsync(user.Id)); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenStaffDoesNotExist() + { + // Arrange + var query = new GetStaffById.Query() + { + StaffId = Guid.NewGuid() + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Staff does not exist."); + } + +} \ No newline at end of file