Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
477 changes: 477 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using OliSaude.Application.Commom;

namespace OliSaude.Application.Clientes.CreateCliente
{
public class ClienteResponse : Response
{
public ClienteResponse()
{

}
public ClienteResponse(ResponseData obj, string mensagem)
{
Data = obj;
Mensagem = mensagem;
StatusCode = 201;
}
public ClienteResponse(string mensagem, int status)
{
Mensagem = mensagem;
StatusCode=status;
}
public ResponseData? Data { get; set; }
}

public record ResponseData(int Id, string Nome);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MediatR;
using OliSaude.Domain.Enums;

namespace OliSaude.Application.Clientes.CreateCliente
{
public class CreateClienteCommand : IRequest<ClienteResponse>
{
public string Nome { get; set; }
public DateTime DataNascimento { get; set; }
public ESexo Sexo { get; set; }
public string NomeProblema { get; set; }
public int GrauProblema { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using MediatR;
using OliSaude.Application.Interfaces;
using OliSaude.Domain.Entities;
using OliSaude.Domain.ValueObject;

namespace OliSaude.Application.Clientes.CreateCliente
{
public class CreateClienteHandler : IRequestHandler<CreateClienteCommand, ClienteResponse>
{
private readonly IClienteRepositorio _repositorio;

public CreateClienteHandler(IClienteRepositorio repositorio)
{
_repositorio = repositorio;
}

public async Task<ClienteResponse> Handle(CreateClienteCommand request, CancellationToken cancellationToken)
{
try
{
var problema = new ProblemaSaude(request.NomeProblema, request.GrauProblema);
var cliente = new Cliente(request.Nome, request.DataNascimento, request.Sexo, problema);
await _repositorio.SaveAsync(cliente, cancellationToken);

if (cliente.Id == 0)
return new ClienteResponse("Não foi possivel salvar o cliente", 404);

var obj = new ResponseData(cliente.Id, cliente.Nome);

return new ClienteResponse(obj, "Cliente Salvo com sucesso");

}catch (Exception ex)
{
return new ClienteResponse(ex.Message, 500);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MediatR;

namespace OliSaude.Application.Clientes.DeleteCliente
{
public class DeleteClienteCommand : IRequest<DeleteResponse>
{
public int Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MediatR;
using OliSaude.Application.Interfaces;

namespace OliSaude.Application.Clientes.DeleteCliente
{
public class DeleteClienteHandler : IRequestHandler<DeleteClienteCommand, DeleteResponse>
{
private readonly IClienteRepositorio _repositorio;

public DeleteClienteHandler(IClienteRepositorio repositorio)
{
_repositorio = repositorio;
}

public async Task<DeleteResponse> Handle(DeleteClienteCommand request, CancellationToken cancellationToken)
{
try
{
var cliente = await _repositorio.GetClienteAsync(request.Id);
if (cliente is null)
return new DeleteResponse("Não foi encontrado um cliente com esse Id", 400);
_repositorio.DeleteCliente(cliente);

var data = new DeleteData(cliente.Id, cliente.Nome, cliente.ProblemaDeSaude.Nome);

return new DeleteResponse(data, "Cliente Removido com sucesso");
}catch (Exception ex)
{
return new DeleteResponse(ex.Message, 500);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using OliSaude.Application.Commom;

namespace OliSaude.Application.Clientes.DeleteCliente
{
public class DeleteResponse : Response
{
public DeleteResponse()
{

}
public DeleteResponse(string mensagem, int status)
{
Mensagem = mensagem;
StatusCode = status;
}

public DeleteResponse(DeleteData data, string mensagem)
{
Data = data;
Mensagem = mensagem;
StatusCode = 200;
}

public DeleteData? Data { get; set; }

}

public record DeleteData(int Id, string Name, string Problema);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AutoMapper;
using MediatR;
using OliSaude.Application.Dto;
using OliSaude.Application.Interfaces;

namespace OliSaude.Application.Clientes.Queries
{
public class GetAllClienteQuery : IRequest<IEnumerable<ClienteDto>>
{
public static bool TryParse(string value, out GetAllClienteQuery result)
{
if (!string.IsNullOrWhiteSpace(value))
{
result = new GetAllClienteQuery();
return true;
}
result = null;
return false;

}
}

public class GetAllClienteHandler : IRequestHandler<GetAllClienteQuery, IEnumerable<ClienteDto>>
{
private readonly IClienteRepositorio _repositorio;
private readonly IMapper _mapper;

public GetAllClienteHandler(IClienteRepositorio repositorio, IMapper mapper)
{
_repositorio = repositorio;
_mapper = mapper;
}


public async Task<IEnumerable<ClienteDto>> Handle(GetAllClienteQuery request, CancellationToken cancellationToken)
{
await Task.CompletedTask;
var clientes = _repositorio.GetAllClientes();
var clientesDto = _mapper.Map<IEnumerable<ClienteDto>>(clientes);

return clientesDto;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AutoMapper;
using MediatR;
using OliSaude.Application.Dto;
using OliSaude.Application.Interfaces;
using OliSaude.Domain.Entities;

namespace OliSaude.Application.Clientes.Queries
{
public class GetClienteByIdQuery: IRequest<ClienteDto>
{
public int Id { get; set; }

public static bool TryParse(string value, out GetClienteByIdQuery query)
{
if(value is null)
{
query = default;
return false;
}
query = new GetClienteByIdQuery { Id = int.Parse(value)};
return true;
}

}

public class GetClienteByIdHandler : IRequestHandler<GetClienteByIdQuery, ClienteDto>
{
private readonly IMapper _mapper;
private readonly IClienteRepositorio _repositorio;

public GetClienteByIdHandler(IMapper mapper, IClienteRepositorio repositorio)
{
_mapper = mapper;
_repositorio = repositorio;
}

public async Task<ClienteDto> Handle(GetClienteByIdQuery request, CancellationToken cancellationToken)
{
var cliente = await _repositorio.GetClienteAsync(request.Id);
var clienteDto = _mapper.Map<ClienteDto>(cliente);
return clienteDto;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using AutoMapper;
using MediatR;
using OliSaude.Application.Dto;
using OliSaude.Application.Interfaces;
using OliSaude.Domain.Entities;

namespace OliSaude.Application.Clientes.Queries
{
public class GetClienteMaiorRiscoQuery : IRequest<IEnumerable<ClienteDto>>
{

}

public class GetClienteMaiorRiscoHandler : IRequestHandler<GetClienteMaiorRiscoQuery, IEnumerable<ClienteDto>>
{
private readonly IClienteRepositorio _repo;
private readonly IMapper _mapper;

public GetClienteMaiorRiscoHandler(IClienteRepositorio repo, IMapper mapper)
{
_repo = repo;
_mapper = mapper;
}

public async Task<IEnumerable<ClienteDto>> Handle(GetClienteMaiorRiscoQuery request, CancellationToken cancellationToken)
{
await Task.CompletedTask;
var clientes = _repo.GetAllClientes().OrderByDescending(cliente => CalculoScore(cliente)).Take(10);

return _mapper.Map<IEnumerable<ClienteDto>>(clientes);
}
private double CalculoScore(Cliente cliente)
{
var sd = cliente.ProblemaDeSaude.Grau;
var denominador = 1 + Math.Pow(Math.E, -(-2.8 + sd));
var score = (1 / denominador) * 100;
return score;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MediatR;
using OliSaude.Application.Clientes.UpdateCliente;

namespace OliSaude.Application.Clientes.UpdateUser;

public class UpdateClienteCommand : IRequest<UpdateResponse>
{
public int Id { get; set; }
public string Nome { get; set; }
public DateTime DataNascimento { get; set; }
public string NomeProblema { get; set; }
public int GrauProblema { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using MediatR;
using OliSaude.Application.Clientes.UpdateUser;
using OliSaude.Application.Interfaces;
using OliSaude.Domain.ValueObject;

namespace OliSaude.Application.Clientes.UpdateCliente
{
public class UpdateClienteHandler : IRequestHandler<UpdateClienteCommand, UpdateResponse>
{
private readonly IClienteRepositorio _repositoio;

public UpdateClienteHandler(IClienteRepositorio repositorio)
{
_repositoio = repositorio;
}

public async Task<UpdateResponse> Handle(UpdateClienteCommand request, CancellationToken cancellationToken)
{
try
{
var cliente = await _repositoio.GetClienteAsync(request.Id);

if (cliente is null)
return new UpdateResponse("Cliente invalido", 400);

var problema = new ProblemaSaude(request.NomeProblema, request.GrauProblema);
cliente.UpdateCliente(request.Nome, request.DataNascimento, problema);
await _repositoio.UpdateClienteAsync(cliente, cancellationToken);

var data = new UpdateData(cliente.Id,
cliente.Nome,
Enum.GetName(cliente.Sexo),
cliente.DataNascimento,
cliente.ProblemaDeSaude.Nome,
cliente.ProblemaDeSaude.Grau,
cliente.DataCriacao,
cliente.DataActualizacao);

return new UpdateResponse(data, "Cliente actualizado com sucesso");
}catch (Exception ex)
{
return new UpdateResponse(ex.Message, 500);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using OliSaude.Application.Commom;

namespace OliSaude.Application.Clientes.UpdateCliente
{
public class UpdateResponse : Response
{
public UpdateResponse()
{

}
public UpdateResponse(string mensagem, int status)
{
Mensagem = mensagem;
StatusCode = status;
}

public UpdateResponse(UpdateData data, string mensagem)
{
Data = data;
Mensagem = mensagem;
StatusCode = 200;
}

public UpdateData? Data { get; set; }
}

public record UpdateData(int Id,
string Name,
string Sexo,
DateTime DataNascimento,
string Problema,
int Grau,
DateTime DataCriacao,
DateTime DataActualizacao);
}
10 changes: 10 additions & 0 deletions dev/OliSaude/OliSaude.Application/Commom/Response.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace OliSaude.Application.Commom
{
public abstract class Response
{
public string Mensagem { get; set; } = string.Empty;
public int StatusCode { get; set; } = 400;
public bool IsSucess => StatusCode >= 200 && StatusCode <= 299;

}
}
Loading