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

183 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using BlueWest.Data;
using BlueWest.WebApi.EF;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BlueWest.WebApi.Controllers
{
/// <summary>
/// Controller responsible to get countryId data
/// </summary>
[ApiController]
[Route("[controller]")]
public class CountryController : ControllerBase
{
private readonly CountryDbContext _dbContext;
/// <summary>
/// Controller responsible for handling countryId data in the Country table
/// </summary>
/// <param name="dbContext"></param>
public CountryController(CountryDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// Get countries
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet]
public ActionResult GetCountries()
{
var array = _dbContext
.Countries
.Select(x => new CountryUnique(x))
.ToArray();
return Ok(array);
}
/// <summary>
/// Get Country by Id
/// </summary>
/// <param name="countryId">ISO 3166-1 countryId numeric code</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{countryId}", Name = nameof(GetCountryById))]
public ActionResult GetCountryById(int countryId)
{
var country = _dbContext.Countries
.Where(x => x.Id == countryId)
.Select(x => new CountryUnique(x))
.FirstOrDefault();
if (country != null)
{
return Ok(country);
}
return new NotFoundResult();
}
/// <summary>
/// Get currencies of a countryId
/// </summary>
/// <param name="countryId"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{countryId}/currencies")]
public ActionResult GetCountryCurrencies(int countryId)
{
var countryObj = _dbContext.Countries.FirstOrDefault(d => d.Id == countryId);
if (countryObj == null) return new NotFoundResult();
var array = _dbContext
.Countries
.Where(data => data.Id == countryId)
.SelectMany(o => o.Currencies)
.Select(x => new CurrencyUnique(x))
.ToArray();
return Ok(array);
}
/// <summary>
/// Add Country
/// </summary>
/// <param name="countryToCreate">The countryId data to create</param>
/// <returns>The newly created countryId</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 countryId</response>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
[HttpPost]
public ActionResult AddCountry(CountryCreate countryToCreate)
{
var (success, country) = _dbContext.AddCountry(countryToCreate);
if (!success) return new BadRequestResult();
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Id}, country);
}
/// <summary>
/// Updates a Country
/// </summary>
/// <param name="countryCode">The countryId 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 (success, country) = _dbContext.UpdateCountry(countryToUpdate, countryCode);
if (success)
{
return Ok(country);
}
return new NotFoundResult();
}
/// <summary>
/// Adds a currency to Country
/// </summary>
/// <param name="countryId"></param>
/// <param name="currencyCreate"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[HttpPost("{countryId}/currencies")]
public ActionResult AddCurrency(int countryId, CurrencyCreate currencyCreate)
{
/*var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate,
new Expression<Func<Currency, bool>>[]
{
x => x.Code == currencyCreate.Code,
y => y.Num == currencyCreate.Num
});
if (!result)
{
return new ConflictObjectResult(message);
}
return Ok(country);*/
return null;
}
}
}