0.1.0 #2

Merged
mfnefd merged 38 commits from dev into main 2024-12-09 04:27:05 +04:00
3 changed files with 28 additions and 1 deletions
Showing only changes of commit cbc45bbecb - Show all commits

View File

@ -8,5 +8,6 @@ public interface IUserRepo
public Task<UserDto?> Get(UserSearch search);
public Task<UserDto> Create(UserDto user);
public Task<UserDto?> Update(UserDto user);
public Task<bool> ChangeBalance(UserSearch search, decimal amount);
public Task<UserDto?> Delete(UserSearch search);
}

View File

@ -15,6 +15,25 @@ public class UserRepo : IUserRepo
_factory = factory;
}
public async Task<bool> ChangeBalance(UserSearch search, decimal amount)
{
using var context = _factory.CreateDbContext();
var user = await context.Users
.FirstOrDefaultAsync(x => x.Id == search.Id
|| x.Name == search.Name);
if (user == null)
{
return false;
}
user.Balance += amount;
context.Users.Update(user);
await context.SaveChangesAsync();
return true;
}
public async Task<UserDto> Create(UserDto user)
{
using var context = _factory.CreateDbContext();

View File

@ -10,16 +10,20 @@ namespace Services.Domain;
public class ChangeRecordService : IChangeRecordService
{
private readonly IChangeRecordRepo _changeRecordRepo;
private readonly IUserRepo _userRepo;
public ChangeRecordService(IChangeRecordRepo changeRecordRepo)
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();
}
@ -30,6 +34,8 @@ public class ChangeRecordService : IChangeRecordService
{
throw new EntryPointNotFoundException("При удалении не получилось найти запись измнения баланса");
}
// Возвращает баланс обратно
await _userRepo.ChangeBalance(new() { Id = record.UserId }, -record.Sum);
return record.ToView();
}
@ -47,6 +53,7 @@ public class ChangeRecordService : IChangeRecordService
{
throw new EntryPointNotFoundException("При изменении не получилось найти запись измнения баланса");
}
await _userRepo.ChangeBalance(new() { Id = model.UserId }, model.Sum - record.Sum);
return record.ToView();
}
}