Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move merged services to Provider #203

Merged
merged 28 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
14ee8fd
Adds Commit to BaseRepository
Chingling152 Jan 4, 2024
335e66f
Adds CommitAsync call to CategoriesProvider
Chingling152 Jan 4, 2024
9d0e913
Add CommitAsync
Chingling152 Jan 6, 2024
fae41fd
Adds integration test to delete category
Chingling152 Jan 6, 2024
73fbddb
Fix delete accounts
Chingling152 Jan 7, 2024
879f046
Moves Default Balance Creation inside AccountProvider
Chingling152 Jan 8, 2024
d5451bb
Fix Account Create Tests
Chingling152 Jan 9, 2024
d26629d
Fix infra data unit tests
Chingling152 Jan 11, 2024
c68df50
Removes IAccountBalanceService from AccountsController
Chingling152 Jan 16, 2024
405254c
Removes AccountBalanceService
Chingling152 Jan 16, 2024
049e6f6
Fixes BalanceProvider tests
Chingling152 Jan 18, 2024
b483f23
Adds BalanceIntegration tests fo deletion
Chingling152 Jan 19, 2024
8c650a2
Fixes AccountProvider unit tests
Chingling152 Jan 19, 2024
72242e8
Changes app.settngs database to financial_hub_test
Chingling152 Jan 25, 2024
5cbf34d
Moves transaction delete to TransactionsProvider
Chingling152 Jan 25, 2024
e953acf
Improves TransactionProvider.delete tests
Chingling152 Jan 25, 2024
fdbf84d
Removes TransactionBalanceService.DeleteTransaction
Chingling152 Jan 25, 2024
f450984
Removes Mapper From Application layer
Chingling152 Jan 25, 2024
83e3981
Small Fixes
Chingling152 Jan 25, 2024
42fb109
Adds Integration Tests for Delete Transaction
Chingling152 Jan 25, 2024
86e1e12
Moves CreateAsync from TransactionBalancesService to TransactionsProv…
Chingling152 Jan 25, 2024
9f7c457
Removes TransactionBalance Logic from Application logic
Chingling152 Jan 25, 2024
8e7c3c7
Fix transactionrepository create unit tests
Chingling152 Jan 25, 2024
fab5f06
Fixes Transactions unit tests
Chingling152 Jan 25, 2024
92f6bf7
Removes ITransactionBalanceService from TransactionsController
Chingling152 Jan 25, 2024
8c5b274
TransactionsProviderCreateAsyncTests
Chingling152 Jan 25, 2024
a7d26ae
Fix code smells
Chingling152 Jan 25, 2024
0a5d8f1
Disables Update Transaction endpoint
Chingling152 Jan 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace FinancialHub.Common.Interfaces.Repositories
/// Base repository with basic CRUD methods
/// </summary>
/// <typeparam name="T">Any Entity that inherits <see cref="BaseEntity"/> </typeparam>
public interface IBaseRepository<T>
where T : BaseEntity
public interface IBaseRepository<T> where T : BaseEntity
{
/// <summary>
/// Adds an entity to the database
Expand Down Expand Up @@ -37,5 +36,10 @@ public interface IBaseRepository<T>
/// </summary>
/// <param name="id">Id of the choosen entity</param>
Task<T?> GetByIdAsync(Guid id);
/// <summary>
/// Commit All changes to the database
/// </summary>
/// <returns>Number of affected entities</returns>
Task<int> CommitAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using FinancialHub.Core.Application.Mappers;
using FinancialHub.Core.Application.Services;
using FinancialHub.Core.Application.Validators;
using FluentValidation;
Expand All @@ -10,7 +9,6 @@ public static class IServiceCollectionExtensions
{
public static IServiceCollection AddCoreServices(this IServiceCollection services)
{
services.AddMapper();
services.AddServices();
services.AddValidators();

Expand All @@ -24,16 +22,6 @@ private static IServiceCollection AddServices(this IServiceCollection services)
services.AddScoped<ITransactionsService, TransactionsService>();
services.AddScoped<IBalancesService, BalancesService>();

services.AddScoped<IAccountBalanceService, AccountBalanceService>();
services.AddScoped<ITransactionBalanceService, TransactionBalanceService>();

return services;
}

private static IServiceCollection AddMapper(this IServiceCollection services)
{
services.AddScoped<IMapperWrapper, FinancialHubMapperWrapper>();

return services;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace FinancialHub.Core.Application.Services
public class BalancesService : IBalancesService
{
private readonly IAccountsProvider accountsProvider;
private readonly IErrorMessageProvider errorMessageProvider;
private readonly IBalancesProvider balancesProvider;
private readonly IErrorMessageProvider errorMessageProvider;

public BalancesService(
IBalancesProvider balancesProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FinancialHub.Core.Application.Services
{
[Obsolete("This Service will be removed and the logic will be moved to Balance and Transaction models")]
public class TransactionBalanceService : ITransactionBalanceService
{
private readonly ITransactionsService transactionsService;
Expand Down Expand Up @@ -120,34 +121,6 @@ public async Task UpdateAmountAsync(TransactionModel oldTransaction, Transaction
}
}

public async Task<ServiceResult<TransactionModel>> CreateTransactionAsync(TransactionModel transaction)
{
var transactionResult = await transactionsService.CreateAsync(transaction);

if(transactionResult.HasError) {
return transactionResult;
}

if(transactionResult.Data!.IsPaid)
{
var balanceResult = await balancesService.GetByIdAsync(transaction.BalanceId);
if(balanceResult.HasError) {
return balanceResult.Error;
}

if (transaction.Type == TransactionType.Earn)
{
await balancesService.UpdateAmountAsync(transaction.BalanceId, balanceResult.Data!.Amount + transaction.Amount);
}
else
{
await balancesService.UpdateAmountAsync(transaction.BalanceId, balanceResult.Data!.Amount - transaction.Amount);
}
}

return transactionResult;
}

public async Task<ServiceResult<TransactionModel>> UpdateTransactionAsync(Guid id, TransactionModel transaction)
{
var oldTransactionResult = await transactionsService.GetByIdAsync(id);
Expand Down Expand Up @@ -178,17 +151,5 @@ public async Task<ServiceResult<TransactionModel>> UpdateTransactionAsync(Guid i

return transactionResult;
}

public async Task<ServiceResult<bool>> DeleteTransactionAsync(Guid id)
{
var deleted = await this.transactionsService.DeleteAsync(id);

if (deleted.HasError)
return deleted.Error;
if (deleted.Data == 0)
return false;

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FinancialHub.Core.Domain.Filters;
using FinancialHub.Core.Domain.Enums;
using FinancialHub.Core.Domain.Interfaces.Resources;

namespace FinancialHub.Core.Application.Services
Expand Down Expand Up @@ -64,13 +63,6 @@ public async Task<ServiceResult<int>> DeleteAsync(Guid id)
return transactionResult.Error;
}

var transaction = transactionResult.Data!;

if (transaction.Status == TransactionStatus.Committed && transaction.IsActive)
{
await this.balancesProvider.DecreaseAmountAsync(transaction.BalanceId, transaction.Amount, transaction.Type);
}

return await this.transactionsProvider.DeleteAsync(id);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ namespace FinancialHub.Core.Domain.Interfaces.Services
{
public interface ITransactionBalanceService
{
Task<ServiceResult<TransactionModel>> CreateTransactionAsync(TransactionModel transaction);
Task<ServiceResult<TransactionModel>> UpdateTransactionAsync(Guid id,TransactionModel transaction);
Task UpdateAmountAsync(TransactionModel oldTransaction, TransactionModel newTransaction);
Task<ServiceResult<bool>> DeleteTransactionAsync(Guid id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

namespace FinancialHub.Core.Infra.Data.Repositories
{
public class BaseRepository<T> : IBaseRepository<T>
where T : BaseEntity
public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
{
protected readonly FinancialHubContext context;
public BaseRepository(FinancialHubContext context)
Expand All @@ -21,7 +20,6 @@ public virtual async Task<T> CreateAsync(T obj)
obj.UpdateTime = DateTimeOffset.Now;

var res = await context.Set<T>().AddAsync(obj);
await context.SaveChangesAsync();
return res.Entity;
}

Expand All @@ -32,7 +30,7 @@ public virtual async Task<int> DeleteAsync(Guid id)
if(entity != null)
{
context.Set<T>().Remove(entity);
return await context.SaveChangesAsync();
return 1;
}
else
{
Expand Down Expand Up @@ -68,5 +66,10 @@ public virtual async Task<ICollection<T>> GetAsync(Func<T, bool> predicate)
{
return await context.Set<T>().AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
}

public async Task<int> CommitAsync()
{
return await context.SaveChangesAsync();
}
}
}
21 changes: 18 additions & 3 deletions src/api/core/FinancialHub.Core.Infra/Providers/AccountsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@ public class AccountsProvider : IAccountsProvider
{
private readonly IMapper mapper;
private readonly IAccountsRepository repository;
private readonly IBalancesRepository balanceRepository;

public AccountsProvider(IMapper mapper, IAccountsRepository repository)
public AccountsProvider(IMapper mapper, IAccountsRepository repository, IBalancesRepository balanceRepository)
{
this.mapper = mapper;
this.repository = repository;
this.balanceRepository = balanceRepository;
}

public async Task<AccountModel> CreateAsync(AccountModel account)
{
var accountEntity = mapper.Map<AccountEntity>(account);

var createdAccount = await this.repository.CreateAsync(accountEntity);

var balance = new BalanceModel()
{
Name = $"{createdAccount!.Name} Default Balance",
AccountId = createdAccount.Id.GetValueOrDefault(),
IsActive = createdAccount.IsActive
};
var balanceEntity = mapper.Map<BalanceEntity>(balance);
await this.balanceRepository.CreateAsync(balanceEntity);

await this.repository.CommitAsync();

return mapper.Map<AccountModel>(createdAccount);
}

public async Task<int> DeleteAsync(Guid id)
{
return await repository.DeleteAsync(id);
await repository.DeleteAsync(id);
return await this.repository.CommitAsync();
}

public async Task<ICollection<AccountModel>> GetAllAsync()
Expand All @@ -48,6 +61,8 @@ public async Task<AccountModel> UpdateAsync(Guid id, AccountModel account)
accountEntity.Id = id;

var updatedAccount = await this.repository.UpdateAsync(accountEntity);
await this.repository.CommitAsync();

return mapper.Map<AccountModel>(updatedAccount);
}
}
Expand Down
Loading
Loading