121 lines
3.0 KiB
C#
121 lines
3.0 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
|
|
.OrderBy(x => x.Key.CurrencyGlobalId)
|
|
.Select(x => x.Key.ToModel(x.Value));
|
|
}
|
|
|
|
[HttpPost("~/api/settings/currencies")]
|
|
public object Post(CurrencyAddModel currency)
|
|
{
|
|
var exec = _currencyService.CurrencyAdd(UserId, new Currency
|
|
{
|
|
UserId = UserId,
|
|
CurrencyGlobalId = currency.Id,
|
|
Name = currency.Name,
|
|
ShortName = currency.ShortName,
|
|
});
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case CurrencyAddStatus.success:
|
|
case CurrencyAddStatus.exists:
|
|
_currencyService.CurrencyRateAdd(UserId, exec.Result!.Id, new CurrencyRate()
|
|
{
|
|
CurrencyId = exec.Result!.Id,
|
|
Rate = currency.Rate,
|
|
Quantity = currency.Quantity,
|
|
DateTime = currency.RateDate.Date,
|
|
});
|
|
return currency;
|
|
|
|
case CurrencyAddStatus.failed:
|
|
return ProblemBadRequest("Adding currency failed.");
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpPut("~/api/settings/currencies/{id}")]
|
|
public object Put(string id, CurrencyEditModel currency)
|
|
{
|
|
var exec = _currencyService.CurrencyUpdate(
|
|
UserId,
|
|
id.AsGuid(),
|
|
new CurrencyEdit { Name = currency.Name, ShortName = currency.ShortName, IsPrimary = currency.IsPrimary }
|
|
);
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case CurrencyEditStatus.success:
|
|
return exec.Result!.ToModel();
|
|
|
|
case CurrencyEditStatus.not_found:
|
|
case CurrencyEditStatus.failed:
|
|
return ProblemBadRequest("Currency not found.");
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpPost("~/api/settings/currencies/{id}/rate")]
|
|
public object Post(string id, CurrencyRateModel currencyRate)
|
|
{
|
|
var exec = _currencyService.CurrencyRateAdd(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());
|
|
}
|
|
}
|
|
}
|