fix
This commit is contained in:
@@ -1,71 +1,85 @@
|
||||
namespace MyOffice.Services.Currency;
|
||||
|
||||
using AutoMapper;
|
||||
|
||||
using Core;
|
||||
using Core.Extensions;
|
||||
using Data.Models.Currencies;
|
||||
using Data.Repositories.Currency;
|
||||
using MyOffice.Services.Currency.Domain;
|
||||
using Domain;
|
||||
|
||||
public class CurrencyService
|
||||
{
|
||||
private readonly ICurrencyGlobalRepository _currencyGlobalRepository;
|
||||
private readonly ICurrencyRepository _currencyRepository;
|
||||
private readonly ICurrencyRateRepository _currencyRateRepository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public CurrencyService(
|
||||
ICurrencyGlobalRepository currencyGlobalRepository,
|
||||
ICurrencyRepository currencyRepository,
|
||||
ICurrencyRateRepository currencyRateRepository
|
||||
ICurrencyRateRepository currencyRateRepository,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
_currencyGlobalRepository = currencyGlobalRepository;
|
||||
_currencyRepository = currencyRepository;
|
||||
_currencyRateRepository = currencyRateRepository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public List<CurrencyGlobal> GetGlobalAll()
|
||||
public List<CurrencyGlobalDto> GetGlobalAll()
|
||||
{
|
||||
return _currencyGlobalRepository.GetAll();
|
||||
return _mapper.Map<List<CurrencyGlobalDto>>(_currencyGlobalRepository.GetAll());
|
||||
}
|
||||
|
||||
public List<Currency> GetAll(Guid userId)
|
||||
public List<CurrencyDto> GetAll(Guid userId)
|
||||
{
|
||||
return _currencyRepository.GetAll(userId);
|
||||
return _mapper.Map<List<CurrencyDto>>(_currencyRepository.GetAll(userId));
|
||||
}
|
||||
|
||||
public Dictionary<Currency, CurrencyRate?> GetAllWithRates(Guid userId)
|
||||
public List<CurrencyWithRateDto> GetAllWithRates(Guid userId)
|
||||
{
|
||||
var list = _currencyRepository.GetAll(userId);
|
||||
return list.ToDictionary(
|
||||
x => x,
|
||||
x => _currencyRateRepository.GetLastRates(x.Id).FirstOrDefault()
|
||||
);
|
||||
|
||||
return list.Select(x => new CurrencyWithRateDto
|
||||
{
|
||||
Currency = _mapper.Map<CurrencyDto>(x),
|
||||
Rate = _mapper.Map<CurrencyRateDto>(_currencyRateRepository.GetLastRates(x.Id).FirstOrDefault()),
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public Exec<Currency, CurrencyAddStatus> CurrencyAdd(Guid userId, Currency currency)
|
||||
public Exec<CurrencyDto, CurrencyAddStatus> CurrencyAdd(Guid userId, CurrencyDto input)
|
||||
{
|
||||
if (currency == null)
|
||||
throw new ArgumentNullException(nameof(currency));
|
||||
if (input == null)
|
||||
throw new ArgumentNullException(nameof(input));
|
||||
|
||||
var result = new Exec<Currency, CurrencyAddStatus>(CurrencyAddStatus.success);
|
||||
var result = new Exec<CurrencyDto, CurrencyAddStatus>(CurrencyAddStatus.success);
|
||||
|
||||
var exists = _currencyRepository.GetByGlobalCurrency(userId, currency.CurrencyGlobalId);
|
||||
var exists = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyGlobalId);
|
||||
if (exists != null)
|
||||
{
|
||||
return result.Set(exists, CurrencyAddStatus.exists);
|
||||
return result.Set(_mapper.Map<CurrencyDto>(exists), CurrencyAddStatus.exists);
|
||||
}
|
||||
|
||||
currency.Id = Guid.NewGuid();
|
||||
currency.UserId = userId;
|
||||
var currency = new Currency
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = userId,
|
||||
CurrencyGlobalId = input.CurrencyGlobalId,
|
||||
Name = input.Name,
|
||||
ShortName = input.ShortName,
|
||||
};
|
||||
|
||||
if (!_currencyRepository.Add(currency))
|
||||
{
|
||||
return result.Set(CurrencyAddStatus.failed);
|
||||
}
|
||||
|
||||
return result.Set(currency);
|
||||
return result.Set(_mapper.Map<CurrencyDto>(currency));
|
||||
}
|
||||
|
||||
public Exec<Currency, CurrencyEditStatus> CurrencyUpdate(
|
||||
public Exec<CurrencyDto, CurrencyEditStatus> CurrencyUpdate(
|
||||
Guid userId,
|
||||
Guid currencyId,
|
||||
CurrencyEdit currency
|
||||
@@ -74,7 +88,7 @@ public class CurrencyService
|
||||
if (currency == null)
|
||||
throw new ArgumentNullException(nameof(currency));
|
||||
|
||||
var result = new Exec<Currency, CurrencyEditStatus>(CurrencyEditStatus.success);
|
||||
var result = new Exec<CurrencyDto, CurrencyEditStatus>(CurrencyEditStatus.success);
|
||||
|
||||
var exists = _currencyRepository.Get(userId, currencyId);
|
||||
if (exists == null)
|
||||
@@ -104,15 +118,15 @@ public class CurrencyService
|
||||
}
|
||||
}
|
||||
|
||||
return result.Set(exists);
|
||||
return result.Set(_mapper.Map<CurrencyDto>(exists));
|
||||
}
|
||||
|
||||
public Exec<CurrencyRate, CurrencyAddRateStatus> CurrencyRateAdd(Guid userId, Guid currencyId, CurrencyRate currencyRate)
|
||||
public Exec<CurrencyRateDto, CurrencyAddRateStatus> CurrencyRateAdd(Guid userId, Guid currencyId, CurrencyRateDto input)
|
||||
{
|
||||
if (currencyRate == null)
|
||||
throw new ArgumentNullException(nameof(currencyRate));
|
||||
if (input == null)
|
||||
throw new ArgumentNullException(nameof(input));
|
||||
|
||||
var result = new Exec<CurrencyRate, CurrencyAddRateStatus>(CurrencyAddRateStatus.success);
|
||||
var result = new Exec<CurrencyRateDto, CurrencyAddRateStatus>(CurrencyAddRateStatus.success);
|
||||
|
||||
var currency = _currencyRepository.Get(userId, currencyId);
|
||||
if (currency == null)
|
||||
@@ -120,11 +134,17 @@ public class CurrencyService
|
||||
return result.Set(CurrencyAddRateStatus.not_found);
|
||||
}
|
||||
|
||||
currencyRate.CurrencyId = currency.Id;
|
||||
currencyRate.DateTime = currencyRate.DateTime.Date;
|
||||
var currencyRate = new CurrencyRate
|
||||
{
|
||||
CurrencyId = currency.Id,
|
||||
DateTime = input.DateTime.Date,
|
||||
Rate = input.Rate,
|
||||
Quantity = input.Quantity,
|
||||
};
|
||||
|
||||
var rates = _currencyRateRepository.GetAtDate(currencyRate.CurrencyId, currencyRate.DateTime);
|
||||
var rate = rates.Find(x => x.Rate == currencyRate.Rate);
|
||||
|
||||
if (rate == null)
|
||||
{
|
||||
if (!_currencyRateRepository.AddRate(currencyRate))
|
||||
@@ -132,15 +152,15 @@ public class CurrencyService
|
||||
return result.Set(CurrencyAddRateStatus.failed);
|
||||
}
|
||||
|
||||
rate = currencyRate;
|
||||
|
||||
if (rate.DateTime.EndOfDay() <= DateTime.UtcNow.EndOfDay())
|
||||
// TODO: more logic to fix current rate
|
||||
if (currencyRate.DateTime.Date == DateTime.UtcNow.Date || !currency.CurrentRateId.HasValue)
|
||||
{
|
||||
currency.CurrentRateId = rate.Id;
|
||||
currency.CurrencyGlobal = null;
|
||||
currency.CurrentRateId = currencyRate.Id;
|
||||
_currencyRepository.Update(currency);
|
||||
}
|
||||
}
|
||||
|
||||
return result.Set(rate);
|
||||
return result.Set(_mapper.Map<CurrencyRateDto>(rate));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user