60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using Contracts.DTO;
|
|
using Contracts.Mappers;
|
|
using Contracts.Repositories;
|
|
using Contracts.SearchModels;
|
|
using Contracts.Services;
|
|
using Contracts.ViewModels;
|
|
|
|
namespace Services.Domain;
|
|
|
|
public class ChangeRecordService : IChangeRecordService
|
|
{
|
|
private readonly IChangeRecordRepo _changeRecordRepo;
|
|
private readonly IUserRepo _userRepo;
|
|
|
|
public ChangeRecordService(IChangeRecordRepo changeRecordRepo, IUserRepo userRepo)
|
|
{
|
|
_changeRecordRepo = changeRecordRepo;
|
|
_userRepo = userRepo;
|
|
}
|
|
|
|
public async Task<ChangeRecordViewModel> Create(ChangeRecordDto model)
|
|
{
|
|
var record = await _changeRecordRepo.Create(model);
|
|
|
|
await _userRepo.ChangeBalance(new() { Id = model.UserId }, model.Sum);
|
|
|
|
return record.ToView();
|
|
}
|
|
|
|
public async Task<ChangeRecordViewModel> Delete(ChangeRecordSearch search)
|
|
{
|
|
var record = await _changeRecordRepo.Delete(search);
|
|
if (record == null)
|
|
{
|
|
throw new EntryPointNotFoundException("При удалении не получилось найти запись измнения баланса");
|
|
}
|
|
// Возвращает баланс обратно
|
|
await _userRepo.ChangeBalance(new() { Id = record.UserId }, -record.Sum);
|
|
return record.ToView();
|
|
}
|
|
|
|
public async Task<IEnumerable<ChangeRecordViewModel>> GetList(ChangeRecordSearch? search = null)
|
|
{
|
|
var records = await _changeRecordRepo.GetList(search);
|
|
|
|
return records.Select(x => x.ToView()).ToList();
|
|
}
|
|
|
|
public async Task<ChangeRecordViewModel> Update(ChangeRecordDto model)
|
|
{
|
|
var record = await _changeRecordRepo.Update(model);
|
|
if (record == null)
|
|
{
|
|
throw new EntryPointNotFoundException("При изменении не получилось найти запись измнения баланса");
|
|
}
|
|
await _userRepo.ChangeBalance(new() { Id = model.UserId }, model.Sum - record.Sum);
|
|
return record.ToView();
|
|
}
|
|
}
|