96 lines
2.3 KiB
C#
96 lines
2.3 KiB
C#
namespace MyOffice.Web.Controllers;
|
|
|
|
using Core.Extensions;
|
|
using Data.Models.Currencies;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Data.Repositories.Currency;
|
|
using Models.Currency;
|
|
using Services.Currency;
|
|
using Services.Currency.Domain;
|
|
using MyOffice.Core;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class CurrencyController : BaseApiController
|
|
{
|
|
private readonly CurrencyService _currencyService;
|
|
private readonly ILogger<CurrencyController> _logger;
|
|
|
|
public CurrencyController(
|
|
CurrencyService currencyService,
|
|
ILogger<CurrencyController> logger
|
|
)
|
|
{
|
|
_currencyService = currencyService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("~/api/settings/currencies")]
|
|
public object Get()
|
|
{
|
|
var list = _currencyService.GetAllWithRates(UserId);
|
|
|
|
return list.Select(x => x.Key.ToModel(x.Value));
|
|
}
|
|
|
|
[HttpPost("~/api/settings/currencies")]
|
|
public object Post(CurrencyAddModel currency)
|
|
{
|
|
var exec = _currencyService.AddCurrency(UserId, new Currency
|
|
{
|
|
UserId = UserId,
|
|
CurrencyGlobalId = currency.Id,
|
|
Name = currency.Name,
|
|
ShortName = currency.ShortName,
|
|
});
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case AddCurrencyStatus.success:
|
|
case AddCurrencyStatus.exists:
|
|
_currencyService.AddCurrencyRate(UserId, exec.Result!.Id, new CurrencyRate()
|
|
{
|
|
CurrencyId = exec.Result!.Id,
|
|
Rate = currency.Rate,
|
|
Quantity = currency.Quantity,
|
|
DateTime = currency.RateDate.Date,
|
|
});
|
|
return currency;
|
|
|
|
case AddCurrencyStatus.failed:
|
|
return ProblemBadRequest("Adding currency failed.");
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpPost("~/api/settings/currencies/{id}/rate")]
|
|
public object Post(string id, CurrencyRateModel currencyRate)
|
|
{
|
|
var exec = _currencyService.AddCurrencyRate(UserId, id.AsGuid(), new CurrencyRate()
|
|
{
|
|
CurrencyId = id.AsGuid(),
|
|
Quantity = currencyRate.Quantity,
|
|
Rate = currencyRate.Rate,
|
|
DateTime = currencyRate.RateDate.Date,
|
|
});
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case CurrencyAddRateStatus.failed:
|
|
return ProblemBadRequest("Adding currency rate failed.");
|
|
case CurrencyAddRateStatus.not_found:
|
|
return ProblemBadRequest("Currency not found.");
|
|
|
|
case CurrencyAddRateStatus.success:
|
|
return exec.Result!;
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
}
|