126 lines
3.5 KiB
C#
126 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BlueWest.Data;
|
|
using BlueWest.WebApi.MySQL;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller responsible to get country data
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class CountryController : ControllerBase
|
|
{
|
|
private readonly CountriesDbContext _dbContext;
|
|
|
|
/// <summary>
|
|
/// Controller responsible for handling country data in the Country table
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
public CountryController(CountriesDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Add Country
|
|
/// </summary>
|
|
/// <param name="countryToCreate">The country data to create</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>
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[HttpPost]
|
|
public ActionResult AddCountry(CountryCreate countryToCreate)
|
|
{
|
|
var newCountry = new Country(countryToCreate, new List<Currency>());
|
|
_dbContext.Countries.Add(newCountry);
|
|
_dbContext.SaveChanges();
|
|
return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Code}, newCountry);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a Country
|
|
/// </summary>
|
|
/// <param name="countryCode">The country ISO 3166 code</param>
|
|
/// <param name="countryToUpdate">Country payload data</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpPut("{countryCode}")]
|
|
public ActionResult UpdateCountry(int countryCode, CountryUpdate countryToUpdate)
|
|
{
|
|
|
|
var country = _dbContext.Countries.FirstOrDefault(x => x.Code == countryCode);
|
|
|
|
if (country != null)
|
|
{
|
|
var updatedCountry = new Country(countryToUpdate, countryCode, null);
|
|
_dbContext.Countries.Update(updatedCountry);
|
|
return Ok(updatedCountry);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get countries
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet]
|
|
public ActionResult GetCountries()
|
|
{
|
|
var array = _dbContext.Countries;
|
|
|
|
if (array != null)
|
|
{
|
|
return Ok(array.ToArray());
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Country by Id
|
|
/// </summary>
|
|
/// <param name="countryId">ISO 3166-1 country numeric code</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{countryId}", Name = nameof(GetCountryById))]
|
|
public ActionResult GetCountryById(int countryId)
|
|
{
|
|
var array = _dbContext.Countries.FirstOrDefault(x => x.Code == countryId);
|
|
|
|
if (array != null)
|
|
{
|
|
return Ok(array);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
}
|
|
}
|