address book without the put

This commit is contained in:
Greg Gauthier 2023-01-04 17:37:30 +00:00
parent 6c64758777
commit 9bb2e8c8a5
6 changed files with 179 additions and 5 deletions

View File

@ -11,9 +11,4 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Controllers" />
<Folder Include="Models" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Lizmans/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mayberry/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Silkdale/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -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<IEnumerable<AddressDto>> GetAddresses()
{
return Ok(AddressStore.addressList);
}
[HttpGet("{id:int}", Name = "GetAddressById")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public ActionResult<AddressDto> 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<AddressDto> 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<AddressDto> 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();
}
}

24
DataStore/AddressStore.cs Normal file
View File

@ -0,0 +1,24 @@
using AddressBookApi.Models.Dto;
namespace AddressBookApi.DataStore;
public static class AddressStore
{
public static List<AddressDto> addressList = new List<AddressDto>
{
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"
}
};
}

11
Models/Address.cs Normal file
View File

@ -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; }
}

24
Models/Dto/AddressDto.cs Normal file
View File

@ -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; }
}