Files
2024-05-30 15:40:13 +03:00

139 lines
6.0 KiB
C#

using System.Net.Http.Json;
using HealthcareManagerUiWebAssem.Entities;
using HealthcareManagerUiWebAssem.Models;
using HealthcareManagerUiWebAssem.Services.RequestHttp;
namespace HealthcareManagerUiWebAssem.Services.PatientManagement;
public class PatientManagementService(IRequestHttpService requestHttpService) : IPatientManagementService
{
public async Task<Patient> GetPatientProfileAsync(Guid patientId)
{
var response = await requestHttpService.GetAsync($"/api/Patients/{patientId}");
if (!response.IsSuccessStatusCode) return null;
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<Patient>>();
return apiResponse?.Data;
}
public async Task<bool> UpdatePatientProfileAsync(Patient patient)
{
var response = await requestHttpService.PutAsync("/api/Patients", patient);
return response.IsSuccessStatusCode;
}
public async Task<bool> DeletePatientProfileAsync(Guid patientId)
{
var response = await requestHttpService.DeleteAsync($"/api/Patients/{patientId}");
return response.IsSuccessStatusCode;
}
public async Task<List<Appointment>> GetAppointmentsAsync(Guid patientId)
{
var response = await requestHttpService.GetAsync($"/api/Appointments/patient/{patientId}");
if (!response.IsSuccessStatusCode) return new List<Appointment>();
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<List<Appointment>>>();
return apiResponse?.Data ?? [];
}
public async Task<ApiResponse<object>> BookAppointmentAsync(NewAppointmentModel newAppointment)
{
var response = await requestHttpService.PostAsync("/api/Appointments", newAppointment);
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<object>>();
return apiResponse;
}
public async Task<bool> CancelAppointmentAsync(Guid doctorId, Guid patientId, DateTime appointmentDate)
{
var cancellationRequest = new AppointmentCancellationRequest
{
DoctorId = doctorId,
PatientId = patientId,
Appointment = appointmentDate
};
var response = await requestHttpService.PutAsync("/api/Appointments", cancellationRequest);
return response.IsSuccessStatusCode;
}
// Medical history methods
public async Task<MedicalHistory?> GetMedicalHistoryAsync(Guid userId)
{
var response = await requestHttpService.GetAsync($"/api/MedicalHistory/user/{userId}");
if (!response.IsSuccessStatusCode) return null;
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<MedicalHistory>>();
return apiResponse.Data;
}
public async Task<ApiResponse<object>> UploadMedicalHistoryAsync(Guid userId, byte[] content)
{
var requestBody = new { UserId = userId, Content = content };
var response = await requestHttpService.PostAsync("/api/MedicalHistory", requestBody);
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<object>>();
return apiResponse;
}
public async Task<bool> DeleteMedicalHistoryAsync(Guid fileId)
{
var response = await requestHttpService.DeleteAsync($"/api/MedicalHistory/{fileId}");
return response.IsSuccessStatusCode;
}
public async Task<byte[]> DownloadMedicalHistoryAsync(Guid fileId)
{
var response = await requestHttpService.GetAsync($"/api/MedicalHistory/{fileId}");
if (response.IsSuccessStatusCode) return await response.Content.ReadAsByteArrayAsync();
return null;
}
public async Task<List<Doctor>> GetAllDoctorsAsync()
{
var response = await requestHttpService.GetAsync("/api/Doctors");
if (!response.IsSuccessStatusCode) return [];
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<List<Doctor>>>();
return apiResponse?.Data ?? [];
}
public async Task<bool> CheckForAccessAsync(Guid medicalRecordId, Guid doctorId)
{
var requestBody = new { MedicalRecordId = medicalRecordId, DoctorId = doctorId };
var response = await requestHttpService.PostAsync("/api/MedicalHistory/check_access", requestBody);
return response.IsSuccessStatusCode;
}
public async Task<bool> GrantAccessAsync(Guid medicalRecordId, Guid doctorId)
{
var requestBody = new { MedicalRecordId = medicalRecordId, DoctorId = doctorId };
var response = await requestHttpService.PutAsync("/api/MedicalHistory/grant_access", requestBody);
if (!response.IsSuccessStatusCode) return false;
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<object>>();
return apiResponse.StatusCode == HttpStatusCodes.OK;
}
public async Task<bool> RevokeAccessAsync(Guid medicalRecordId, Guid doctorId)
{
var requestBody = new { MedicalRecordId = medicalRecordId, DoctorId = doctorId };
var response = await requestHttpService.PutAsync("/api/MedicalHistory/revoke_access", requestBody);
if (!response.IsSuccessStatusCode) return false;
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<object>>();
return apiResponse.StatusCode == HttpStatusCodes.OK;
}
public async Task<bool> SendMessageAsync(Guid senderId, Guid receiverId, string message)
{
var requestBody = new { sender = senderId, receiver = receiverId, message };
var response = await requestHttpService.PostAsync("/api/Chat/send_message", requestBody);
return response.IsSuccessStatusCode;
}
public async Task<Chat> GetConversationAsync(Guid userId1, Guid userId2)
{
var requestBody = new { idUser1 = userId1, idUser2 = userId2 };
var response = await requestHttpService.PostAsync("/api/Chat/get_conversation", requestBody);
if (!response.IsSuccessStatusCode) return null;
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<Chat>>();
return apiResponse?.Data;
}
}