From 94a78a3b8207d64c5549f793c9788fbadc89376c Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Thu, 5 Jan 2023 10:34:38 +0000 Subject: [PATCH] add put and patch; add acceptable formats; add console logging --- AddressBookApi.csproj | 3 ++ Controllers/AddressBookApiController.cs | 67 ++++++++++++++++++++++++- Program.cs | 4 ++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/AddressBookApi.csproj b/AddressBookApi.csproj index 5bd76c6..45b524d 100644 --- a/AddressBookApi.csproj +++ b/AddressBookApi.csproj @@ -7,7 +7,10 @@ + + + diff --git a/Controllers/AddressBookApiController.cs b/Controllers/AddressBookApiController.cs index 5404d4c..4ad36ac 100644 --- a/Controllers/AddressBookApiController.cs +++ b/Controllers/AddressBookApiController.cs @@ -1,5 +1,6 @@ using AddressBookApi.DataStore; using AddressBookApi.Models.Dto; +using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc; namespace AddressBookApi.Controllers; @@ -7,10 +8,18 @@ namespace AddressBookApi.Controllers; [ApiController] [Route("api/AddressBookApi")] public class AddressBookApiController : ControllerBase -{ +{ + private readonly ILogger _addressBookLog; + + public AddressBookApiController(ILogger logger) + { + _addressBookLog = logger; + } + [HttpGet] public ActionResult> 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 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(); + } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 8264bac..7a43e12 100644 --- a/Program.cs +++ b/Program.cs @@ -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();