204 lines
6.2 KiB
C#
204 lines
6.2 KiB
C#
using BlueWest.Domain;
|
|
using BlueWest.Data;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller responsible to get countryId data
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
[Authorize(Policy = SessionConstants.ApiNamePolicy)]
|
|
[EnableCors(Constants.CorsPolicyName)]
|
|
[ServiceFilter(typeof(SessionAuthorizationFilter))]
|
|
|
|
// [Authorize(Roles = "Administrator")]
|
|
public class CountryController : ControllerBase
|
|
{
|
|
|
|
#region Initialization
|
|
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;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public override AcceptedAtActionResult AcceptedAtAction(string actionName, string controllerName)
|
|
{
|
|
return base.AcceptedAtAction(actionName, controllerName);
|
|
}
|
|
|
|
#region GetCountries
|
|
|
|
/// <summary>
|
|
/// Get countries
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet]
|
|
public ActionResult GetCountries(
|
|
int skip = 0,
|
|
int take = 50,
|
|
int orderDir = 1)
|
|
{
|
|
|
|
var (success, countries) = _dbContext.GetCountries( skip, take, orderDir);
|
|
if (!success) return new NotFoundResult();
|
|
return Ok(countries);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetCountryById
|
|
|
|
/// <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 (success, country) = _dbContext.GetOneCountryById(countryId);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(country);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetCountryCurrencies
|
|
|
|
/// <summary>
|
|
/// Get currencies of a countryId
|
|
/// </summary>
|
|
/// <param name="countryId"></param>
|
|
/// <param name="skip">How many records to skip</param>
|
|
/// <param name="take">How many records to take</param>
|
|
/// <param name="orderDir">The order direction</param>
|
|
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{countryId}/currencies")]
|
|
public ActionResult GetCountryCurrencies(int countryId, int skip = 0, int take = 50, int orderDir = 1)
|
|
{
|
|
var (success, result) = _dbContext.GetCountryCurrencies(countryId, skip, take, orderDir);
|
|
|
|
if(success)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AddCountry
|
|
|
|
/// <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);
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region UpdateCountry
|
|
|
|
/// <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();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AddCurrencyToCountry
|
|
|
|
/// <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 AddCurrencyToCountry(int countryId, CurrencyCreate currencyCreate)
|
|
{
|
|
var (success, message, newCurrency) = _dbContext.AddCurrencyToCountry(countryId, currencyCreate);
|
|
|
|
if(success)
|
|
{
|
|
return Ok(newCurrency);
|
|
}
|
|
|
|
return new NotFoundObjectResult(message);
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
} |