medicalHistory: deletion when patient is deleted
This commit is contained in:
@@ -16,10 +16,10 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.5.1" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.5.1"/>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.24.0"/>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2"/>
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Jwt;
|
||||
|
||||
using Infrastructure.Data;
|
||||
using Infrastructure.Services.HashingAlgorithms;
|
||||
using Infrastructure.Services.MongoDB;
|
||||
using Infrastructure.Services.PostgreSQL;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Infrastructure;
|
||||
|
||||
@@ -38,7 +35,7 @@ public static class DependencyInjection
|
||||
|
||||
return new MedicalHistoryMongoDbService(connectionString, databaseName, collectionName);
|
||||
});
|
||||
|
||||
|
||||
services.AddSingleton<IChatMongoDbService>(serviceProvider =>
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
@@ -48,7 +45,7 @@ public static class DependencyInjection
|
||||
|
||||
return new ChatMongoDbService(connectionString, databaseName, collectionName);
|
||||
});
|
||||
|
||||
|
||||
services.AddSingleton<IAppointmentsMongoDbService>(serviceProvider =>
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
@@ -58,19 +55,18 @@ public static class DependencyInjection
|
||||
|
||||
return new AppointmentsMongoDbService(connectionString, databaseName, collectionName);
|
||||
});
|
||||
|
||||
|
||||
// Other Services
|
||||
services.AddScoped<IHashingAlgorithms, HashingAlgorithms>();
|
||||
|
||||
|
||||
services.AddSingleton<IJwtService, JwtService>(serviceProvider =>
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
return new JwtService(configuration);
|
||||
});
|
||||
|
||||
|
||||
// services.AddScoped<IEmailService, EmailService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,16 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Application.Services.Jwt;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
public class JwtService : IJwtService
|
||||
{
|
||||
private readonly string _secretKey;
|
||||
private readonly string _issuer;
|
||||
private readonly string _audience;
|
||||
private readonly double _expiryMinutes;
|
||||
private readonly string _issuer;
|
||||
private readonly string _secretKey;
|
||||
|
||||
public JwtService(IConfiguration configuration)
|
||||
{
|
||||
@@ -34,13 +33,14 @@ public class JwtService : IJwtService
|
||||
Expires = DateTime.UtcNow.AddMinutes(_expiryMinutes),
|
||||
Issuer = _issuer,
|
||||
Audience = _audience,
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
SigningCredentials =
|
||||
new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
|
||||
|
||||
public bool ValidateJwtToken(string token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
@@ -58,9 +58,9 @@ public class JwtService : IJwtService
|
||||
ValidateAudience = true,
|
||||
ValidIssuer = _issuer,
|
||||
ValidAudience = _audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
}, out SecurityToken validatedToken);
|
||||
|
||||
ClockSkew = TimeSpan.Zero
|
||||
}, out var validatedToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
@@ -68,20 +68,14 @@ public class JwtService : IJwtService
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string RefreshToken(string token)
|
||||
{
|
||||
var principal = ValidateTokenAndGetPrincipal(token);
|
||||
if (principal == null)
|
||||
{
|
||||
throw new SecurityTokenException("Invalid token.");
|
||||
}
|
||||
if (principal == null) throw new SecurityTokenException("Invalid token.");
|
||||
|
||||
var emailClaim = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
|
||||
if (emailClaim == null)
|
||||
{
|
||||
throw new SecurityTokenException("Token does not contain an email claim.");
|
||||
}
|
||||
if (emailClaim == null) throw new SecurityTokenException("Token does not contain an email claim.");
|
||||
|
||||
return GenerateJwtToken(emailClaim.Value);
|
||||
}
|
||||
@@ -100,7 +94,7 @@ public class JwtService : IJwtService
|
||||
ValidateAudience = true,
|
||||
ValidIssuer = _issuer,
|
||||
ValidAudience = _audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ClockSkew = TimeSpan.Zero
|
||||
}, out _);
|
||||
|
||||
return principal;
|
||||
@@ -111,5 +105,4 @@ public class JwtService : IJwtService
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,46 +14,38 @@ public class AppointmentsMongoDbService : MongoDbService, IAppointmentsMongoDbSe
|
||||
public async Task<bool> IsAppointmentUnique(AppointmentManagementDto dto)
|
||||
{
|
||||
var appointment = dto.Appointment.ToUniversalTime();
|
||||
|
||||
|
||||
var criteria = new List<(string FieldName, string Value)>
|
||||
{
|
||||
("DoctorId", dto.DoctorId.ToString()),
|
||||
("PatientId", dto.PatientId.ToString())
|
||||
};
|
||||
|
||||
|
||||
var appointments = await FindAsync<Appointment>(criteria);
|
||||
|
||||
foreach (var app in appointments)
|
||||
{
|
||||
if (app.AppointmentsList.Any(a => a.Date == appointment.Date))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DoesAppointmentExists(AppointmentManagementDto dto)
|
||||
{
|
||||
var appointment = dto.Appointment.ToUniversalTime();
|
||||
|
||||
|
||||
var criteria = new List<(string FieldName, string Value)>
|
||||
{
|
||||
("DoctorId", dto.DoctorId.ToString()),
|
||||
("PatientId", dto.PatientId.ToString())
|
||||
};
|
||||
|
||||
|
||||
var appointments = await FindAsync<Appointment>(criteria);
|
||||
|
||||
foreach (var app in appointments)
|
||||
{
|
||||
if (app.AppointmentsList.Any(a => a.Date == appointment.Date))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +1,79 @@
|
||||
using Application.Services.Database;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class MongoDbService
|
||||
{
|
||||
public class MongoDbService
|
||||
private readonly string _collectionName;
|
||||
private readonly IMongoClient _database;
|
||||
private readonly string _databaseName;
|
||||
|
||||
public MongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
{
|
||||
private readonly IMongoClient _database;
|
||||
private readonly string _databaseName;
|
||||
private readonly string _collectionName;
|
||||
_databaseName = databaseName;
|
||||
_collectionName = collectionName;
|
||||
|
||||
public MongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
Console.WriteLine($"Connection string: {connectionString}");
|
||||
Console.WriteLine($"Database Name: {databaseName}");
|
||||
Console.WriteLine($"Collection Name: {collectionName}");
|
||||
|
||||
var settings = MongoClientSettings.FromConnectionString(connectionString);
|
||||
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
|
||||
_database = new MongoClient(settings);
|
||||
|
||||
try
|
||||
{
|
||||
_databaseName = databaseName;
|
||||
_collectionName = collectionName;
|
||||
|
||||
Console.WriteLine($"Connection string: {connectionString}");
|
||||
Console.WriteLine($"Database Name: {databaseName}");
|
||||
Console.WriteLine($"Collection Name: {collectionName}");
|
||||
|
||||
var settings = MongoClientSettings.FromConnectionString(connectionString);
|
||||
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
|
||||
_database = new MongoClient(settings);
|
||||
|
||||
try
|
||||
{
|
||||
var result = _database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
var result = _database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>()
|
||||
catch (Exception ex)
|
||||
{
|
||||
return _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
}
|
||||
|
||||
public async Task<List<T>> FindAsync<T>(List<(string FieldName, string Value)> criteria)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
|
||||
var filters = new List<FilterDefinition<T>>();
|
||||
foreach (var (FieldName, Value) in criteria) filters.Add(Builders<T>.Filter.Eq(FieldName, Value));
|
||||
|
||||
var combinedFilter = Builders<T>.Filter.And(filters);
|
||||
|
||||
return await collection.Find(combinedFilter).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task AddAsync<T>(T document)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
await collection.InsertOneAsync(document);
|
||||
}
|
||||
|
||||
public async Task ModifyAsync<T>(string keyField, string keyValue, T document)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.ReplaceOneAsync(filter, document, new ReplaceOptions { IsUpsert = true });
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<T>(string keyField, string keyValue)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
|
||||
public async Task DeleteByIdAsync<T>(string id)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq("_id", id);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>()
|
||||
{
|
||||
return _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
}
|
||||
|
||||
public async Task<List<T>> FindAsync<T>(List<(string FieldName, string Value)> criteria)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
|
||||
var filters = new List<FilterDefinition<T>>();
|
||||
foreach (var (FieldName, Value) in criteria) filters.Add(Builders<T>.Filter.Eq(FieldName, Value));
|
||||
|
||||
var combinedFilter = Builders<T>.Filter.And(filters);
|
||||
|
||||
return await collection.Find(combinedFilter).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task AddAsync<T>(T document)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
await collection.InsertOneAsync(document);
|
||||
}
|
||||
|
||||
public async Task ModifyAsync<T>(string keyField, string keyValue, T document)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.ReplaceOneAsync(filter, document, new ReplaceOptions { IsUpsert = true });
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<T>(string keyField, string keyValue)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
|
||||
public async Task DeleteByIdAsync<T>(string id)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq("_id", id);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Core.Entities;
|
||||
using Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Core.Entities;
|
||||
using Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Core.Entities;
|
||||
using Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Infrastructure")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+8663a186a056b0dfbfeebf9ae16be42b40101093")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+13bfa2bdd901a8b258d4cf881b5bb079066ab1c6")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Infrastructure")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Infrastructure")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
90add3a549a5ad86ae4df2628a148c5a9756bdaa56fd8988b963942aa2b42329
|
||||
30fec65cc45becf276dcf8ed6696bf02cadfb1a90989db6bf5e21ba21d2c8f27
|
||||
|
||||
Reference in New Issue
Block a user