-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create loanRepository and service and api
- Loading branch information
Showing
30 changed files
with
541 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ | |
|
||
<ItemGroup> | ||
<Folder Include="Behavior\" /> | ||
<Folder Include="Services\Loan\Queries\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 13 additions & 2 deletions
15
Application/Services/Loan/Commands/AddLoan/AddLoanCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
|
||
} |
48 changes: 46 additions & 2 deletions
48
Application/Services/Loan/Commands/AddLoan/AddLoanCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Application/Services/Loan/Commands/AddLoan/AddLoanCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Application/Services/Loan/Commands/DeleteLoan/DeleteLoanCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>> | ||
{ | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Application/Services/Loan/Queries/GetAllLoan/GetAllLoanQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Application/Services/Loan/Queries/GetByCodeLoan/GetByCodeLoanQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.