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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public EmployeeActionResultController(IFakeRepository repository)
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<Employee?> GetById(int id)
public ActionResult<Employee> GetById(int id)
{
if (!_repository.TryGetEmployee(id, out var employee))
{
Expand All @@ -31,15 +31,14 @@ public EmployeeActionResultController(IFakeRepository repository)
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Employee>> CreateAsync(Employee employee)
{
int? employeeNameLength = employee?.Name?.Length;
if (employeeNameLength < 3 || employeeNameLength > 30)
if (employee.Name is not { Length: >= 3 and <= 30 })
{
return BadRequest("Name should be between 3 and 30 characters.");
}

await _repository.AddEmployeeAsync(employee);

return CreatedAtAction(nameof(GetById), new { id = employee?.Id }, employee);
return CreatedAtAction(nameof(GetById), new { id = employee.Id }, employee);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace APIReturnType.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeHttpResultsController : ControllerBase
{
public IFakeRepository _repository;

public EmployeeHttpResultsController(IFakeRepository repository)
{
_repository = repository;
}

[HttpGet("{id}")]
public Results<NotFound, Ok<Employee>> GetById(int id)
{
if (!_repository.TryGetEmployee(id, out var employee))
{
return TypedResults.NotFound();
}

return TypedResults.Ok(employee);
}

[HttpPost]
public async Task<Results<BadRequest<string>, Created<Employee>>> CreateAsync(Employee employee)
{
if (employee.Name is not { Length: >= 3 and <= 30 })
{
return TypedResults.BadRequest("Name should be between 3 and 30 characters.");
}

await _repository.AddEmployeeAsync(employee);

return TypedResults.Created($"/api/employeehttpresults/{employee.Id}", employee);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public EmployeeIActionResultController(IFakeRepository repository)
}

[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Employee))]
[ProducesResponseType<Employee>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetById(int id)
{
Expand All @@ -31,7 +31,7 @@ public IActionResult GetById(int id)
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateAsync(Employee employee)
{
if (employee.Name.Length < 3 || employee.Name.Length > 30)
if (employee.Name is not { Length: >= 3 and <= 30 })
{
return BadRequest("Name should be between 3 and 30 characters.");
}
Expand Down
6 changes: 4 additions & 2 deletions aspnetcore-webapi/ReturnTypes/APIReturnType/FakeRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace APIReturnType
using System.Diagnostics.CodeAnalysis;

namespace APIReturnType
{
public class FakeRepository : IFakeRepository
{
Expand Down Expand Up @@ -31,7 +33,7 @@ public IEnumerable<Employee> GetEmployees()
return Employees;
}

public bool TryGetEmployee(int id, out Employee? employee)
public bool TryGetEmployee(int id, [NotNullWhen(true)] out Employee? employee)
{
employee = GetEmployees().FirstOrDefault(e => e.Id == id);
return employee != null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace APIReturnType
using System.Diagnostics.CodeAnalysis;

namespace APIReturnType
{
public interface IFakeRepository
{
public IEnumerable<Employee> GetEmployees();

public bool TryGetEmployee(int id, out Employee? employee);
public bool TryGetEmployee(int id, [NotNullWhen(true)] out Employee? employee);

public IEnumerable<Employee> GetActiveEmployees();

Expand Down
7 changes: 2 additions & 5 deletions aspnetcore-webapi/ReturnTypes/APIReturnType/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOpenApi();

builder.Services.AddSingleton<IFakeRepository, FakeRepository>();

Expand All @@ -17,8 +15,7 @@
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();
}

app.UseHttpsRedirection();
Expand Down
12 changes: 6 additions & 6 deletions aspnetcore-webapi/ReturnTypes/Tests/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.12" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading