diff --git a/AddressBookApi.csproj b/AddressBookApi.csproj index fe4dc41..5bd76c6 100644 --- a/AddressBookApi.csproj +++ b/AddressBookApi.csproj @@ -11,9 +11,4 @@ - - - - - diff --git a/AddressBookApi.sln.DotSettings.user b/AddressBookApi.sln.DotSettings.user new file mode 100644 index 0000000..676d65c --- /dev/null +++ b/AddressBookApi.sln.DotSettings.user @@ -0,0 +1,4 @@ + + True + True + True \ No newline at end of file diff --git a/Controllers/AddressBookApiController.cs b/Controllers/AddressBookApiController.cs new file mode 100644 index 0000000..5404d4c --- /dev/null +++ b/Controllers/AddressBookApiController.cs @@ -0,0 +1,116 @@ +using AddressBookApi.DataStore; +using AddressBookApi.Models.Dto; +using Microsoft.AspNetCore.Mvc; + +namespace AddressBookApi.Controllers; + +[ApiController] +[Route("api/AddressBookApi")] +public class AddressBookApiController : ControllerBase +{ + [HttpGet] + public ActionResult> GetAddresses() + { + return Ok(AddressStore.addressList); + } + + [HttpGet("{id:int}", Name = "GetAddressById")] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + [ProducesResponseType(404)] + public ActionResult GetAddress(int id) + { + if (id == 0) + { + return BadRequest(); + } + + var address = AddressStore.addressList.FirstOrDefault((u => u.Id == id)); + if (address == null) + { + return NotFound(); + } + + return Ok(address); + } + + [HttpGet("{name}", Name = "GetAddressByName")] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + [ProducesResponseType(404)] + public ActionResult GetAddress(string name) + { + if (name == "") + { + return BadRequest(); + } + + var address = AddressStore.addressList.FirstOrDefault(u => u.Name == name); + if (address == null) + { + return NotFound(); + } + + return Ok(address); + } + + [HttpPost] + [ProducesResponseType(201)] + [ProducesResponseType(400)] + public ActionResult AddAddress([FromBody] AddressDto newAddressDto) + { + if (AddressStore.addressList.FirstOrDefault(u=>String.Equals(u.Name, newAddressDto.Name, StringComparison.CurrentCultureIgnoreCase)) != null) + { + ModelState.AddModelError("Address Error 1", "Addressee Already Exists!"); + return BadRequest(ModelState); + } + if (newAddressDto.Id > 0) + { + return StatusCode(StatusCodes.Status400BadRequest); + } + + if (newAddressDto.Name == "") + { + return StatusCode(StatusCodes.Status400BadRequest); + } + + if (newAddressDto.Street == "") + { + return StatusCode(StatusCodes.Status400BadRequest); + } + + if (newAddressDto.City == "") + { + return StatusCode(StatusCodes.Status400BadRequest); + } + + if (newAddressDto.PostCode == "") + { + return StatusCode(StatusCodes.Status400BadRequest); + } + + newAddressDto.Id = AddressStore.addressList.MaxBy(u => u.Id)!.Id + 1; + AddressStore.addressList.Add(newAddressDto); + return CreatedAtRoute("GetAddressById", new { id = newAddressDto.Id }, newAddressDto); + } + + [HttpDelete("{id:int}", Name = "DeleteAddress")] + [ProducesResponseType(204)] + [ProducesResponseType(400)] + [ProducesResponseType(404)] + public IActionResult DeleteAddress(int id) + { + if (id == 0) + { + return BadRequest(); + } + + var address = AddressStore.addressList.FirstOrDefault(u => u.Id == id); + if (address == null) + { + return NotFound(); + } + AddressStore.addressList.Remove(address); + return NoContent(); + } +} \ No newline at end of file diff --git a/DataStore/AddressStore.cs b/DataStore/AddressStore.cs new file mode 100644 index 0000000..02f6ee9 --- /dev/null +++ b/DataStore/AddressStore.cs @@ -0,0 +1,24 @@ +using AddressBookApi.Models.Dto; + +namespace AddressBookApi.DataStore; + +public static class AddressStore +{ + public static List addressList = new List + { + new AddressDto + { + Id = 1, Name = "Fred Sanford", Street = "10 ItsTheBigOne Avenue", City = "Los Angeles", + PostCode = "90201" + }, + new AddressDto + { + Id = 2, Name = "Barney Fife", Street = "25 AweShucksAndy Street", City = "Mayberry", PostCode = "012345" + }, + new AddressDto + { + Id = 3, Name = "Greg Gauthier", Street = "84 Lizmans Court; Silkdale Close", City = "Oxford", + PostCode = "OX4 2HG" + } + }; +} \ No newline at end of file diff --git a/Models/Address.cs b/Models/Address.cs new file mode 100644 index 0000000..6d73ef0 --- /dev/null +++ b/Models/Address.cs @@ -0,0 +1,11 @@ +namespace AddressBookApi.Models; + +public class Address +{ + public int Id { get; set; } + public DateTime CreatedDateTime { get; set; } + public string? Name { get; set; } + public string? Street { get; set; } + public string? City { get; set; } + public string? PostCode { get; set; } +} \ No newline at end of file diff --git a/Models/Dto/AddressDto.cs b/Models/Dto/AddressDto.cs new file mode 100644 index 0000000..85d0bbd --- /dev/null +++ b/Models/Dto/AddressDto.cs @@ -0,0 +1,24 @@ +using System.ComponentModel.DataAnnotations; + +namespace AddressBookApi.Models.Dto; + +public class AddressDto +{ + public int Id { get; set; } + + [Required] + [MaxLength(50)] + public string? Name { get; set; } + + [Required] + [MaxLength(100)] + public string? Street { get; set; } + + [Required] + [MaxLength(50)] + public string? City { get; set; } + + [Required] + [MaxLength(18)] + public string? PostCode { get; set; } +} \ No newline at end of file