medicalHistory: deletion when patient is deleted
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user