205 lines
6.3 KiB
C#
205 lines
6.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using BlueWest.Data;
|
|
using BlueWest.WebApi.EF;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// The controller responsible to fetch currency data
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
|
|
public partial class CurrencyController : ControllerBase
|
|
{
|
|
|
|
#region Initialization
|
|
private readonly CountryDbContext _dbContext;
|
|
|
|
/// <summary>
|
|
/// Api Controller for Currency data
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
public CurrencyController(CountryDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetCurrencies
|
|
/// <summary>
|
|
/// Gets the currency data from currency table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet]
|
|
public ActionResult GetCurrencies(int skip = 0, int take = 50, int orderDir = 1)
|
|
{
|
|
var (success, result) = _dbContext.GetCurrencies(skip, take, orderDir);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetCurrencyById
|
|
/// <summary>
|
|
/// Gets a currency by the currency number (id)
|
|
/// </summary>
|
|
/// <param name="currencyId">The id of the currency to get</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
|
|
public ActionResult GetCurrencyById(int currencyId)
|
|
{
|
|
var (success, result) = _dbContext.GetOneCurrencyById(currencyId);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetCurrencyWithCode
|
|
|
|
/// <summary>
|
|
/// Gets a currency by code.
|
|
/// </summary>
|
|
/// <param name="currencyCode">The currency Code </param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{currencyCode}", Name = nameof(GetCurrencyWithCode))]
|
|
public ActionResult GetCurrencyWithCode(string currencyCode)
|
|
{
|
|
var (success, currency) =
|
|
_dbContext.GetCurrencyWith(x => x.Code == currencyCode);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(currency);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AddCurrency
|
|
/// <summary>
|
|
/// Add Currency to the table of currencies
|
|
/// </summary>
|
|
/// <param name="currencyToCreate">Currency data to create</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[HttpPost]
|
|
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
|
|
{
|
|
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
|
|
|
|
if (!success)
|
|
{
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
return CreatedAtRoute(nameof(GetCurrencyById), new { CurrencyId = newCurrency.Code }, newCurrency);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region UpdateCurrency
|
|
/// <summary>
|
|
/// Update a currency data from the Currency table in the database
|
|
/// </summary>
|
|
/// <param name="currencyNumber">The currency number we want to update</param>
|
|
/// <param name="currencyToUpdate">Currency Data to update</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpPut("{currencyNumber}")]
|
|
public ActionResult UpdateCurrency(int currencyNumber, CurrencyUpdate currencyToUpdate)
|
|
{
|
|
var (success, currency) = _dbContext.UpdateCurrency(currencyToUpdate, currencyNumber);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(currency);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetCountryFromCurrency
|
|
/// <summary>
|
|
/// Gets a specific country id in a country
|
|
/// </summary>
|
|
/// <param name="currencyId">The id of the currency</param>
|
|
/// <param name="countryId">The id of the country</param>
|
|
/// <returns></returns>
|
|
/*
|
|
* [GetOneFrom(nameof(Currency.Id), nameof(Country.Id), typeof(CountryUnique))]
|
|
*/
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCountryFromCurrency))]
|
|
public ActionResult GetCountryFromCurrency(int currencyId, int countryId)
|
|
{
|
|
var (success, country) = _dbContext.GetCountryFromCurrency(currencyId, countryId);
|
|
|
|
|
|
if (success)
|
|
{
|
|
return Ok(country);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AddCountry
|
|
/// <summary>
|
|
/// Add Currency to the table of currencies
|
|
/// </summary>
|
|
/// <param name="countryToCreate"></param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[HttpPost("{currencyId}/countries")]
|
|
public ActionResult AddCountry(CountryCreate countryToCreate)
|
|
{
|
|
var (success, result) = _dbContext.AddCountry(countryToCreate);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
return new BadRequestResult();
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
} |