Skip to content

Commit

Permalink
create loanRepository and service and api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mstaheri committed Jul 9, 2024
1 parent 6896a36 commit 7420a5f
Show file tree
Hide file tree
Showing 30 changed files with 541 additions and 56 deletions.
1 change: 0 additions & 1 deletion Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

<ItemGroup>
<Folder Include="Behavior\" />
<Folder Include="Services\Loan\Queries\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +18,7 @@ public UpdateBankSafeCommandValidator()

RuleFor(p => p.SharePrice)
.NotNull().WithMessage(string.Format(ConstMessages.IsNull, "SharePrice"));

}
}
}
15 changes: 13 additions & 2 deletions Application/Services/Loan/Commands/AddLoan/AddLoanCommand.cs
Original file line number Diff line number Diff line change
@@ -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<OperationResult<Guid>>
{
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; }
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
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;
using System.Threading.Tasks;

namespace Application.Services.Loan.Commands.AddLoan
{
internal class AddLoanCommandHandler
public class AddLoanCommandHandler
: IRequestHandler<AddLoanCommand, OperationResult<Guid>>
{
private readonly ILoanRepositorie _loanRepositorie;
private readonly IUnitOfWork _unitOfWork;
private readonly ILogger<AddLoanCommandHandler> _logger;
public AddLoanCommandHandler(ILoanRepositorie loanRepositorie,
IUnitOfWork unitOfWork
, ILogger<AddLoanCommandHandler> logger)
{
_loanRepositorie = loanRepositorie;
_unitOfWork = unitOfWork;
_logger = logger;
}
public async Task<OperationResult<Guid>> 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<Guid>(true, null , loan.Code);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return new OperationResult<Guid>(false, ex.Message , Guid.Empty);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AddLoanCommand>
{
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"));

}
}
}
15 changes: 15 additions & 0 deletions Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommand.cs
Original file line number Diff line number Diff line change
@@ -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<OperationResult>
{
public required Guid Code { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -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<DeleteLoanCommand, OperationResult>
{
private readonly ILoanRepositorie _loanRepositorie;
private readonly IUnitOfWork _unitOfWork;
private readonly ILogger<DeleteLoanCommandHandler> _logger;
public DeleteLoanCommandHandler(ILoanRepositorie loanRepositorie,
IUnitOfWork unitOfWork
, ILogger<DeleteLoanCommandHandler> logger)
{
_loanRepositorie = loanRepositorie;
_unitOfWork = unitOfWork;
_logger = logger;
}
public async Task<OperationResult> 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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<DeleteLoanCommand>
{
public DeleteLoanCommandValidator()
{
RuleFor(p => p.Code)
.NotNull().WithMessage(string.Format(ConstMessages.IsNull, "Code"));
}
}
}
16 changes: 16 additions & 0 deletions Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQuery.cs
Original file line number Diff line number Diff line change
@@ -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<OperationResult<List<Domain.Entity.Loan>>>
{

}
}
Original file line number Diff line number Diff line change
@@ -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<GetAllLoanQuery , OperationResult<List<Domain.Entity.Loan>>>
{
private readonly ILoanRepositorie _loanRepositorie;
private readonly ILogger<GetAllLoanQueryHandler> _logger;
public GetAllLoanQueryHandler(ILoanRepositorie loanRepositorie
, ILogger<GetAllLoanQueryHandler> logger)
{
_loanRepositorie = loanRepositorie;
_logger = logger;
}

public async Task<OperationResult<List<Domain.Entity.Loan>>> 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<List<Domain.Entity.Loan>>(true, null, result);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return new OperationResult<List<Domain.Entity.Loan>>(false, ex.Message, null);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<OperationResult<Domain.Entity.Loan>>
{
public required Guid Code { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -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<GetByCodeLoanQuery, OperationResult<Domain.Entity.Loan>>
{
private readonly ILoanRepositorie _loanRepositorie;
private readonly ILogger<GetByCodeLoanQueryHandler> _logger;
public GetByCodeLoanQueryHandler(ILoanRepositorie loanRepositorie
, ILogger<GetByCodeLoanQueryHandler> logger)
{
_loanRepositorie = loanRepositorie;
_logger = logger;
}
public async Task<OperationResult<Domain.Entity.Loan>> 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<Domain.Entity.Loan>(true, null, result);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return new OperationResult<Domain.Entity.Loan>(false, ex.Message, null);
}
}
}
}
5 changes: 3 additions & 2 deletions Domain/Entity/Loan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ 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;
FirstName = firstName;
LastName = lastName;
NumberOfInstallments = numberOfInstallments;
Amount = amount;
Wage = wage;
}
public Guid Code { get; private set; }
public Name NameBankSafe { get; private set; }
public Name FirstName { get; private set; }
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> LoanTransactions { get; private set; }
public ICollection<LoanDocument> LoanDocuments { get; private set; }
Expand Down
3 changes: 3 additions & 0 deletions Domain/Exceptions/ConstMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Domain.Exceptions
{
Expand All @@ -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}";


}
}
Loading

0 comments on commit 7420a5f

Please sign in to comment.