add put and patch; add acceptable formats; add console logging

This commit is contained in:
Greg Gauthier 2023-01-05 10:34:38 +00:00
parent 9bb2e8c8a5
commit 94a78a3b82
3 changed files with 73 additions and 1 deletions

View File

@ -7,7 +7,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -1,5 +1,6 @@
using AddressBookApi.DataStore;
using AddressBookApi.Models.Dto;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
namespace AddressBookApi.Controllers;
@ -8,9 +9,17 @@ namespace AddressBookApi.Controllers;
[Route("api/AddressBookApi")]
public class AddressBookApiController : ControllerBase
{
private readonly ILogger<AddressBookApiController> _addressBookLog;
public AddressBookApiController(ILogger<AddressBookApiController> logger)
{
_addressBookLog = logger;
}
[HttpGet]
public ActionResult<IEnumerable<AddressDto>> GetAddresses()
{
_addressBookLog.LogInformation("Retrieving all addresses...");
return Ok(AddressStore.addressList);
}
@ -22,12 +31,14 @@ public class AddressBookApiController : ControllerBase
{
if (id == 0)
{
_addressBookLog.LogWarning("Address entry id 0 is not valid.");
return BadRequest();
}
var address = AddressStore.addressList.FirstOrDefault((u => u.Id == id));
if (address == null)
{
_addressBookLog.LogWarning("Address entry id {id} not found", id);
return NotFound();
}
@ -113,4 +124,58 @@ public class AddressBookApiController : ControllerBase
AddressStore.addressList.Remove(address);
return NoContent();
}
[HttpPut("{id:int}", Name = "UpdateAddress")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public IActionResult UpdateAddress(int id, [FromBody] AddressDto addressDto)
{
if (id != addressDto.Id | id == 0)
{
return BadRequest();
}
var address = AddressStore.addressList.FirstOrDefault(u => u.Id == id);
if (address == null)
{
return NotFound();
}
address.Name = addressDto.Name;
address.Street = addressDto.Street;
address.City = addressDto.City;
address.PostCode = addressDto.PostCode;
return NoContent();
}
[HttpPatch("{id:int}", Name = "UpdateAddressItem")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public IActionResult UpdateAddressItem(int id, JsonPatchDocument<AddressDto> addressPatch)
{
if (id == 0) // Can't compare the ids yet, because its embedded in the Patch Doc
{
return BadRequest();
}
var address = AddressStore.addressList.FirstOrDefault(u => u.Id == id);
if (address == null)
{
return NotFound();
}
if (address.Id != id)
{
return BadRequest();
}
addressPatch.ApplyTo(address, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return NoContent();
}
}

View File

@ -3,6 +3,10 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddControllers(option =>
{
option.ReturnHttpNotAcceptable = true;
}).AddNewtonsoftJson(option => {}).AddXmlDataContractSerializerFormatters();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();