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

143 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
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>
/// The controller responsible to fetch currency data
/// </summary>
[ApiController]
[Route("[controller]")]
public class CurrencyController : ControllerBase
{
private readonly CountryDbContext _dbContext;
/// <summary>
/// Api Controller for Currency data
/// </summary>
/// <param name="dbContext"></param>
public CurrencyController(CountryDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// Gets the currency data from currency table
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet]
public ActionResult GetCurrencies()
{
var currencies = _dbContext.Currencies
.Select(currency => new CurrencyUnique(currency))
.ToArray();
return Ok(currencies);
}
/// <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 currency = _dbContext
.Currencies
.Where(x => x.Id == currencyId)
.Select(x => new CurrencyUnique(x))
.FirstOrDefault();
if (currency != null)
{
return Ok(currency);
}
return new NotFoundResult();
}
/// <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}, new CurrencyUnique(newCurrency));
}
/// <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(currencyNumber, currencyToUpdate);
if (success)
{
return Ok(new CurrencyUnique(currency));
}
return new NotFoundResult();
}
/// <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 AddCountry(int currencyId, CountryCreate countryToCreate)
{
var (result, message, country) = _dbContext.AddCountry(currencyId, countryToCreate,
new Expression<Func<Country, bool>>[]
{
x => x.Code == countryToCreate.Code,
y => y.StateName == countryToCreate.StateName,
z => z.Alpha2Code == countryToCreate.Alpha2Code
});
if (!result)
{
return new ConflictObjectResult(message);
}
return Ok(country);
}
}
}