This commit is contained in:
2023-07-31 08:50:05 +03:00
parent cca943f37d
commit c5d9ae7b93
49 changed files with 546 additions and 398 deletions
+30 -30
View File
@@ -1,20 +1,19 @@
namespace MyOffice.Services.Account
{
using AutoMapper;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Identity;
using Core;
using Core.Extensions;
using Core.Helpers;
using Data.Models.Accounts;
using Data.Models.Items;
using Data.Repositories.Account;
using Data.Repositories.Currency;
using Data.Repositories.Item;
using Domain;
using Item;
using Item.Domain;
using Microsoft.Extensions.Logging;
using MyOffice.Services.Account.Domain;
using System.Collections.Generic;
using Identity;
public class AccountService
{
@@ -84,15 +83,19 @@
return result.Set(_mapper.Map<AccountCategoryDto>(category));
}
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryAdd(Guid userId, AccountCategory category)
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryAdd(Guid userId, AccountCategoryDto input)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
category.Id = Guid.NewGuid();
category.UserId = userId;
var category = new AccountCategory
{
Id = Guid.NewGuid(),
UserId = userId,
Name = input.Name,
};
if (!_accountCategoryRepository.Add(category))
{
@@ -102,10 +105,10 @@
return result.Set(_mapper.Map<AccountCategoryDto>(category));
}
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, AccountCategory category)
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, AccountCategoryDto input)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
@@ -115,7 +118,7 @@
return result.Set(GeneralExecStatus.not_found);
}
exists.Name = category.Name;
exists.Name = input.Name;
if (!_accountCategoryRepository.Update(exists))
{
@@ -300,14 +303,11 @@
if (category != null && account.Categories!.All(x => x.CategoryId != category.Id))
{
account.Categories = new List<AccountAccountCategory>
_accountAccountCategoryRepository.Add(new AccountAccountCategory
{
new()
{
AccountId = account.Id,
CategoryId = category.Id,
}
};
AccountId = account.Id,
CategoryId = category.Id,
});
}
if (!_accountRepository.Update(account))
@@ -346,7 +346,7 @@
return result.Set(categories);
}
public Exec<List<Motion>, MotionAddStatus> MotionAdd(
public Exec<List<MotionDto>, MotionAddStatus> MotionAdd(
Guid userId,
Guid accountId,
MotionAddUpdate motion
@@ -357,7 +357,7 @@
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<List<Motion>, MotionAddStatus>(MotionAddStatus.success);
var result = new Exec<List<MotionDto>, MotionAddStatus>(MotionAddStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
@@ -388,8 +388,8 @@
return result.Set(MotionAddStatus.failure);
}
result.Set(new List<Motion>());
result.Result!.Add(motionDb);
result.Set(new List<MotionDto>());
result.Result!.Add(_mapper.Map<MotionDto>(motionDb));
if (motion.AccountId.IsPresent() && motion.AmountBalancing != 0)
{
@@ -419,7 +419,7 @@
if (_motionRepository.Add(motionBalancing))
{
result.Result!.Add(motionBalancing);
result.Result!.Add(_mapper.Map<MotionDto>(motionBalancing));
}
}
}
@@ -429,7 +429,7 @@
return result;
}
public Exec<Motion, MotionUpdateStatus> MotionUpdate(
public Exec<MotionDto, MotionUpdateStatus> MotionUpdate(
Guid userId,
Guid accountId,
Guid motionId,
@@ -441,7 +441,7 @@
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<Motion, MotionUpdateStatus>(MotionUpdateStatus.success);
var result = new Exec<MotionDto, MotionUpdateStatus>(MotionUpdateStatus.success);
var exists = _motionRepository.Get(userId, motionId);
if (exists == null || exists.AccountId != accountId)
@@ -467,7 +467,7 @@
}
return result.Set(exists);
return result.Set(_mapper.Map<MotionDto>(exists));
}
public Exec<List<Motion>, GeneralExecStatus> GetMotions(
@@ -0,0 +1,14 @@
namespace MyOffice.Services.Account.Domain;
using MyOffice.Data.Models.Users;
public class ItemCategoryDto
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public User User { get; set; } = null!;
public string Name { get; set; } = null!;
public List<ItemDto> Items { get; set; } = null!;
public bool IsInternal { get; set; }
}
@@ -0,0 +1,13 @@
namespace MyOffice.Services.Account.Domain;
using MyOffice.Data.Models.Items;
public class ItemDto
{
public Guid Id { get; set; }
public string? Name { get; set; }
public bool AllowDelete { get; set; }
public Guid CategoryId { get; set; }
public ItemCategoryDto? Category { get; set; }
}
@@ -0,0 +1,16 @@
namespace MyOffice.Services.Account.Domain;
public class MotionDto
{
public Guid Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime DateTime { get; set; }
public Guid AccountId { get; set; }
public AccountDto Account { get; set; } = null!;
public int ItemId { get; set; }
public ItemDto Item { get; set; } = null!;
public string? Description { get; set; }
public decimal AmountPlus { get; set; }
public decimal AmountMinus { get; set; }
public DateTime? DeletedOn { get; set; }
}
+54 -34
View File
@@ -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));
}
}
@@ -0,0 +1,22 @@
namespace MyOffice.Services.Currency.Domain;
using MyOffice.Data.Models.Currencies;
using MyOffice.Data.Models.Users;
public class CurrencyDto
{
public Guid Id { get; set; }
public string CurrencyGlobalId { get; set; } = null!;
public CurrencyGlobal? CurrencyGlobal { get; set; }
public Guid UserId { get; set; }
public User? User { get; set; }
public string Name { get; set; } = null!;
public string ShortName { get; set; } = null!;
public IEnumerable<CurrencyRate>? Rates { get; set; }
public int? CurrentRateId { get; set; }
public CurrencyRate? CurrentRate { get; set; }
public bool IsPrimary { get; set; }
}
@@ -0,0 +1,9 @@
namespace MyOffice.Services.Currency.Domain;
public class CurrencyGlobalDto
{
public string Id { get; set; } = null!;
public string Name { get; set; } = null!;
public string Symbol { get; set; } = null!;
public int DefaultQuantity { get; set; }
}
@@ -0,0 +1,12 @@
namespace MyOffice.Services.Currency.Domain;
public class CurrencyRateDto
{
public int Id { get; set; }
public Guid CurrencyId { get; set; }
public CurrencyDto? Currency { get; set; }
public DateTime DateTime { get; set; }
public int Quantity { get; set; }
public decimal Rate { get; set; }
public IEnumerable<CurrencyDto>? Currencies { get; set; }
}
@@ -0,0 +1,7 @@
namespace MyOffice.Services.Currency.Domain;
public class CurrencyWithRateDto
{
public CurrencyDto Currency { get; set; } = null!;
public CurrencyRateDto? Rate { get; set; }
}
+38 -32
View File
@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using AutoMapper;
using Domain;
using MyOffice.Core;
using MyOffice.Data.Models.Accounts;
@@ -16,20 +17,23 @@ public class ItemService
private readonly IItemCategoryRepository _itemCategoryRepository;
private readonly IItemRepository _itemRepository;
private readonly IItemGlobalRepository _itemGlobalRepository;
private readonly IMapper _mapper;
public ItemService(
IItemCategoryRepository itemCategoryRepository,
IItemRepository itemRepository,
IItemGlobalRepository itemGlobalRepository
IItemGlobalRepository itemGlobalRepository,
IMapper mapper
)
{
_itemCategoryRepository = itemCategoryRepository;
_itemRepository = itemRepository;
_itemGlobalRepository = itemGlobalRepository;
_mapper = mapper;
}
public List<ItemCategory> GetAllCategories(Guid userId)
public List<ItemCategoryDto> GetAllCategories(Guid userId)
{
var result = _itemCategoryRepository.GetAll(userId);
if (result.All(x => x.Id != userId))
@@ -44,35 +48,38 @@ public class ItemService
result.Add(_itemCategoryRepository.Get(userId, userId)!);
}
return result;
return _mapper.Map<List<ItemCategoryDto>>(result);
}
public Exec<ItemCategory, GeneralExecStatus> CategoryAdd(Guid userId, ItemCategory category)
public Exec<ItemCategoryDto, GeneralExecStatus> CategoryAdd(Guid userId, ItemCategoryDto input)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<ItemCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
category.Id = Guid.NewGuid();
category.UserId = userId;
category.IsInternal = category.IsInternal;
category.Items = new List<Item>();
var category = new ItemCategory
{
Id = Guid.NewGuid(),
UserId = userId,
Name = input.Name,
IsInternal = input.IsInternal,
};
if (!_itemCategoryRepository.Add(category))
{
return result.Set(GeneralExecStatus.failure);
}
return result.Set(category);
return result.Set(_mapper.Map<ItemCategoryDto>(category));
}
public Exec<ItemCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, ItemCategory category)
public Exec<ItemCategoryDto, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, ItemCategoryDto category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<ItemCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
var exists = _itemCategoryRepository.Get(userId, id);
if (exists == null)
@@ -82,19 +89,18 @@ public class ItemService
exists.Name = category.Name;
exists.IsInternal = category.IsInternal;
exists.Items = new List<Item>();
if (!_itemCategoryRepository.Update(exists))
{
return result.Set(GeneralExecStatus.failure);
}
return result.Set(exists);
return result.Set(_mapper.Map<ItemCategoryDto>(exists));
}
public Exec<ItemCategory, ItemCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
public Exec<ItemCategoryDto, ItemCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
{
var result = new Exec<ItemCategory, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
var result = new Exec<ItemCategoryDto, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
var exists = _itemCategoryRepository.Get(userId, id);
if (exists == null)
@@ -111,33 +117,33 @@ public class ItemService
return result.Set(ItemCategoryRemoveResult.failure);
}
return result.Set(exists);
return result.Set(_mapper.Map<ItemCategoryDto>(exists));
}
public List<Item> GetAll(Guid userId)
public List<ItemDto> GetAll(Guid userId)
{
return _itemRepository.GetAll(userId);
return _mapper.Map<List<ItemDto>>(_itemRepository.GetAll(userId));
}
public List<Item> GetByCategory(Guid userId, Guid categoryId)
public List<ItemDto> GetByCategory(Guid userId, Guid categoryId)
{
return _itemRepository.GetByCategory(userId, categoryId);
return _mapper.Map<List<ItemDto>>(_itemRepository.GetByCategory(userId, categoryId));
}
public Exec<Item, GeneralExecStatus> Update(Guid userId, Guid motionId, Guid categoryId)
public Exec<ItemDto, GeneralExecStatus> Update(Guid userId, Guid motionId, Guid categoryId)
{
var result = new Exec<Item, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<ItemDto, GeneralExecStatus>(GeneralExecStatus.success);
var motion = _itemRepository.GetByGlobal(userId, motionId);
if (motion == null)
var item = _itemRepository.GetByGlobal(userId, motionId);
if (item == null)
{
return result.Set(GeneralExecStatus.not_found);
}
motion.Category!.Id = categoryId;
_itemRepository.Update(motion);
item.CategoryId = categoryId;
_itemRepository.Update(item);
return result.Set(motion);
return result.Set(_mapper.Map<ItemDto>(item));
}
public Exec<Item, ItemGetOrAddResult> GetOrCreate(Guid userId, string name)
@@ -191,11 +197,11 @@ public class ItemService
return result.Set(item);
}
public List<Item> FindItems(Guid userId, string term, int limit = 15)
public List<ItemDto> FindItems(Guid userId, string term, int limit = 15)
{
if (term == null)
throw new ArgumentNullException(nameof(term));
return _itemRepository.Find(userId, term, limit);
return _mapper.Map<List<ItemDto>>(_itemRepository.Find(userId, term, limit));
}
}
@@ -3,14 +3,33 @@
using Account.Domain;
using AutoMapper;
using Data.Models.Accounts;
using Data.Models.Currencies;
using Data.Models.Items;
using Data.Models.Users;
using MyOffice.Services.Currency.Domain;
public class AccountServiceProfile : Profile
{
public AccountServiceProfile()
{
CreateMap<User, UserDto>();
MapUser();
MapAccount();
MapCurrency();
MapItems();
CreateMap<Motion, MotionDto>();
}
private void MapUser()
{
CreateMap<User, UserDto>();
}
private void MapAccount()
{
CreateMap<Account, AccountDto>()
.ForMember(x => x.HasMotions, o => o.MapFrom(x => x.Motions!.Any()))
.ForMember(x => x.CurrentUserId, o => o.MapFrom<UserIdResolver>())
@@ -45,4 +64,24 @@ public class AccountServiceProfile : Profile
.ForMember(x => x.Account, o => o.MapFrom(x => x.Account!.Name))
;
}
private void MapCurrency()
{
CreateMap<CurrencyGlobal, CurrencyGlobalDto>();
CreateMap<Currency, CurrencyDto>();
CreateMap<CurrencyRate, CurrencyRateDto>();
}
private void MapItems()
{
CreateMap<ItemCategory, ItemCategoryDto>();
CreateMap<Item, ItemDto>()
.ForMember(x => x.Id, o => o.MapFrom(x => x.ItemGlobalId))
.ForMember(x => x.Name, o => o.MapFrom(x => x.ItemGlobal.Name))
.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.Motions!.Any()))
;
}
}