Skip to content

Commit

Permalink
Merge pull request #13 from kamil-oberaj/KO/get-by-id
Browse files Browse the repository at this point in the history
[FEAT]: GetById queries
  • Loading branch information
kamil-oberaj authored May 26, 2024
2 parents 8b1601b + a7cb936 commit 62632dc
Show file tree
Hide file tree
Showing 52 changed files with 662 additions and 72 deletions.
9 changes: 9 additions & 0 deletions StudioManager.API/Controllers/V1/EquipmentTypesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using StudioManager.Application.EquipmentTypes.Create;
using StudioManager.Application.EquipmentTypes.Delete;
using StudioManager.Application.EquipmentTypes.GetAll;
using StudioManager.Application.EquipmentTypes.GetById;
using StudioManager.Application.EquipmentTypes.Update;
using StudioManager.Domain.Common.Results;
using StudioManager.Domain.Filters;
Expand Down Expand Up @@ -53,6 +54,14 @@ public async Task<IResult> DeleteEquipmentTypeAsync(Guid id)
var command = new DeleteEquipmentTypeCommand(id);
return await SendAsync(command);
}

[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(QueryResult<EquipmentTypeReadDto>), StatusCodes.Status200OK)]
public async Task<IResult> GetEquipmentTypeByIdAsync(Guid id)
{
var command = new GetEquipmentTypeByIdQuery(id);
return await SendAsync(command);
}

private static EquipmentTypeFilter CreateFilter(string? ft)
{
Expand Down
8 changes: 8 additions & 0 deletions StudioManager.API/Controllers/V1/EquipmentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using StudioManager.Application.Equipments.Create;
using StudioManager.Application.Equipments.Delete;
using StudioManager.Application.Equipments.GetAll;
using StudioManager.Application.Equipments.GetById;
using StudioManager.Application.Equipments.Update;
using StudioManager.Domain.Common.Results;
using StudioManager.Domain.Filters;
Expand Down Expand Up @@ -54,6 +55,13 @@ public async Task<IResult> GetEquipments(
var command = new GetAllEquipmentsQuery(filter, pagination);
return await SendAsync(command);
}

[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(QueryResult<EquipmentReadDto>), StatusCodes.Status200OK)]
public async Task<IResult> GetEquipmentById(Guid id)
{
return await SendAsync(new GetEquipmentByIdQuery(id));
}

