diff --git a/Application/Application.csproj b/Application/Application.csproj
index 628e76e..7deab53 100644
--- a/Application/Application.csproj
+++ b/Application/Application.csproj
@@ -20,7 +20,6 @@
-
diff --git a/Application/Services/BankSafes/Commands/UpdateBankSafe/UpdateBankSafeCommandValidator.cs b/Application/Services/BankSafes/Commands/UpdateBankSafe/UpdateBankSafeCommandValidator.cs
index 253185f..aa9b81d 100644
--- a/Application/Services/BankSafes/Commands/UpdateBankSafe/UpdateBankSafeCommandValidator.cs
+++ b/Application/Services/BankSafes/Commands/UpdateBankSafe/UpdateBankSafeCommandValidator.cs
@@ -1,7 +1,6 @@
using Application.Services.BankAccounts.Commands.AddBankAccount;
using Domain.Exceptions;
using FluentValidation;
-using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -19,6 +18,7 @@ public UpdateBankSafeCommandValidator()
RuleFor(p => p.SharePrice)
.NotNull().WithMessage(string.Format(ConstMessages.IsNull, "SharePrice"));
+
}
}
}
diff --git a/Application/Services/Loan/Commands/AddLoan/AddLoanCommand.cs b/Application/Services/Loan/Commands/AddLoan/AddLoanCommand.cs
index f1a3bd5..fd779fe 100644
--- a/Application/Services/Loan/Commands/AddLoan/AddLoanCommand.cs
+++ b/Application/Services/Loan/Commands/AddLoan/AddLoanCommand.cs
@@ -1,12 +1,23 @@
-using System;
+using Domain.Exceptions;
+using Domain.ValueObjects;
+using MediatR;
+using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
namespace Application.Services.Loan.Commands.AddLoan
{
- internal class AddLoanCommand
+ public record AddLoanCommand : IRequest>
{
+ public required string NameBankSafe { get; init; }
+ public required string FirstName { get; init; }
+ public required string LastName { get; init; }
+ public required int NumberOfInstallments { get; init; }
+ public required decimal Amount { get; init; }
+ public required int Wage { get; init; }
}
+
}
diff --git a/Application/Services/Loan/Commands/AddLoan/AddLoanCommandHandler.cs b/Application/Services/Loan/Commands/AddLoan/AddLoanCommandHandler.cs
index ca47cfe..126d1ca 100644
--- a/Application/Services/Loan/Commands/AddLoan/AddLoanCommandHandler.cs
+++ b/Application/Services/Loan/Commands/AddLoan/AddLoanCommandHandler.cs
@@ -1,4 +1,11 @@
-using System;
+using Application.Services.BankSafes.Commands.AddBankSafe;
+using Application.UnitOfWork;
+using Domain.Entity;
+using Domain.Exceptions;
+using Domain.IRepositories;
+using MediatR;
+using Microsoft.Extensions.Logging;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +13,44 @@
namespace Application.Services.Loan.Commands.AddLoan
{
- internal class AddLoanCommandHandler
+ public class AddLoanCommandHandler
+ : IRequestHandler>
{
+ private readonly ILoanRepositorie _loanRepositorie;
+ private readonly IUnitOfWork _unitOfWork;
+ private readonly ILogger _logger;
+ public AddLoanCommandHandler(ILoanRepositorie loanRepositorie,
+ IUnitOfWork unitOfWork
+ , ILogger logger)
+ {
+ _loanRepositorie = loanRepositorie;
+ _unitOfWork = unitOfWork;
+ _logger = logger;
+ }
+ public async Task> Handle(AddLoanCommand request, CancellationToken cancellationToken)
+ {
+ try
+ {
+ var loan = new Domain.Entity.Loan(
+ request.NameBankSafe,
+ request.FirstName,
+ request.LastName,
+ request.NumberOfInstallments,
+ request.Amount,
+ request.Wage);
+ await _loanRepositorie.AddAsync(loan, cancellationToken);
+ await _unitOfWork.SaveChangesAsync(cancellationToken);
+ string message = string.Format(ConstMessages.Successfully
+ , loan.FirstName.Value + " " + loan.LastName.Value
+ , nameof(AddLoanCommandHandler));
+ _logger.LogInformation(message);
+ return new OperationResult(true, null , loan.Code);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, ex.Message);
+ return new OperationResult(false, ex.Message , Guid.Empty);
+ }
+ }
}
}
diff --git a/Application/Services/Loan/Commands/AddLoan/AddLoanCommandValidator.cs b/Application/Services/Loan/Commands/AddLoan/AddLoanCommandValidator.cs
new file mode 100644
index 0000000..f8ca2bf
--- /dev/null
+++ b/Application/Services/Loan/Commands/AddLoan/AddLoanCommandValidator.cs
@@ -0,0 +1,36 @@
+using Domain.Exceptions;
+using FluentValidation;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Commands.AddLoan
+{
+ public class AddLoanCommandValidator:AbstractValidator
+ {
+ public AddLoanCommandValidator()
+ {
+ RuleFor(p => p.NameBankSafe)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "NameBankSafe"))
+ .MaximumLength(50).WithMessage(string.Format(ConstMessages.MaximumLength, "NameBankSafe", "50"));
+
+ RuleFor(p => p.FirstName)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "FirstName"))
+ .MaximumLength(50).WithMessage(string.Format(ConstMessages.MaximumLength, "FirstName", "50"));
+
+ RuleFor(p => p.LastName)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "LastName"))
+ .MaximumLength(50).WithMessage(string.Format(ConstMessages.MaximumLength, "LastName", "50"));
+
+ RuleFor(p => p.NumberOfInstallments)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "NumberOfInstallments"));
+
+ RuleFor(p => p.Amount)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "Amount"));
+
+ RuleFor(p => p.Wage)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "Wage"));
+
+ }
+ }
+}
diff --git a/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommand.cs b/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommand.cs
new file mode 100644
index 0000000..84c39d0
--- /dev/null
+++ b/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommand.cs
@@ -0,0 +1,15 @@
+using Domain.Exceptions;
+using MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Commands.DeleteLoan
+{
+ public record DeleteLoanCommand : IRequest
+ {
+ public required Guid Code { get; init; }
+ }
+}
diff --git a/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandHandler.cs b/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandHandler.cs
new file mode 100644
index 0000000..14df7a7
--- /dev/null
+++ b/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandHandler.cs
@@ -0,0 +1,48 @@
+using Application.Services.BankSafes.Commands.DeleteBankSafe;
+using Application.UnitOfWork;
+using Domain.Exceptions;
+using Domain.IRepositories;
+using MediatR;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Commands.DeleteLoan
+{
+ public class DeleteLoanCommandHandler :
+ IRequestHandler
+ {
+ private readonly ILoanRepositorie _loanRepositorie;
+ private readonly IUnitOfWork _unitOfWork;
+ private readonly ILogger _logger;
+ public DeleteLoanCommandHandler(ILoanRepositorie loanRepositorie,
+ IUnitOfWork unitOfWork
+ , ILogger logger)
+ {
+ _loanRepositorie = loanRepositorie;
+ _unitOfWork = unitOfWork;
+ _logger = logger;
+ }
+ public async Task Handle(DeleteLoanCommand request, CancellationToken cancellationToken)
+ {
+ try
+ {
+ await _loanRepositorie.DeleteAsync(request.Code, cancellationToken);
+ await _unitOfWork.SaveChangesAsync(cancellationToken);
+ string message = string.Format(ConstMessages.Successfully
+ , request.Code
+ , nameof(DeleteLoanCommandHandler));
+ _logger.LogInformation(message);
+ return new OperationResult(true, null);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, ex.Message);
+ return new OperationResult(false, ex.Message);
+ }
+ }
+ }
+}
diff --git a/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandValidator.cs b/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandValidator.cs
new file mode 100644
index 0000000..3316632
--- /dev/null
+++ b/Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandValidator.cs
@@ -0,0 +1,19 @@
+using Domain.Exceptions;
+using FluentValidation;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Commands.DeleteLoan
+{
+ public class DeleteLoanCommandValidator : AbstractValidator
+ {
+ public DeleteLoanCommandValidator()
+ {
+ RuleFor(p => p.Code)
+ .NotNull().WithMessage(string.Format(ConstMessages.IsNull, "Code"));
+ }
+ }
+}
diff --git a/Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQuery.cs b/Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQuery.cs
new file mode 100644
index 0000000..4b37aaa
--- /dev/null
+++ b/Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQuery.cs
@@ -0,0 +1,16 @@
+using Domain.Exceptions;
+using MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Queries.GetAllLoan
+{
+ public record GetAllLoanQuery
+ : IRequest>>
+ {
+
+ }
+}
diff --git a/Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQueryHandler.cs b/Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQueryHandler.cs
new file mode 100644
index 0000000..6b2393a
--- /dev/null
+++ b/Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQueryHandler.cs
@@ -0,0 +1,45 @@
+using Application.Services.BankSafes.Queries.GetAllBankSafe;
+using Domain.Entity;
+using Domain.Exceptions;
+using Domain.IRepositories;
+using MediatR;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Queries.GetAllLoan
+{
+ public class GetAllLoanQueryHandler :
+ IRequestHandler>>
+ {
+ private readonly ILoanRepositorie _loanRepositorie;
+ private readonly ILogger _logger;
+ public GetAllLoanQueryHandler(ILoanRepositorie loanRepositorie
+ , ILogger logger)
+ {
+ _loanRepositorie = loanRepositorie;
+ _logger = logger;
+ }
+
+ public async Task>> Handle(GetAllLoanQuery request, CancellationToken cancellationToken)
+ {
+ try
+ {
+ var result = await _loanRepositorie.GetAllAsync(cancellationToken);
+ string message = string.Format(ConstMessages.Successfully
+ , nameof(GetAllLoanQueryHandler)
+ , "");
+ _logger.LogInformation(message);
+ return new OperationResult>(true, null, result);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, ex.Message);
+ return new OperationResult>(false, ex.Message, null);
+ }
+ }
+ }
+}
diff --git a/Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQuery.cs b/Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQuery.cs
new file mode 100644
index 0000000..e2cbff1
--- /dev/null
+++ b/Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQuery.cs
@@ -0,0 +1,16 @@
+using Domain.Exceptions;
+using MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Queries.GetByCodeLoan
+{
+ public record GetByCodeLoanQuery
+ : IRequest>
+ {
+ public required Guid Code { get; init; }
+ }
+}
diff --git a/Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQueryHandler.cs b/Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQueryHandler.cs
new file mode 100644
index 0000000..da98ba3
--- /dev/null
+++ b/Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQueryHandler.cs
@@ -0,0 +1,43 @@
+using Application.Services.Loan.Queries.GetAllLoan;
+using Domain.Exceptions;
+using Domain.IRepositories;
+using MediatR;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Application.Services.Loan.Queries.GetByCodeLoan
+{
+ public class GetByCodeLoanQueryHandler :
+ IRequestHandler>
+ {
+ private readonly ILoanRepositorie _loanRepositorie;
+ private readonly ILogger _logger;
+ public GetByCodeLoanQueryHandler(ILoanRepositorie loanRepositorie
+ , ILogger logger)
+ {
+ _loanRepositorie = loanRepositorie;
+ _logger = logger;
+ }
+ public async Task> Handle(GetByCodeLoanQuery request, CancellationToken cancellationToken)
+ {
+ try
+ {
+ var result = await _loanRepositorie.GetByCodeAsync(request.Code, cancellationToken);
+ string message = string.Format(ConstMessages.Successfully
+ , nameof(GetAllLoanQueryHandler)
+ , "");
+ _logger.LogInformation(message);
+ return new OperationResult(true, null, result);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, ex.Message);
+ return new OperationResult(false, ex.Message, null);
+ }
+ }
+ }
+}
diff --git a/Domain/Entity/Loan.cs b/Domain/Entity/Loan.cs
index 228f2eb..6349f58 100644
--- a/Domain/Entity/Loan.cs
+++ b/Domain/Entity/Loan.cs
@@ -12,7 +12,7 @@ namespace Domain.Entity
[AudiTable]
public class Loan : IEntity
{
- public Loan(Name nameBankSafe, Name firstName, Name lastName, Number numberOfInstallments, Money amount)
+ public Loan(Name nameBankSafe, Name firstName, Name lastName, Number numberOfInstallments, Money amount , Percent wage)
{
Code = Guid.NewGuid();
NameBankSafe = nameBankSafe;
@@ -20,6 +20,7 @@ public Loan(Name nameBankSafe, Name firstName, Name lastName, Number numberOfIns
LastName = lastName;
NumberOfInstallments = numberOfInstallments;
Amount = amount;
+ Wage = wage;
}
public Guid Code { get; private set; }
public Name NameBankSafe { get; private set; }
@@ -27,7 +28,7 @@ public Loan(Name nameBankSafe, Name firstName, Name lastName, Number numberOfIns
public Name LastName { get; private set; }
public Number NumberOfInstallments { get; private set; }
public Money Amount { get; private set; }
- public int Wage { get; private set; }
+ public Percent Wage { get; private set; }
public BankSafe BankSafe { get; private set; }
public ICollection LoanTransactions { get; private set; }
public ICollection LoanDocuments { get; private set; }
diff --git a/Domain/Exceptions/ConstMessages.cs b/Domain/Exceptions/ConstMessages.cs
index dd45137..11ebd85 100644
--- a/Domain/Exceptions/ConstMessages.cs
+++ b/Domain/Exceptions/ConstMessages.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Domain.Exceptions
{
@@ -19,6 +20,8 @@ public class ConstMessages
public const string NotNegative = "The {0} must not be negative";
public const string NotInventory = "The balance of {0} account in Bank {1} is {2}";
public const string MaximumLength = "{0} cannot be more than {1} characters";
+ public const string NotBetweenNumber = "The {0} number must be between {1} and {2}";
+
}
}
diff --git a/Domain/Exceptions/OperationResult.cs b/Domain/Exceptions/OperationResult.cs
index 49b1627..c84378e 100644
--- a/Domain/Exceptions/OperationResult.cs
+++ b/Domain/Exceptions/OperationResult.cs
@@ -23,7 +23,7 @@ public static OperationResult Failure(string message)
=> new(message);
public static OperationResult Success(T data)
- => new(data);
+ => new(data);
}
public class OperationResult : OperationResult
{
diff --git a/Domain/IRepositories/ILoanRepositorie.cs b/Domain/IRepositories/ILoanRepositorie.cs
index 3ed3185..e005880 100644
--- a/Domain/IRepositories/ILoanRepositorie.cs
+++ b/Domain/IRepositories/ILoanRepositorie.cs
@@ -1,4 +1,6 @@
-using System;
+using Domain.Entity;
+using Domain.ValueObjects;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +8,11 @@
namespace Domain.IRepositories
{
- internal class ILoanRepositorie
+ public interface ILoanRepositorie
{
+ ValueTask AddAsync(Loan loan, CancellationToken cancellationToken);
+ Task DeleteAsync(Guid code, CancellationToken cancellationToken);
+ Task GetByCodeAsync(Guid code , CancellationToken cancellationToken);
+ Task> GetAllAsync(CancellationToken cancellationToken);
}
}
diff --git a/Domain/ValueObjects/AccountNumber.cs b/Domain/ValueObjects/AccountNumber.cs
index 03a4e77..8f9e804 100644
--- a/Domain/ValueObjects/AccountNumber.cs
+++ b/Domain/ValueObjects/AccountNumber.cs
@@ -27,7 +27,6 @@ public AccountNumber(string value)
private OperationResult CheckAccountNumber(string value)
{
var result = OperationResult.CreateValidator(value)
- .Validate(x => string.IsNullOrWhiteSpace(x) , string.Format(ConstMessages.IsNull, nameof(AccountNumber)))
.Validate(x => x.Length != 16 , string.Format(ConstMessages.IncorrectFormat, nameof(AccountNumber)))
.Validate(x => !Validation.CheckNumberFormat(x) , string.Format(ConstMessages.IncorrectFormat, nameof(AccountNumber)));
return result;
diff --git a/Domain/ValueObjects/Name.cs b/Domain/ValueObjects/Name.cs
index 8f51e9d..6d18f01 100644
--- a/Domain/ValueObjects/Name.cs
+++ b/Domain/ValueObjects/Name.cs
@@ -29,15 +29,16 @@ public Name(string value)
}
private OperationResult CheckValueName(string value)
{
- if (string.IsNullOrWhiteSpace(value))
- {
- string message = string.Format(ConstMessages.IsNull, nameof(Name));
- return new OperationResult(false, message);
- }
- else
- {
- return new OperationResult(true, null);
- }
+ //if (string.IsNullOrWhiteSpace(value))
+ //{
+ // string message = string.Format(ConstMessages.IsNull, nameof(Name));
+ // return new OperationResult(false, message);
+ //}
+ //else
+ //{
+ // return new OperationResult(true, null);
+ //}
+ return new OperationResult(true, null);
}
protected override IEnumerable