This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 13:37:58 +03:00
parent 333ad8be09
commit 370bbdedcd
164 changed files with 3915 additions and 161 deletions
@@ -0,0 +1,129 @@
using HealthcareManagerUI.Models;
using HealthcareManagerUI.Services.Http;
namespace HealthcareManagerUI.Services.Authentication;
// In AuthenticationService.cs
public class AuthenticationService : IAuthenticationService
{
private readonly IHttpService _httpService;
private readonly string _apiUrl = "http://localhost:5151/api";
public AuthenticationService(IHttpService httpService)
{
_httpService = httpService;
}
public async Task<BaseResponse> LoginDoctor(UserLoginModel userLoginModel)
{
try
{
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/login", userLoginModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = 400,
Data = null,
Message = ex.Message
};
}
}
public async Task<BaseResponse> LoginPatient(UserLoginModel userLoginModel)
{
try
{
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/login", userLoginModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse {
StatusCode = 400,
Data = null,
Message = ex.Message
};
}
}
public async Task<BaseResponse> RegisterDoctor(UserRegisterModel userRegistrationModel)
{
try
{
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/register", userRegistrationModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = 400,
Data = null,
Message = ex.Message
};
}
}
public async Task<BaseResponse> RegisterPatient(UserRegisterModel userRegisterModel)
{
var patientRegisterDto = new PatientRegisterModel
{
Name = userRegisterModel.Name,
Email = userRegisterModel.Email,
Password = userRegisterModel.Password
};
try
{
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/register", userRegisterModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = 400,
Data = null,
Message = ex.Message
};
}
}
public async Task<BaseResponse> ResetDoctorPassword(UserRegisterModel userResetPasswordModel)
{
try
{
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/reset_password", userResetPasswordModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = 400,
Data = null,
Message = ex.Message
};
}
}
public async Task<BaseResponse> ResetPatientPassword(UserRegisterModel userResetPasswordModel)
{
try
{
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/reset_password", userResetPasswordModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = 400,
Data = null,
Message = ex.Message
};
}
}
}
@@ -0,0 +1,18 @@
using HealthcareManagerUI.Models;
namespace HealthcareManagerUI.Services.Authentication;
public interface IAuthenticationService
{
Task<BaseResponse> LoginDoctor(UserLoginModel userLoginModel);
Task<BaseResponse> LoginPatient(UserLoginModel userLoginModel);
Task<BaseResponse> RegisterDoctor(UserRegisterModel userRegistrationModel);
Task<BaseResponse> RegisterPatient(UserRegisterModel userRegistrationModel);
Task<BaseResponse> ResetDoctorPassword(UserRegisterModel userResetPasswordModel);
Task<BaseResponse> ResetPatientPassword(UserRegisterModel userResetPasswordModel);
}
@@ -0,0 +1,83 @@
using System.Text;
using System.Text.Json;
namespace HealthcareManagerUI.Services.Http;
public class HttpService : IHttpService
{
private readonly string _apiKey;
public HttpService(IConfiguration configuration)
{
_apiKey = configuration.GetValue<string>("ApiKey") ?? "NoKey";
}
public async Task<T> GetAsync<T>(string uri, IDictionary<string, string> headers = null)
{
using var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, uri);
AddHeaders(request, headers);
var response = await httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(
responseContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
public async Task<T> GetByIdAsync<T>(string uri, int id, IDictionary<string, string> headers = null)
{
return await GetAsync<T>($"{uri}/{id}", headers);
}
public async Task<T> PostAsync<T>(string uri, object data, IDictionary<string, string> headers = null)
{
using var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, uri);
AddHeaders(request, headers);
request.Content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(
responseContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
public async Task<T> PutAsync<T>(string uri, int id, object data, IDictionary<string, string> headers = null)
{
using var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Put, $"{uri}/{id}");
AddHeaders(request, headers);
request.Content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(
responseContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
public async Task DeleteAsync(string uri, int id, IDictionary<string, string> headers = null)
{
using var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Delete, $"{uri}/{id}");
AddHeaders(request, headers);
var response = await httpClient.SendAsync(request);
}
private void AddHeaders(HttpRequestMessage request, IDictionary<string, string> headers)
{
// Add the API key header to every request
request.Headers.Add("ApiKey", _apiKey);
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
}
}