private static EquipmentFilter CreateFilter(
string? search,
Expand Down
8 changes: 8 additions & 0 deletions StudioManager.API/Controllers/V1/ReservationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using StudioManager.Application.Reservations.Create;
using StudioManager.Application.Reservations.Delete;
using StudioManager.Application.Reservations.GetAll;
using StudioManager.Application.Reservations.GetById;
using StudioManager.Application.Reservations.Update;
using StudioManager.Domain.Common.Results;
using StudioManager.Domain.Filters;
Expand Down Expand Up @@ -57,4 +58,11 @@ public async Task<IResult> GetAllReservations(

return await SendAsync(new GetAllReservationsQuery(filter, pagination));
}

[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(QueryResult<ReservationReadDto>), StatusCodes.Status200OK)]
public async Task<IResult> GetReservationById(Guid id)
{
return await SendAsync(new GetReservationByIdQuery(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task SetUpAsync()
}

[Test]
public async Task should_return_not_found_when_updating_non_existing_entity_async()
public async Task should_return_not_found_when_removing_non_existing_entity_async()
{
// Arrange
await using (var dbContext = await _testDbContextFactory.CreateDbContextAsync(Cts.Token))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using FluentAssertions;
using NUnit.Framework;
using StudioManager.API.Contracts.EquipmentTypes;
using StudioManager.Application.EquipmentTypes.GetById;
using StudioManager.Domain.Common.Results;
using StudioManager.Domain.Entities;
using StudioManager.Infrastructure;
using StudioManager.Tests.Common;
using StudioManager.Tests.Common.DbContextExtensions;

namespace StudioManager.Application.Tests.EquipmentTypes.GetEquipmentTypeByIdQueryHandlerTests;

public sealed class Handle : IntegrationTestBase
{
private static GetEquipmentTypeByIdQueryHandler _testCandidate = null!;
private static TestDbContextFactory<StudioManagerDbContext> _testDbContextFactory = null!;

[SetUp]
public async Task SetUpAsync()
{
var connectionString = await DbMigrator.MigrateDbAsync();
_testDbContextFactory = new TestDbContextFactory<StudioManagerDbContext>(connectionString);
_testCandidate = new GetEquipmentTypeByIdQueryHandler(_testDbContextFactory, Mapper);
}

[Test]
public async Task should_return_not_found_when_requesting_non_existing_entity_async()
{
// Arrange
await using (var dbContext = await _testDbContextFactory.CreateDbContextAsync(Cts.Token))
{
await ClearTableContentsForAsync<EquipmentType>(dbContext);
}

var id = Guid.NewGuid();

var command = new GetEquipmentTypeByIdQuery(id);

// Act
var result = await _testCandidate.Handle(command, Cts.Token);

result.Should().NotBeNull();
result.Should().BeOfType<QueryResult<EquipmentTypeReadDto>>();
result.Data.Should().BeNull();
result.Succeeded.Should().BeFalse();
result.StatusCode.Should().Be(NotFoundStatusCode);
result.Error.Should().NotBeNullOrWhiteSpace();
result.Error.Should().Be($"[NOT FOUND] {nameof(EquipmentType)} with id '{id}' does not exist");
}

[Test]
public async Task should_return_success_async()
{
// Arrange
var equipmentType = EquipmentType.Create("Test-Equipment-Type");
await using (var dbContext = await _testDbContextFactory.CreateDbContextAsync(Cts.Token))
{
await AddEntitiesToTable(dbContext, equipmentType);
}

var command = new GetEquipmentTypeByIdQuery(equipmentType.Id);

// Act
var result = await _testCandidate.Handle(command, Cts.Token);

result.Should().NotBeNull();
result.Should().BeOfType<QueryResult<EquipmentTypeReadDto>>();
result.Data.Should().NotBeNull();
result.Data.Should().BeOfType<EquipmentTypeReadDto>();
result.Succeeded.Should().BeTrue();
result.StatusCode.Should().Be(OkStatusCode);
result.Error.Should().BeNullOrWhiteSpace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using FluentAssertions;
using NUnit.Framework;
using StudioManager.Application.EquipmentTypes.GetById;

namespace StudioManager.Application.Tests.EquipmentTypes.GetEquipmentTypeByIdQueryHandlerTests;

public sealed class Validator
{
[Test]
public async Task should_return_error_when_id_is_empty_async()
{
// Arrange
var query = new GetEquipmentTypeByIdQuery(Guid.Empty);
var validator = new GetEquipmentTypeByIdQueryValidator();

// Act
var result = await validator.ValidateAsync(query, CancellationToken.None);

// Assert
result.IsValid.Should().BeFalse();
result.Errors.Should().ContainSingle(e => e.PropertyName == nameof(query.Id) && e.ErrorMessage == "'Id' must not be empty.");
}

[Test]
public async Task should_return_success_when_query_is_valid_async()
{
// Arrange
var query = new GetEquipmentTypeByIdQuery(Guid.NewGuid());
var validator = new GetEquipmentTypeByIdQueryValidator();

// Act
var result = await validator.ValidateAsync(query, CancellationToken.None);

// Assert
result.IsValid.Should().BeTrue();
result.Errors.Should().BeEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task SetUpAsync()
}

[Test]
public async Task should_return_not_found_when_updating_non_existing_entity_async()
public async Task should_return_not_found_when_removing_non_existing_entity_async()
{
// Arrange
await using (var dbContext = await _testDbContextFactory.CreateDbContextAsync(Cts.Token))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using FluentAssertions;
using NUnit.Framework;
using StudioManager.API.Contracts.Equipments;
using StudioManager.Application.Equipments.GetById;
using StudioManager.Domain.Common.Results;
using StudioManager.Domain.Entities;
using StudioManager.Infrastructure;
using StudioManager.Tests.Common;
using StudioManager.Tests.Common.DbContextExtensions;

namespace StudioManager.Application.Tests.Equipments.GetEquipmentByIdQueryHandlerTests;

public sealed class Handle : IntegrationTestBase
{
private static GetEquipmentByIdQueryHandler _testCandidate = null!;
private static TestDbContextFactory<StudioManagerDbContext> _testDbContextFactory = null!;


[SetUp]
public async Task SetUpAsync()
{
var connectionString = await DbMigrator.MigrateDbAsync();
_testDbContextFactory = new TestDbContextFactory<StudioManagerDbContext>(connectionString);
_testCandidate = new GetEquipmentByIdQueryHandler(_testDbContextFactory, Mapper);
}

[Test]
public async Task should_return_not_found_when_getting_non_existing_entity_async()
{
// Arrange
await using (var dbContext = await _testDbContextFactory.CreateDbContextAsync(Cts.Token))
{
await ClearTableContentsForAsync<Equipment>(dbContext);
}

var id = Guid.NewGuid();

var command = new GetEquipmentByIdQuery(id);

// Act
var result = await _testCandidate.Handle(command, Cts.Token);

result.Should().NotBeNull();
result.Should().BeOfType<QueryResult<EquipmentReadDto>>();
result.Data.Should().BeNull();
result.Succeeded.Should().BeFalse();
result.StatusCode.Should().Be(NotFoundStatusCode);
result.Error.Should().NotBeNullOrWhiteSpace();
result.Error.Should().Be($"[NOT FOUND] {nameof(Equipment)} with id '{id}' does not exist");
}

[Test]
public async Task should_return_success_async()
{
// Arrange
var equipmentType = EquipmentType.Create("Test-Equipment-Type");
var equipment = Equipment.Create("Test-Equipment", equipmentType.Id, 1);
await using (var dbContext = await _testDbContextFactory.CreateDbContextAsync(Cts.Token))
{
await ClearTableContentsForAsync<EquipmentType>(dbContext);
await ClearTableContentsForAsync<Equipment>(dbContext);
await AddEntitiesToTable(dbContext, equipmentType);
await AddEntitiesToTable(dbContext, equipment);
}

var command = new GetEquipmentByIdQuery(equipment.Id);

// Act
var result = await _testCandidate.Handle(command, Cts.Token);

result.Should().NotBeNull();
result.Should().BeOfType<QueryResult<EquipmentReadDto>>();
result.Data.Should().NotBeNull();
result.Data.Should().BeOfType<EquipmentReadDto>();
result.Succeeded.Should().BeTrue();
result.StatusCode.Should().Be(OkStatusCode);
result.Error.Should().BeNullOrWhiteSpace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentAssertions;
using NUnit.Framework;
using StudioManager.Application.Equipments.GetById;

namespace StudioManager.Application.Tests.Equipments.GetEquipmentByIdQueryHandlerTests;

public sealed class Validator
{
[Test]
public async Task should_return_error_when_id_is_empty_async()
{
// Arrange
var command = new GetEquipmentByIdQuery(Guid.Empty);
var validator = new GetEquipmentByIdQueryValidator();

// Act
var result = await validator.ValidateAsync(command, CancellationToken.None);

// Assert
result.Should().NotBeNull();
result.IsValid.Should().BeFalse();
result.Errors.Should().NotBeNullOrEmpty();
result.Errors.Should().HaveCount(1);
result.Errors.First().ErrorMessage.Should().Be("'Id' must not be empty.");
}

[Test]
public async Task should_return_success_when_query_is_valid_async()
{
// Arrange
var command = new GetEquipmentByIdQuery(Guid.NewGuid());
var validator = new GetEquipmentByIdQueryValidator();

// Act
var result = await validator.ValidateAsync(command, CancellationToken.None);

// Assert
result.Should().NotBeNull();
result.IsValid.Should().BeTrue();
result.Errors.Should().BeEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using FluentAssertions;
using NUnit.Framework;
using StudioManager.API.Contracts.Reservations;
using StudioManager.Application.Reservations.GetById;
using StudioManager.Application.Tests.Reservations.Common;
using StudioManager.Domain.Common.Results;
using StudioManager.Domain.Entities;
using StudioManager.Infrastructure;
using StudioManager.Tests.Common;
using StudioManager.Tests.Common.DbContextExtensions;

namespace StudioManager.Application.Tests.Reservations.GetReservationByIdQueryHandlerTests;

public sealed class Handle : IntegrationTestBase
{
private static TestDbContextFactory<StudioManagerDbContext> _dbContextFactory = null!;
private static GetReservationByIdQueryHandler _testCandidate = null!;

[SetUp]
public async Task SetUpAsync()
{
var connectionString = await DbMigrator.MigrateDbAsync();
_dbContextFactory = new TestDbContextFactory<StudioManagerDbContext>(connectionString);
_testCandidate = new GetReservationByIdQueryHandler(_dbContextFactory, Mapper);
}

[Test]
public async Task should_return_not_found_when_requesting_non_existing_reservation_async()
{
// Arrange
var command = new GetReservationByIdQuery(Guid.NewGuid());

// Act
var result = await _testCandidate.Handle(command, CancellationToken.None);

// Assert
result.Should().NotBeNull();
result.Succeeded.Should().BeFalse();
result.StatusCode.Should().Be(NotFoundStatusCode);
result.Data.Should().BeNull();
result.Should().BeOfType<QueryResult<ReservationReadDto>>();
result.Error.Should().NotBeNullOrWhiteSpace();
result.Error.Should().Be($"[NOT FOUND] {nameof(Reservation)} with id '{command.Id}' does not exist");
}

[Test]
public async Task should_return_success_async()
{
// Arrange
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
var reservation =
await ReservationTestHelper.AddReservationAsync(dbContext);

var command = new GetReservationByIdQuery(reservation.Id);

// Act
var result = await _testCandidate.Handle(command, CancellationToken.None);

// Assert
result.Should().NotBeNull();
result.Succeeded.Should().BeTrue();
result.StatusCode.Should().Be(OkStatusCode);
result.Data.Should().NotBeNull();
result.Data.Should().BeOfType<ReservationReadDto>();
result.Should().BeOfType<QueryResult<ReservationReadDto>>();
result.Error.Should().BeNullOrWhiteSpace();
}
}
Loading

0 comments on commit 62632dc

Please sign in to comment.