CodeLiturgy.Dashboard/BlueWest.Api/Controllers/CountriesController.cs

121 lines
3.3 KiB
C#
Raw Normal View History

2022-08-13 06:35:36 +03:00
using System.Linq;
using BlueWest.Data;
using BlueWest.WebApi.MySQL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2022-08-17 23:21:00 +03:00
namespace BlueWest.WebApi.Controllers
{
2022-08-13 06:35:36 +03:00
[ApiController]
[Route("[controller]")]
2022-08-17 23:21:00 +03:00
public class CountriesController : ControllerBase
2022-08-13 06:35:36 +03:00
{
private readonly CountriesDbContext _dbContext;
2022-08-17 23:17:37 +03:00
/// <summary>
/// Controller responsible for handling country data in the Country table
/// </summary>
/// <param name="dbContext"></param>
2022-08-17 23:21:00 +03:00
public CountriesController(CountriesDbContext dbContext)
2022-08-13 06:35:36 +03:00
{
_dbContext = dbContext;
}
2022-08-13 20:15:43 +03:00
/// <summary>
/// Add Country
/// </summary>
/// <param name="country"></param>
/// <returns>The newly created country</returns>
/// /// <summary>
/// Creates a Country.
/// </summary>
/// <remarks>
/// Sample request:
///
/// POST /Countries
/// {
/// "code": 1,
/// "stateName": "United States of America",
/// "tld": "us"
/// }
///
/// </remarks>
/// <response code="201">Returns the newly created country</response>
2022-08-13 06:35:36 +03:00
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddCountry(Country country)
{
_dbContext.Countries.Add(country);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country);
}
2022-08-13 20:15:43 +03:00
/// <summary>
/// Updates a Country
/// </summary>
2022-08-17 23:17:37 +03:00
/// <param name="countryCode">The country ISO 3166 code</param>
/// <param name="countryToUpdate">Country payload data</param>
2022-08-13 20:15:43 +03:00
/// <returns></returns>
2022-08-13 06:35:36 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
2022-08-17 23:17:37 +03:00
[HttpPut("{countryCode}")]
public ActionResult UpdateCountry(int countryCode, CountryUpdate countryToUpdate)
2022-08-13 06:35:36 +03:00
{
2022-08-17 23:17:37 +03:00
var country = _dbContext.Countries.FirstOrDefault(x => x.Code == countryCode);
2022-08-13 06:35:36 +03:00
2022-08-13 20:15:43 +03:00
if (country != null)
2022-08-13 06:35:36 +03:00
{
2022-08-17 23:17:37 +03:00
var updatedCountry = new Country(countryToUpdate, countryCode, null);
2022-08-13 20:15:43 +03:00
_dbContext.Countries.Update(updatedCountry);
return Ok(updatedCountry);
2022-08-13 06:35:36 +03:00
}
return new NotFoundResult();
}
2022-08-13 20:15:43 +03:00
/// <summary>
/// Get countries
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2022-08-17 23:21:00 +03:00
[HttpGet]
2022-08-13 20:15:43 +03:00
public ActionResult GetCountries()
{
var array = _dbContext.Countries;
if (array != null)
{
return Ok(array.ToArray());
}
return new NotFoundResult();
}
/// <summary>
/// Get Country by Id
/// </summary>
2022-08-17 23:17:37 +03:00
/// <param name="countryId">ISO 3166-1 country numeric code</param>
2022-08-13 20:15:43 +03:00
/// <returns></returns>
2022-08-13 06:35:36 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2022-08-17 23:17:37 +03:00
[HttpGet("{countryId}", Name = nameof(GetCountryById))]
2022-08-13 06:35:36 +03:00
public ActionResult GetCountryById(int countryId)
{
var array = _dbContext.Countries.FirstOrDefault(x => x.Code == countryId);
if (array != null)
{
return Ok(array);
}
return new NotFoundResult();
}
2022-08-17 23:21:00 +03:00
}
}