This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 00:03:24 +03:00
parent 2ccec617a5
commit b48d5ee19e
168 changed files with 2877 additions and 1146 deletions
@@ -0,0 +1,19 @@
using System.Security.Cryptography;
using System.Text;
using Application.Services.HashingAlgorithms;
namespace Infrastructure.Services.HashingAlgorithms;
public class HashingAlgorithms : IHashingAlgorithms
{
public string? SHA256Algorithm(string? password)
{
if (password == null) return null;
using (var sha256 = SHA256.Create())
{
var bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
return Convert.ToBase64String(bytes);
}
}
}
@@ -0,0 +1,53 @@
using Application.Services.Database;
using MongoDB.Driver;
namespace Infrastructure.Services.MongoDB;
public class MongoDbService : IMongoDbService
{
private readonly IMongoDatabase _database;
public MongoDbService(string connectionString)
{
var url = new MongoUrl(connectionString);
var client = new MongoClient(url);
_database = client.GetDatabase(url.DatabaseName);
}
public IMongoCollection<T> GetCollection<T>(string collectionName)
{
return _database.GetCollection<T>(collectionName);
}
public async Task<List<T>> FindAsync<T>(string collectionName, List<(string FieldName, string Value)> criteria)
{
var collection = _database.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>(string collectionName, T document)
{
var collection = _database.GetCollection<T>(collectionName);
await collection.InsertOneAsync(document);
}
public async Task ModifyAsync<T>(string collectionName, string keyField, string keyValue, T document)
{
var collection = _database.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 collectionName, string keyField, string keyValue)
{
var collection = _database.GetCollection<T>(collectionName);
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
await collection.DeleteOneAsync(filter);
}
}
@@ -12,7 +12,7 @@ public class BasePostgreSQLRepository<T> where T : class
_context = context;
}
public async Task<T> GetByIdAsync(Guid id)
public async Task<T?> GetByIdAsync(Guid id)
{
return await _context.Set<T>().FindAsync(id);
}
@@ -40,4 +40,4 @@ public class BasePostgreSQLRepository<T> where T : class
_context.Set<T>().Remove(entity);
await _context.SaveChangesAsync();
}
}
}
@@ -5,8 +5,16 @@ using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Services.PostgreSQL;
public class DoctorRepository(HealthcareManagerDatabase context) : BasePostgreSQLRepository<Doctor>(context), IDoctorRepository
public class DoctorRepository(HealthcareManagerDatabase context)
: BasePostgreSQLRepository<Doctor>(context), IDoctorRepository
{
public async Task<Doctor?> FindByEmailAsync(string email)
=> await _context.Doctors.FirstOrDefaultAsync(d => d.Email == email);
}
public async Task<Doctor?> FindByEmailAsync(string email)
{
return await _context.Doctors.FirstOrDefaultAsync(d => d.Email == email);
}
public async Task<bool> CredentialsMatch(string email, string password)
{
return _context.Doctors.Any(u => u.Email == email && u.Password == password);
}
}
@@ -12,6 +12,7 @@ public class MedicalHistoryRepository : BasePostgreSQLRepository<MedicalHistory>
}
public async Task<MedicalHistory?> GetByUserIdAsync(Guid userId)
=> await _context.MedicalHistories.FirstOrDefaultAsync(d => d.UserId == userId);
}
{
return await _context.MedicalHistories.FirstOrDefaultAsync(d => d.UserId == userId);
}
}
@@ -1,12 +0,0 @@
using Application.Services.Database;
using Core.Entities;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Services.PostgreSQL;
public class PacientRepository(HealthcareManagerDatabase context) : BasePostgreSQLRepository<Pacient>(context), IPacientRepository
{
public async Task<Pacient?> FindByEmailAsync(string email)
=> await _context.Pacients.FirstOrDefaultAsync(d => d.Email == email);
}
@@ -0,0 +1,15 @@
using Application.Services.Database;
using Core.Entities;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Services.PostgreSQL;
public class PatientRepository(HealthcareManagerDatabase context)
: BasePostgreSQLRepository<Patient>(context), IPatientRepository
{
public async Task<Patient?> FindByEmailAsync(string email)
{
return await _context.Pacients.FirstOrDefaultAsync(d => d.Email == email);
}
}