2022-08-17 23:17:37 +03:00
|
|
|
using System.Collections.Generic;
|
2022-08-13 06:35:36 +03:00
|
|
|
using System.Linq;
|
|
|
|
using BlueWest.Data;
|
|
|
|
using BlueWest.WebApi.MySQL;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers;
|
|
|
|
|
|
|
|
public class CurrencyController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly CountriesDbContext _dbContext;
|
|
|
|
|
|
|
|
public CurrencyController(CountriesDbContext dbContext)
|
|
|
|
{
|
|
|
|
_dbContext = dbContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
[HttpPost]
|
|
|
|
public ActionResult AddCurrency(Currency currency)
|
|
|
|
{
|
|
|
|
_dbContext.Currencies.Add(currency);
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currency.Code}, currency);
|
|
|
|
}
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2022-08-17 23:17:37 +03:00
|
|
|
[HttpPut("countries/{currencyNumber}")]
|
|
|
|
public ActionResult UpdateCurrency(int currencyNumber, CurrencyUpdate currencyToUpdate)
|
2022-08-13 06:35:36 +03:00
|
|
|
{
|
|
|
|
|
2022-08-17 23:17:37 +03:00
|
|
|
var currency = _dbContext.Currencies.FirstOrDefault(x => x.Num ==currencyNumber);
|
2022-08-13 06:35:36 +03:00
|
|
|
|
2022-08-17 23:17:37 +03:00
|
|
|
if (currency != null)
|
2022-08-13 06:35:36 +03:00
|
|
|
{
|
2022-08-17 23:17:37 +03:00
|
|
|
var updatedCurrency = new Currency(currencyToUpdate, currencyNumber, new List<Country>());
|
|
|
|
_dbContext.Update(updatedCurrency);
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
return Ok(updatedCurrency);
|
2022-08-13 06:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
[HttpGet("countries/{CurrencyId}", Name = nameof(GetCurrencyById))]
|
|
|
|
public ActionResult GetCurrencyById(int CurrencyId)
|
|
|
|
{
|
|
|
|
var array = _dbContext.Countries.FirstOrDefault(x => x.Code == CurrencyId);
|
|
|
|
|
|
|
|
if (array != null)
|
|
|
|
{
|
|
|
|
return Ok(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
}
|