finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -1,144 +0,0 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Core.Entities;
namespace Application.Endpoints.Chats;
public class ChatHandler
{
private readonly IChatMongoDbService _chatMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public ChatHandler(IChatMongoDbService chatMongoDbService, IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
_chatMongoDbService = chatMongoDbService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
public async Task<BaseResponse> HandleSendMessage(SendMessageDto sendMessageDto)
{
var validation = new SendMessageValidator();
var validationResult = await validation.ValidateAsync(sendMessageDto);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
if (!await CheckForUsersExistence(sendMessageDto.Sender, sendMessageDto.Receiver))
return new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
Message = "Users can't be found in the system.",
Data = null
};
var chatId = IdentifierGenerator.GenerateId(sendMessageDto.Sender, sendMessageDto.Receiver);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
var documents = await _chatMongoDbService.FindAsync<Chat>(criteria);
if (!documents.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history not found.",
Data = null
};
var chat = documents[0].Messages;
chat.Add(new Message(sendMessageDto.Sender, sendMessageDto.Message));
var newChat = new Chat();
newChat.SetId(chatId);
newChat.SetMessages(chat);
await _chatMongoDbService.ModifyAsync("_id", chatId, newChat);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
public async Task<BaseResponse> HandleGetConversation(GetConversationDto getConversationDto)
{
var validation = new GetConversationValidator();
var validationResult = await validation.ValidateAsync(getConversationDto);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
if (!await CheckForUsersExistence(getConversationDto.IdUser1, getConversationDto.IdUser2))
return new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
Message = "Users can't be found in the system.",
Data = null
};
var chatId = IdentifierGenerator.GenerateId(getConversationDto.IdUser1, getConversationDto.IdUser2);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
Chat? chat = null;
var documents = await _chatMongoDbService.FindAsync<Chat>(criteria);
if (!documents.Any())
{
chat = new Chat();
chat.SetId(chatId);
await _chatMongoDbService.AddAsync(chat);
}
else
{
chat = documents[0];
}
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Fetching messages.",
Data = chat
};
}
private async Task<bool> CheckForUsersExistence(Guid idUser1, Guid idUser2)
{
var firstCheck = await _patientRepository.GetByIdAsync(idUser1) != null &&
await _doctorRepository.GetByIdAsync(idUser2) != null;
var secondCheck = await _patientRepository.GetByIdAsync(idUser2) != null &&
await _doctorRepository.GetByIdAsync(idUser1) != null;
return firstCheck || secondCheck;
}
}
@@ -0,0 +1,7 @@
namespace Application.Endpoints.Chats.GetChats;
public class GetConversationsCommand
{
public Guid IdUser1 { get; set; } = Guid.Empty;
public Guid IdUser2 { get; set; } = Guid.Empty;
}
@@ -0,0 +1,59 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.Chats.GetChats;
public class GetConversationsHandler(
IChatMongoDbService chatMongoDbService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
public async Task<BaseResponse> HandleGetConversation(GetConversationsCommand getConversationCommand,
CancellationToken token)
{
var validation = new GetConversationsValidator(doctorRepository, patientRepository);
var validationResult = await validation.ValidateAsync(getConversationCommand, token);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var chatId = IdentifierGenerator.GenerateId(getConversationCommand.IdUser1, getConversationCommand.IdUser2);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
Chat? chat = null;
var documents = await chatMongoDbService.FindAsync<Chat>(criteria, token);
if (!documents.Any())
{
chat = new Chat();
chat.SetId(chatId);
await chatMongoDbService.AddAsync(chat, token);
}
else
{
chat = documents[0];
}
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Fetching messages.",
Data = chat
};
}
}
@@ -0,0 +1,37 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Chats.GetChats;
public class GetConversationsValidator : AbstractValidator<GetConversationsCommand>
{
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
public GetConversationsValidator(IDoctorRepository doctorRepository, IPatientRepository patientRepository)
{
RuleFor(x => x.IdUser1)
.NotEmpty().WithMessage("IdUser1 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.IdUser2)
.NotEmpty().WithMessage("IdUser2 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(CheckForUsersExistence).WithMessage("One of the users is not existing.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
private async Task<bool> CheckForUsersExistence(GetConversationsCommand request, CancellationToken token)
{
var firstCheck = await _patientRepository.GetByIdAsync(request.IdUser1, token) != null &&
await _doctorRepository.GetByIdAsync(request.IdUser2, token) != null;
var secondCheck = await _patientRepository.GetByIdAsync(request.IdUser2, token) != null &&
await _doctorRepository.GetByIdAsync(request.IdUser1, token) != null;
return firstCheck || secondCheck;
}
}
@@ -1,7 +0,0 @@
namespace Application.Endpoints.Chats;
public class GetConversationDto
{
public Guid IdUser1 { get; set; }
public Guid IdUser2 { get; set; }
}
@@ -1,15 +0,0 @@
using FluentValidation;
namespace Application.Endpoints.Chats;
public class GetConversationValidator : AbstractValidator<GetConversationDto>
{
public GetConversationValidator()
{
RuleFor(x => x.IdUser1)
.NotEmpty().WithMessage("IdUser1 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.IdUser2)
.NotEmpty().WithMessage("IdUser2 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
}
@@ -0,0 +1,8 @@
namespace Application.Endpoints.Chats.SendMessage;
public class SendMessageCommand
{
public Guid Sender { get; set; } = Guid.Empty;
public Guid Receiver { get; set; } = Guid.Empty;
public string Message { get; set; } = string.Empty;
}
@@ -0,0 +1,61 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.Chats.SendMessage;
public class SendMessageHandler(
IChatMongoDbService chatMongoDbService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
public async Task<BaseResponse> Handle(SendMessageCommand request, CancellationToken token)
{
var validation = new SendMessageValidator(doctorRepository, patientRepository);
var validationResult = await validation.ValidateAsync(request, token);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var chatId = IdentifierGenerator.GenerateId(request.Sender, request.Receiver);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
var documents = await chatMongoDbService.FindAsync<Chat>(criteria, token);
if (!documents.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history not found.",
Data = null
};
var chat = documents[0].Messages;
chat.Add(new Message(request.Sender, request.Message));
var newChat = new Chat();
newChat.SetId(chatId);
newChat.SetMessages(chat);
await chatMongoDbService.ModifyAsync("_id", chatId, newChat, token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -0,0 +1,40 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Chats.SendMessage;
public class SendMessageValidator : AbstractValidator<SendMessageCommand>
{
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public SendMessageValidator(IDoctorRepository doctorRepository, IPatientRepository patientRepository)
{
RuleFor(x => x.Sender)
.NotEmpty().WithMessage("Sender Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Receiver)
.NotEmpty().WithMessage("Receiver Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Message)
.NotEmpty().WithMessage("Message is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(CheckForUsersExistence).WithMessage("One of the users is not existing.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
private async Task<bool> CheckForUsersExistence(SendMessageCommand request, CancellationToken token)
{
var firstCheck = await _patientRepository.GetByIdAsync(request.Sender, token) != null &&
await _doctorRepository.GetByIdAsync(request.Receiver, token) != null;
var secondCheck = await _patientRepository.GetByIdAsync(request.Receiver, token) != null &&
await _doctorRepository.GetByIdAsync(request.Sender, token) != null;
return firstCheck || secondCheck;
}
}
@@ -1,8 +0,0 @@
namespace Application.Endpoints.Chats;
public class SendMessageDto
{
public Guid Sender { get; set; }
public Guid Receiver { get; set; }
public string Message { get; set; }
}
@@ -1,18 +0,0 @@
using FluentValidation;
namespace Application.Endpoints.Chats;
public class SendMessageValidator : AbstractValidator<SendMessageDto>
{
public SendMessageValidator()
{
RuleFor(x => x.Sender)
.NotEmpty().WithMessage("Sender Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Receiver)
.NotEmpty().WithMessage("Receiver Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Message)
.NotEmpty().WithMessage("Message is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
}