using System; 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 { /// /// The controller responsible to fetch currency data /// [ApiController] [Route("[controller]")] public partial class CurrencyController : ControllerBase { private readonly CountryDbContext _dbContext; /// /// Api Controller for Currency data /// /// public CurrencyController(CountryDbContext dbContext) { _dbContext = dbContext; } /// /// Gets the currency data from currency table /// /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet] public ActionResult GetCurrencies() { var currencies = _dbContext.Currencies .Select(currency => new CurrencyUnique(currency)) .ToArray(); return Ok(currencies); } /// /// Gets a currency by the currency number (id) /// /// The id of the currency to get /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("{currencyId}", Name = nameof(GetCurrencyById))] public ActionResult GetCurrencyById(int currencyId) { var queryResult = from aCurrency in _dbContext.Currencies where aCurrency.Id == currencyId select new CurrencyUnique(aCurrency); var currency = queryResult.SingleOrDefault(); if (currency != null) { return Ok(currency); } return new NotFoundResult(); } /// /// Add Currency to the table of currencies /// /// Currency data to create /// [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); } /// /// Update a currency data from the Currency table in the database /// /// The currency number we want to update /// Currency Data to update /// [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(); } /// /// Gets a specific country id in a country /// /// The id of the currency /// The id of the country /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCurrencyById))] public ActionResult GetCountry(int currencyId, int countryId) { var countryQuery = from aCurrency in _dbContext.Currencies where aCurrency.Id == currencyId let countries = aCurrency.Countries from country in countries where country.Id == countryId select new CountryUnique(country); var queryResult = countryQuery.SingleOrDefault(); if (queryResult == null) { return new NotFoundResult(); } return Ok(queryResult); } /// /// Add Currency to the table of currencies /// /// /// /// [ProducesResponseType(StatusCodes.Status201Created)] [HttpPost("{currencyId}/countries")] public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate) { /*var (success, message, country) = _dbContext.AddCountry(currencyId, countryToCreate, new Expression>[] { x => x.Code == countryToCreate.Code, y => y.StateName == countryToCreate.StateName, z => z.Alpha2Code == countryToCreate.Alpha2Code }); if (!success) { return new ConflictObjectResult(message); } return Ok(country);*/ return null; } } }