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

145 lines
4.6 KiB
C#
Raw Normal View History

2022-08-18 18:46:20 +03:00
using System.Collections.Generic;
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-17 23:17:37 +03:00
/// <summary>
2022-08-19 06:18:50 +03:00
/// Controller responsible to get country data
2022-08-17 23:17:37 +03:00
/// </summary>
2022-08-19 06:18:50 +03:00
[ApiController]
[Route("[controller]")]
public class CountryController : ControllerBase
2022-08-13 06:35:36 +03:00
{
2022-08-19 06:22:55 +03:00
private readonly CountryDbContext _dbContext;
2022-08-13 06:35:36 +03:00
2022-08-19 06:18:50 +03:00
/// <summary>
/// Controller responsible for handling country data in the Country table
/// </summary>
/// <param name="dbContext"></param>
2022-08-19 06:22:55 +03:00
public CountryController(CountryDbContext dbContext)
2022-08-19 06:18:50 +03:00
{
_dbContext = dbContext;
}
2022-08-13 06:35:36 +03:00
2022-08-19 06:18:50 +03:00
/// <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)
2022-08-13 06:35:36 +03:00
{
2022-08-19 06:18:50 +03:00
var newCountry = countryToCreate.ToCountry();
_dbContext.Countries.Add(newCountry);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Code}, newCountry);
2022-08-13 06:35:36 +03:00
}
2022-08-19 06:18:50 +03:00
/// <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)
2022-08-13 20:15:43 +03:00
{
2022-08-19 06:18:50 +03:00
var country = _dbContext.Countries.FirstOrDefault(x => x.Code == countryCode);
if (country != null)
{
var updatedCountry = new Country(countryToUpdate, countryCode, null, null);
_dbContext.Countries.Update(updatedCountry);
return Ok(updatedCountry);
}
return new NotFoundResult();
2022-08-13 20:15:43 +03:00
}
2022-08-19 06:18:50 +03:00
/// <summary>
/// Get countries
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet]
public ActionResult GetCountries()
2022-08-13 06:35:36 +03:00
{
2022-08-19 06:18:50 +03:00
var array = _dbContext.Countries;
if (array != null)
{
return Ok(array.ToArray());
}
return new NotFoundResult();
2022-08-13 06:35:36 +03:00
}
2022-08-19 06:18:50 +03:00
/// <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();
}
/// <summary>
/// Get currencies of a country
/// </summary>
/// <param name="countryId"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{countryId}/currencies")]
public ActionResult GetCountryCurrencies(int countryId)
{
var country = _dbContext.Countries.FirstOrDefault(d => d.Code == countryId);
if (country == null) return new NotFoundResult();
var array = _dbContext
.Countries
.Where(data => data.Code == countryId)
.SelectMany(o => o.Currencies)
.ToArray();
2022-08-13 06:35:36 +03:00
2022-08-19 06:18:50 +03:00
return Ok(array);
}
}
}