Skip to content

Commit

Permalink
make endpoints more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed May 5, 2024
1 parent 3a2a76f commit 91d183e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task DeleteEmployeeById_IsDeleted()
var response = await Sut.CreateClientFor<IEmployeeApiClient>().DeleteEmployeeById(employee.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.OK);
await response.AssertStatusCode(HttpStatusCode.NoContent);
Sut.AssertDatabase(context => { context.Employees.Should().BeEmpty(); });
}

Expand Down
1 change: 1 addition & 0 deletions CleanAspCore.Api.Tests/HttpAssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static async Task AssertStatusCode(this HttpResponseMessage response, Htt

public static async Task AssertJsonBodyIsEquivalentTo<T>(this HttpResponseMessage response, T expected)
{
using var scope = new AssertionScope();
response.Content.Headers.ContentType.Should().NotBeNull();
response.Content.Headers.ContentType!.MediaType.Should().Be("application/json");
response.Content.Headers.ContentType!.CharSet.Should().Be("utf-8");
Expand Down
19 changes: 12 additions & 7 deletions CleanAspCore/Features/Departments/Endpoints/GetDepartmentById.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

namespace CleanAspCore.Features.Departments.Endpoints;

public sealed record DepartmentDto(Guid Id, string Name, string City);
public sealed class DepartmentDto
{
public required Guid Id { get; init; }
public required string Name { get; init; }
public required string City { get; init; }
}

internal static class GetDepartmentById
{
Expand All @@ -19,10 +24,10 @@ internal static async Task<Ok<DepartmentDto>> Handle(Guid id, HrContext context,
return TypedResults.Ok(department);
}

private static DepartmentDto ToDto(this Department department) => new
(
department.Id,
department.Name,
department.City
);
private static DepartmentDto ToDto(this Department department) => new()
{
Id = department.Id,
Name = department.Name,
City = department.City
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace CleanAspCore.Features.Employees.Endpoints;

internal static class DeleteEmployeeById
{
internal static async Task<Results<Ok, NotFound>> Handle(Guid id, HrContext context, CancellationToken cancellationToken)
internal static async Task<Results<NoContent, NotFound>> Handle(Guid id, HrContext context, CancellationToken cancellationToken)
{
var result = await context.Employees
.Where(x => x.Id == id)
.ExecuteDeleteAsync(cancellationToken);

return result switch
{
1 => TypedResults.Ok(),
1 => TypedResults.NoContent(),
_ => TypedResults.NotFound()
};
}
Expand Down
32 changes: 21 additions & 11 deletions CleanAspCore/Features/Employees/Endpoints/GetEmployeeById.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

namespace CleanAspCore.Features.Employees.Endpoints;

public sealed record EmployeeDto(Guid Id, string FirstName, string LastName, string Email, string Gender, Guid DepartmentId, Guid JobId);
public sealed class GetEmployeeResponse
{
public required Guid Id { get; init; }
public required string FirstName { get; init; }
public required string LastName { get; init; }
public required string Email { get; init; }
public required string Gender { get; init; }
public required Guid DepartmentId { get; init; }
public required Guid JobId { get; init; }
}

internal static class GetEmployeeById
{
internal static async Task<JsonHttpResult<EmployeeDto>> Handle(Guid id, HrContext context, CancellationToken cancellationToken)
internal static async Task<JsonHttpResult<GetEmployeeResponse>> Handle(Guid id, HrContext context, CancellationToken cancellationToken)
{
var result = await context.Employees
.Where(x => x.Id == id)
Expand All @@ -18,13 +27,14 @@ internal static async Task<JsonHttpResult<EmployeeDto>> Handle(Guid id, HrContex
return TypedResults.Json(result);
}

private static EmployeeDto ToDto(this Employee employee) => new(
employee.Id,
employee.FirstName,
employee.LastName,
employee.Email.ToString(),
employee.Gender,
employee.DepartmentId,
employee.JobId
);
private static GetEmployeeResponse ToDto(this Employee employee) => new()
{
Id = employee.Id,
FirstName = employee.FirstName,
LastName = employee.LastName,
Email = employee.Email.ToString(),
Gender = employee.Gender,
DepartmentId = employee.DepartmentId,
JobId = employee.JobId
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class UpdateEmployeeRequest

internal static class UpdateEmployeeById
{
internal static async Task<Results<NoContent, NotFound, ValidationProblem>> Handle(
internal static async Task<Results<NoContent, NotFound>> Handle(
Guid id, [FromBody] UpdateEmployeeRequest updateEmployeeRequest, HrContext context, CancellationToken cancellationToken)
{
var builder = new SetPropertyBuilder<Employee>()
Expand Down
15 changes: 10 additions & 5 deletions CleanAspCore/Features/Jobs/Endpoints/GetJobById.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

namespace CleanAspCore.Features.Jobs.Endpoints;

public sealed record JobDto(Guid Id, string Name);
public sealed class JobDto
{
public Guid Id { get; init; }
public string Name { get; init; }

Check warning on line 11 in CleanAspCore/Features/Jobs/Endpoints/GetJobById.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

internal static class GetJobById
{
Expand All @@ -18,8 +22,9 @@ internal static async Task<JsonHttpResult<JobDto>> Handle(Guid id, HrContext con
return TypedResults.Json(results);
}

private static JobDto ToDto(this Job department) => new(
department.Id,
department.Name
);
private static JobDto ToDto(this Job department) => new()
{
Id = department.Id,
Name = department.Name
};
}

0 comments on commit 91d183e

Please sign in to comment.