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

101 lines
3.3 KiB
C#
Raw Normal View History

2022-08-17 23:17:37 +03:00
using System.Collections.Generic;
2022-08-18 22:59:13 +03:00
using System.Collections.Immutable;
2022-08-13 06:35:36 +03:00
using System.Linq;
using BlueWest.Data;
2022-08-22 00:14:50 +03:00
using BlueWest.WebApi.EF;
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-08-22 00:14:50 +03:00
internal class CurrencyController : ControllerBase
2022-08-13 06:35:36 +03:00
{
2022-08-19 06:22:55 +03:00
private readonly CountryDbContext _dbContext;
2022-08-20 05:47:32 +03:00
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-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]
public ActionResult GetCurrencies()
{
2022-08-22 00:14:50 +03:00
var currencies = _dbContext.Currencies.ToArray();
return Ok(currencies);
2022-08-18 22:59:13 +03:00
}
2022-08-13 06:35:36 +03:00
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>
2022-08-18 22:59:13 +03:00
/// <param name="currencyAssociatedCountries">Countries</param>
2022-08-18 18:46:20 +03:00
/// <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-08-18 22:59:13 +03:00
2022-08-22 00:14:50 +03:00
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
if (!success)
{
return new NotFoundResult();
}
2022-08-18 22:59:13 +03:00
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, newCurrency);
2022-08-13 06:35:36 +03:00
}
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-22 00:14:50 +03:00
var (success, currency) = _dbContext.UpdateCurrency(currencyNumber, currencyToUpdate);
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-22 00:14:50 +03:00
return Ok(currency);
2022-08-17 23:21:00 +03:00
}
return new NotFoundResult();
}
2022-08-18 18:46:20 +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>
2022-08-17 23:21:00 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
public ActionResult GetCurrencyById(int currencyId)
{
var currency = _dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
2022-08-17 23:21:00 +03:00
2022-08-22 00:14:50 +03:00
if (currency != null)
2022-08-17 23:21:00 +03:00
{
2022-08-22 00:14:50 +03:00
return Ok(currency);
2022-08-17 23:21:00 +03:00
}
return new NotFoundResult();
2022-08-13 06:35:36 +03:00
}
}
}