chat implemented
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Endpoints.Chats;
|
||||
|
||||
public class ChatHandler
|
||||
{
|
||||
private readonly IChatMongoDbService _chatMongoDbService;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
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 = ChatIdentifier.GenerateChatId(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 = ChatIdentifier.GenerateChatId(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,18 @@
|
||||
namespace Application.Endpoints.Chats;
|
||||
|
||||
public class ChatIdentifier
|
||||
{
|
||||
public static string GenerateChatId(Guid id1, Guid id2)
|
||||
{
|
||||
// Convert GUIDs to strings
|
||||
string strId1 = id1.ToString();
|
||||
string strId2 = id2.ToString();
|
||||
|
||||
// Sort the GUID strings
|
||||
string firstId = strId1.CompareTo(strId2) < 0 ? strId1 : strId2;
|
||||
string secondId = strId1.CompareTo(strId2) < 0 ? strId2 : strId1;
|
||||
|
||||
// Combine them to get a symmetric string
|
||||
return firstId + "-" + secondId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.Chats;
|
||||
|
||||
public class GetConversationDto
|
||||
{
|
||||
public Guid IdUser1 { get; set; }
|
||||
public Guid IdUser2 { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
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;
|
||||
|
||||
public class SendMessageDto
|
||||
{
|
||||
public Guid Sender { get; set; }
|
||||
public Guid Receiver { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
-2
@@ -1,6 +1,5 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
|
||||
|
||||
@@ -113,7 +112,6 @@ public class MedicalHistoryManageAuthorizationHandler
|
||||
}
|
||||
|
||||
var authorisations = documents[0].Authorisation;
|
||||
Console.WriteLine(authorisations);
|
||||
if (!authorisations.Contains(infoDto.DoctorId.ToString()))
|
||||
{
|
||||
return new BaseResponse()
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
namespace Application.Services.Database.MongoDB;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Application.Services.Database.MongoDB;
|
||||
|
||||
public interface IChatMongoDbService
|
||||
{
|
||||
// Additional methods specific to Chat database
|
||||
IMongoCollection<T> GetCollection<T>();
|
||||
|
||||
Task<List<T>> FindAsync<T>(List<(string FieldName, string Value)> criteria);
|
||||
|
||||
Task AddAsync<T>(T document);
|
||||
|
||||
Task ModifyAsync<T>(string keyField, string keyValue, T document);
|
||||
|
||||
Task DeleteAsync<T>(string keyField, string keyValue);
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -14,14 +14,12 @@
|
||||
"outputPath": "C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\Andrei Cerbu\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
"C:\\Users\\Andrei Cerbu\\AppData\\Roaming\\NuGet\\NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
@@ -69,7 +67,7 @@
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.202/PortableRuntimeIdentifierGraph.json"
|
||||
"runtimeIdentifierGraphPath": "C:\\Users\\Andrei Cerbu\\.dotnet\\sdk\\8.0.203/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -83,14 +81,12 @@
|
||||
"outputPath": "C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Core\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\Andrei Cerbu\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
"C:\\Users\\Andrei Cerbu\\AppData\\Roaming\\NuGet\\NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
@@ -130,7 +126,7 @@
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.202/PortableRuntimeIdentifierGraph.json"
|
||||
"runtimeIdentifierGraphPath": "C:\\Users\\Andrei Cerbu\\.dotnet\\sdk\\8.0.203/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Application")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b4eca98e0a616e4db63ed57610d69f27f43f4765")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+48eb2adcbd149cd66c77ba558f492179d2bf29be")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Application")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Application")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
78ff498bf54e9d999affcd9081c61dde8845fd1e78f6485753e0ca4436dc7cdc
|
||||
4923b7cd0f6d06f807cd85db7dc3c654c90e66ef676c3798df7f8469fdb9dd17
|
||||
|
||||
Binary file not shown.
@@ -1 +1 @@
|
||||
003e629f8ad4defbd9bc4b4a250b4753763e3945de1c7fb70b833ff828554693
|
||||
a14527b9f0436826149b6114fc93ca0d3615f169c5065e509d90189e6d49d7bf
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
|
||||
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/b4eca98e0a616e4db63ed57610d69f27f43f4765/*"}}
|
||||
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/48eb2adcbd149cd66c77ba558f492179d2bf29be/*"}}
|
||||
Binary file not shown.
Binary file not shown.
@@ -851,14 +851,12 @@
|
||||
"outputPath": "C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\Andrei Cerbu\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
"C:\\Users\\Andrei Cerbu\\AppData\\Roaming\\NuGet\\NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
@@ -906,7 +904,7 @@
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.202/PortableRuntimeIdentifierGraph.json"
|
||||
"runtimeIdentifierGraphPath": "C:\\Users\\Andrei Cerbu\\.dotnet\\sdk\\8.0.203/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"version": 2,
|
||||
"dgSpecHash": "+8cvg1kefgWQCHZNz0UFBqaMFHCU9vs6eKiSXlImrLEp7SlxQgKE4onuOThyjCArZTaU+iPlTxlBKu/wIdbkNg==",
|
||||
"dgSpecHash": "6rK5hZ4j6ClAzfxuWc17Wft98VDUQupYiEeI5viBfPq8M3e7wy2vBJ/LEy/lClFvezbz4wHPGdpeQDPOA9ZtBw==",
|
||||
"success": true,
|
||||
"projectFilePath": "C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\Application.csproj",
|
||||
"expectedPackageFiles": [
|
||||
|
||||
@@ -1 +1 @@
|
||||
"restore":{"projectUniqueName":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\Application.csproj","projectName":"Application","projectPath":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\Application.csproj","outputPath":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\obj\\","projectStyle":"PackageReference","originalTargetFrameworks":["net8.0"],"sources":{"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\":{},"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net8.0":{"targetAlias":"net8.0","projectReferences":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Core\\Core.csproj":{"projectPath":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Core\\Core.csproj"}}}},"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net8.0":{"targetAlias":"net8.0","dependencies":{"FluentValidation":{"target":"Package","version":"[11.9.0, )"},"MongoDB.Driver":{"target":"Package","version":"[2.24.0, )"}},"imports":["net461","net462","net47","net471","net472","net48","net481"],"assetTargetFallback":true,"warn":true,"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"C:\\Program Files\\dotnet\\sdk\\8.0.202/PortableRuntimeIdentifierGraph.json"}}
|
||||
"restore":{"projectUniqueName":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\Application.csproj","projectName":"Application","projectPath":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\Application.csproj","outputPath":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Application\\obj\\","projectStyle":"PackageReference","originalTargetFrameworks":["net8.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net8.0":{"targetAlias":"net8.0","projectReferences":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Core\\Core.csproj":{"projectPath":"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\backend\\Core\\Core.csproj"}}}},"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net8.0":{"targetAlias":"net8.0","dependencies":{"FluentValidation":{"target":"Package","version":"[11.9.0, )"},"MongoDB.Driver":{"target":"Package","version":"[2.24.0, )"}},"imports":["net461","net462","net47","net471","net472","net48","net481"],"assetTargetFallback":true,"warn":true,"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"C:\\Users\\Andrei Cerbu\\.dotnet\\sdk\\8.0.203/PortableRuntimeIdentifierGraph.json"}}
|
||||
@@ -1 +1 @@
|
||||
17122552827049708
|
||||
17125558124497896
|
||||
@@ -1 +1 @@
|
||||
17125075151933320
|
||||
17125584645910462
|
||||
Reference in New Issue
Block a user