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

209 lines
6.4 KiB
C#
Raw Normal View History

2022-08-22 05:57:06 +03:00
using System;
2022-08-13 06:35:36 +03:00
using System.Linq;
2022-08-22 05:57:06 +03:00
using System.Linq.Expressions;
2022-08-13 06:35:36 +03:00
using BlueWest.Data;
2022-08-22 00:14:50 +03:00
using BlueWest.WebApi.EF;
2022-09-10 07:12:03 +03:00
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
2022-09-17 22:13:35 +03:00
using Microsoft.AspNetCore.Cors;
2022-08-13 06:35:36 +03:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2022-08-17 23:21:00 +03:00
namespace BlueWest.WebApi.Controllers
2022-08-13 06:35:36 +03:00
{
2022-08-18 18:46:20 +03:00
/// <summary>
/// The controller responsible to fetch currency data
/// </summary>
2022-08-17 23:21:00 +03:00
[ApiController]
[Route("[controller]")]
2022-09-10 07:12:03 +03:00
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
2022-09-11 01:22:04 +03:00
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
2022-09-17 22:13:35 +03:00
[EnableCors(Constants.CorsPolicyName)]
2022-09-12 17:57:37 +03:00
// [Authorize(Roles = "Administrator")]
2022-08-22 18:02:48 +03:00
public partial class CurrencyController : ControllerBase
2022-08-13 06:35:36 +03:00
{
2022-09-06 00:42:45 +03:00
#region Initialization
2022-08-19 06:22:55 +03:00
private readonly CountryDbContext _dbContext;
2022-08-22 18:02:48 +03:00
2022-08-22 02:51:45 +03:00
/// <summary>
/// Api Controller for Currency data
/// </summary>
/// <param name="dbContext"></param>
2022-08-19 06:22:55 +03:00
public CurrencyController(CountryDbContext dbContext)
2022-08-17 23:21:00 +03:00
{
_dbContext = dbContext;
}
2022-08-13 06:35:36 +03:00
2022-09-06 00:42:45 +03:00
#endregion
2022-09-09 06:26:16 +03:00
#region GetCurrencies
2022-08-18 22:59:13 +03:00
/// <summary>
/// Gets the currency data from currency table
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet]
2022-09-09 06:26:16 +03:00
public ActionResult GetCurrencies(int skip = 0, int take = 50, int orderDir = 1)
2022-08-18 22:59:13 +03:00
{
2022-09-10 07:12:03 +03:00
var (success, result) = _dbContext.GetCurrencies(skip, take, orderDir);
2022-08-22 18:02:48 +03:00
2022-09-09 06:26:16 +03:00
if (success)
{
return Ok(result);
}
return new NotFoundResult();
2022-08-18 22:59:13 +03:00
}
2022-08-22 18:02:48 +03:00
2022-09-09 06:26:16 +03:00
#endregion
2022-08-22 18:02:48 +03:00
2022-09-09 06:26:16 +03:00
#region GetCurrencyById
2022-08-22 05:57:06 +03:00
/// <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)
{
2022-09-09 06:26:16 +03:00
var (success, result) = _dbContext.GetOneCurrencyById(currencyId);
2022-08-22 05:57:06 +03:00
2022-09-09 06:26:16 +03:00
if (success)
2022-08-22 05:57:06 +03:00
{
2022-09-09 06:26:16 +03:00
return Ok(result);
2022-08-22 05:57:06 +03:00
}
return new NotFoundResult();
}
2022-09-09 06:26:16 +03:00
#endregion
#region GetCurrencyWithCode
2022-09-06 00:42:45 +03:00
/// <summary>
2022-09-10 00:33:17 +03:00
/// Gets a currency by code.
2022-09-06 00:42:45 +03:00
/// </summary>
2022-09-10 00:33:17 +03:00
/// <param name="currencyCode">The currency Code </param>
2022-09-06 00:42:45 +03:00
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyCode}", Name = nameof(GetCurrencyWithCode))]
2022-09-06 01:37:52 +03:00
public ActionResult GetCurrencyWithCode(string currencyCode)
2022-09-06 00:42:45 +03:00
{
2022-09-06 01:37:52 +03:00
var (success, currency) =
_dbContext.GetCurrencyWith(x => x.Code == currencyCode);
2022-09-06 00:42:45 +03:00
2022-09-06 01:37:52 +03:00
if (success)
2022-09-06 00:42:45 +03:00
{
return Ok(currency);
}
return new NotFoundResult();
}
2022-08-13 06:35:36 +03:00
2022-09-09 06:26:16 +03:00
#endregion
#region AddCurrency
2022-08-18 18:46:20 +03:00
/// <summary>
/// Add Currency to the table of currencies
/// </summary>
/// <param name="currencyToCreate">Currency data to create</param>
/// <returns></returns>
2022-08-17 23:21:00 +03:00
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
2022-08-18 18:46:20 +03:00
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
2022-08-13 06:35:36 +03:00
{
2022-09-01 08:54:42 +03:00
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
2022-08-22 00:14:50 +03:00
if (!success)
{
return new NotFoundResult();
}
2022-08-22 18:02:48 +03:00
2022-09-09 06:26:16 +03:00
return CreatedAtRoute(nameof(GetCurrencyById), new { CurrencyId = newCurrency.Code }, newCurrency);
2022-08-24 19:56:51 +03:00
2022-08-13 06:35:36 +03:00
}
2022-09-09 06:26:16 +03:00
#endregion
#region UpdateCurrency
2022-08-18 18:46:20 +03:00
/// <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>
2022-08-17 23:21:00 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPut("{currencyNumber}")]
public ActionResult UpdateCurrency(int currencyNumber, CurrencyUpdate currencyToUpdate)
2022-08-13 06:35:36 +03:00
{
2022-08-29 03:40:57 +03:00
var (success, currency) = _dbContext.UpdateCurrency(currencyToUpdate, currencyNumber);
2022-08-17 23:21:00 +03:00
2022-08-22 00:14:50 +03:00
if (success)
2022-08-17 23:21:00 +03:00
{
2022-08-29 03:40:57 +03:00
return Ok(currency);
2022-08-17 23:21:00 +03:00
}
return new NotFoundResult();
}
2022-08-23 19:48:16 +03:00
2022-09-09 06:26:16 +03:00
#endregion
#region GetCountryFromCurrency
2022-08-22 18:02:48 +03:00
/// <summary>
2022-08-23 19:48:16 +03:00
/// Gets a specific country id in a country
2022-08-22 18:02:48 +03:00
/// </summary>
2022-08-23 19:48:16 +03:00
/// <param name="currencyId">The id of the currency</param>
/// <param name="countryId">The id of the country</param>
2022-08-22 18:02:48 +03:00
/// <returns></returns>
2022-09-09 06:26:16 +03:00
/*
* [GetOneFrom(nameof(Currency.Id), nameof(Country.Id), typeof(CountryUnique))]
*/
2022-08-22 18:02:48 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2022-09-06 07:54:48 +03:00
[HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCountryFromCurrency))]
public ActionResult GetCountryFromCurrency(int currencyId, int countryId)
2022-08-22 18:02:48 +03:00
{
2022-09-09 06:26:16 +03:00
var (success, country) = _dbContext.GetCountryFromCurrency(currencyId, countryId);
if (success)
2022-08-22 18:02:48 +03:00
{
2022-09-09 06:26:16 +03:00
return Ok(country);
2022-08-22 18:02:48 +03:00
}
2022-09-09 06:26:16 +03:00
return new NotFoundResult();
2022-08-22 18:02:48 +03:00
}
2022-09-09 06:26:16 +03:00
#endregion
#region AddCountry
2022-08-18 18:46:20 +03:00
/// <summary>
2022-08-22 05:57:06 +03:00
/// Add Currency to the table of currencies
2022-08-18 18:46:20 +03:00
/// </summary>
2022-08-22 18:02:48 +03:00
/// <param name="countryToCreate"></param>
2022-08-18 18:46:20 +03:00
/// <returns></returns>
2022-08-22 05:57:06 +03:00
[ProducesResponseType(StatusCodes.Status201Created)]
2022-08-22 18:02:48 +03:00
[HttpPost("{currencyId}/countries")]
2022-09-09 06:26:16 +03:00
public ActionResult AddCountry(CountryCreate countryToCreate)
2022-08-17 23:21:00 +03:00
{
2022-09-09 06:26:16 +03:00
var (success, result) = _dbContext.AddCountry(countryToCreate);
2022-08-17 23:21:00 +03:00
2022-09-09 06:26:16 +03:00
if (success)
2022-08-17 23:21:00 +03:00
{
2022-09-09 06:26:16 +03:00
return Ok(result);
2022-08-17 23:21:00 +03:00
}
2022-09-09 06:26:16 +03:00
return new BadRequestResult();
2022-08-13 06:35:36 +03:00
}
2022-09-09 06:26:16 +03:00
#endregion
2022-08-13 06:35:36 +03:00
}
}