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

66 lines
2.1 KiB
C#
Raw Normal View History

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;
2022-08-17 23:21:00 +03:00
namespace BlueWest.WebApi.Controllers
2022-08-13 06:35:36 +03:00
{
2022-08-17 23:21:00 +03:00
[ApiController]
[Route("[controller]")]
public class CurrenciesController : ControllerBase
2022-08-13 06:35:36 +03:00
{
2022-08-17 23:21:00 +03:00
private readonly CountriesDbContext _dbContext;
2022-08-13 06:35:36 +03:00
2022-08-17 23:21:00 +03:00
public CurrenciesController(CountriesDbContext dbContext)
{
_dbContext = dbContext;
}
2022-08-13 06:35:36 +03:00
2022-08-17 23:21:00 +03:00
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddCurrency(CurrencyCreate currency)
2022-08-13 06:35:36 +03:00
{
2022-08-17 23:21:00 +03:00
var newCurrency = new Currency();
_dbContext.Currencies.Add(newCurrency);
2022-08-17 23:17:37 +03:00
_dbContext.SaveChanges();
2022-08-17 23:21:00 +03:00
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currency.Code}, currency);
2022-08-13 06:35:36 +03:00
}
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-17 23:21:00 +03:00
var currency = _dbContext.Currencies.FirstOrDefault(x => x.Num == currencyNumber);
if (currency != null)
{
var updatedCurrency = new Currency(currencyToUpdate, currencyNumber, new List<Country>());
_dbContext.Update(updatedCurrency);
_dbContext.SaveChanges();
return Ok(updatedCurrency);
}
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{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();
2022-08-13 06:35:36 +03:00
}
}
}