migrare UI in WebAssembly + integrare Serviciu stocare info pe web + WebToken
This commit is contained in:
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
public class AppointmentsController : BaseApiController
|
||||
{
|
||||
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
using Application.Endpoints;
|
||||
using Application.Endpoints.Authorization;
|
||||
using Application.Endpoints.Authorization.Doctor;
|
||||
using Application.Endpoints.Authorization.Patient;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.Email;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Jwt;
|
||||
using Core.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
public class AuthorizationController : BaseApiController
|
||||
{
|
||||
private readonly IJwtService _jwtService;
|
||||
private readonly IEmailService _emailService;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public AuthorizationController(IEmailService emailService, IJwtService jwtService,
|
||||
IPatientRepository patientRepository, IDoctorRepository doctorRepository,
|
||||
IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_jwtService = jwtService;
|
||||
_emailService = emailService;
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<BaseResponse>> Login(UserLoginModel dto)
|
||||
{
|
||||
BaseResponse? response = null;
|
||||
var loginInfo = new LoginDto();
|
||||
loginInfo.Email = dto.Email;
|
||||
loginInfo.Password = dto.Password;
|
||||
|
||||
switch (dto.UserType)
|
||||
{
|
||||
case "doctor":
|
||||
var handlerDoctor = new DoctorLoginHandler(_doctorRepository, _hashingAlgorithms);
|
||||
response = await handlerDoctor.Handle(loginInfo).ConfigureAwait(false);
|
||||
|
||||
if (response.Data != null)
|
||||
{
|
||||
Doctor patient = (Doctor)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(patient.Email);
|
||||
Console.WriteLine(patient.Email);
|
||||
|
||||
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
}
|
||||
break;
|
||||
case "patient":
|
||||
var handlerPatient = new PatientLoginHandler(_patientRepository, _hashingAlgorithms);
|
||||
response = await handlerPatient.Handle(loginInfo).ConfigureAwait(false);
|
||||
|
||||
if (response.Data != null)
|
||||
{
|
||||
Patient patient = (Patient)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(patient.Email);
|
||||
|
||||
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return new ActionResult<BaseResponse>(new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Need user type.",
|
||||
Data = null
|
||||
});
|
||||
}
|
||||
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("reset_password")]
|
||||
public async Task<ActionResult<BaseResponse>> ResetPassword(UserLoginModel dto)
|
||||
{
|
||||
BaseResponse? response = null;
|
||||
var loginInfo = new LoginDto();
|
||||
loginInfo.Email = dto.Email;
|
||||
loginInfo.Password = dto.Password;
|
||||
|
||||
switch (dto.UserType)
|
||||
{
|
||||
case "doctor":
|
||||
var handlerDoctor = new DoctorResetPasswordHandler(_doctorRepository, _hashingAlgorithms);
|
||||
response = await handlerDoctor.Handle(loginInfo).ConfigureAwait(false);
|
||||
break;
|
||||
case "patient":
|
||||
var handlerPatient = new PatientResetPasswordHandler(_patientRepository, _hashingAlgorithms);
|
||||
response = await handlerPatient.Handle(loginInfo).ConfigureAwait(false);
|
||||
break;
|
||||
default:
|
||||
return new ActionResult<BaseResponse>(new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Need user type.",
|
||||
Data = null
|
||||
});
|
||||
}
|
||||
|
||||
if (response.StatusCode < HttpStatusCodes.BadRequest)
|
||||
{
|
||||
var body = _emailService.GenerateResetCredentialsEmailBody(
|
||||
loginInfo.Email, loginInfo.Password);
|
||||
await _emailService.SendEmailAsync(loginInfo.Email, "Password reset successfully!", body);
|
||||
}
|
||||
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("refresh_token")]
|
||||
public async Task<ActionResult<BaseResponse>> RefreshToken()
|
||||
{
|
||||
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
|
||||
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Invalid request header format.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
|
||||
|
||||
if (!_jwtService.ValidateJwtToken(oldToken))
|
||||
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Unauthorized,
|
||||
Message = "Invalid JWT token.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var newToken = _jwtService.RefreshToken(oldToken);
|
||||
return StatusCode(HttpStatusCodes.OK, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Token refreshed successfully.",
|
||||
Data = new { Token = newToken }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
public class ChatController : BaseApiController
|
||||
{
|
||||
private readonly IChatMongoDbService _chatMongoDbService;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
using Application.Endpoints;
|
||||
using Application.Endpoints.Doctors.Login;
|
||||
using Application.Endpoints.Authorization.Doctor;
|
||||
using Application.Endpoints.Doctors.Profile;
|
||||
using Application.Endpoints.Doctors.Registration;
|
||||
using Application.Endpoints.Doctors.ResetPassword;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.Email;
|
||||
@@ -18,17 +17,15 @@ namespace API.Controllers;
|
||||
public class DoctorsController : ControllerBase
|
||||
{
|
||||
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
|
||||
private readonly IDoctorRepository _database;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IJwtService _jwtService;
|
||||
private readonly IEmailService _emailService;
|
||||
|
||||
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms,
|
||||
IJwtService jwtService, IAppointmentsMongoDbService appointmentsMongoDbService, IEmailService emailService)
|
||||
public DoctorsController(IDoctorRepository doctorRepository, IHashingAlgorithms hashingAlgorithms,
|
||||
IAppointmentsMongoDbService appointmentsMongoDbService, IEmailService emailService)
|
||||
{
|
||||
_database = database;
|
||||
_doctorRepository = doctorRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
_jwtService = jwtService;
|
||||
_appointmentsMongoDbService = appointmentsMongoDbService;
|
||||
_emailService = emailService;
|
||||
}
|
||||
@@ -36,7 +33,7 @@ public class DoctorsController : ControllerBase
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctorRegistrationDto)
|
||||
{
|
||||
var handler = new DoctorRegistrationHandler(_database, _hashingAlgorithms);
|
||||
var handler = new DoctorRegistrationHandler(_doctorRepository, _hashingAlgorithms);
|
||||
var response = await handler.Handle(doctorRegistrationDto).ConfigureAwait(false);
|
||||
|
||||
if (response.StatusCode < HttpStatusCodes.BadRequest)
|
||||
@@ -49,67 +46,10 @@ public class DoctorsController : ControllerBase
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<BaseResponse>> Login(DoctorLoginDto doctorLoginDto)
|
||||
{
|
||||
var handler = new DoctorLoginHandler(_database, _hashingAlgorithms);
|
||||
var response = await handler.Handle(doctorLoginDto).ConfigureAwait(false);
|
||||
/*
|
||||
if (response.Data != null)
|
||||
{
|
||||
Doctor doctor = (Doctor)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(doctor.Email);
|
||||
|
||||
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
}
|
||||
*/
|
||||
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("refresh_token")]
|
||||
public async Task<ActionResult<BaseResponse>> RefreshToken()
|
||||
{
|
||||
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
|
||||
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Invalid request header format.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
|
||||
|
||||
if (!_jwtService.ValidateJwtToken(oldToken))
|
||||
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Unauthorized,
|
||||
Message = "Invalid JWT token.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var newToken = _jwtService.RefreshToken(oldToken);
|
||||
return StatusCode(HttpStatusCodes.OK, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Token refreshed successfully.",
|
||||
Data = new { Token = newToken }
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("reset_password")]
|
||||
public async Task<ActionResult<BaseResponse>> ResetPassword(DoctorResetPasswordDto resetDoctorDto)
|
||||
{
|
||||
var handler = new DoctorResetPasswordHandler(_database, _hashingAlgorithms);
|
||||
var response = await handler.Handle(resetDoctorDto).ConfigureAwait(false);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<BaseResponse>> GetAllDoctors()
|
||||
{
|
||||
var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
|
||||
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
|
||||
var response = await handler.HandleGetAll();
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -117,7 +57,7 @@ public class DoctorsController : ControllerBase
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<BaseResponse>> GetDoctor(Guid id)
|
||||
{
|
||||
var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
|
||||
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
|
||||
var response = await handler.HandleGet(id);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -125,7 +65,7 @@ public class DoctorsController : ControllerBase
|
||||
[HttpPut]
|
||||
public async Task<ActionResult<BaseResponse>> UpdateDoctorProfile(DoctorProfileUpdateDto doctorUpdateDto)
|
||||
{
|
||||
var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
|
||||
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
|
||||
var response = await handler.HandleUpdate(doctorUpdateDto).ConfigureAwait(false);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -133,7 +73,7 @@ public class DoctorsController : ControllerBase
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult<BaseResponse>> DeleteDoctorProfile(Guid id)
|
||||
{
|
||||
var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
|
||||
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
|
||||
var response = await handler.HandleDelete(id).ConfigureAwait(false);
|
||||
|
||||
if (response.StatusCode < HttpStatusCodes.BadRequest) DeleteDoctorAppointments(id);
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
using Application.Endpoints;
|
||||
using Application.Endpoints.Patients.Login;
|
||||
using Application.Endpoints.Patients.Profile;
|
||||
using Application.Endpoints.Patients.Registration;
|
||||
using Application.Endpoints.Patients.ResetPassword;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Email;
|
||||
@@ -17,17 +15,15 @@ namespace API.Controllers;
|
||||
public class PatientsController : ControllerBase
|
||||
{
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IJwtService _jwtService;
|
||||
private readonly IMedicalHistoryRepository _medicalHistory;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IEmailService _emailService;
|
||||
|
||||
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
|
||||
IJwtService jwtService, IMedicalHistoryRepository medicalHistory, IEmailService emailService)
|
||||
IMedicalHistoryRepository medicalHistory, IEmailService emailService)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
_jwtService = jwtService;
|
||||
_medicalHistory = medicalHistory;
|
||||
_emailService = emailService;
|
||||
}
|
||||
@@ -40,65 +36,10 @@ public class PatientsController : ControllerBase
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<BaseResponse>> GetPatient(Guid id)
|
||||
{
|
||||
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
|
||||
var response = await handler.HandleGet(id);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<BaseResponse>> Login(PatientLoginDto patientLoginDto)
|
||||
{
|
||||
var handler = new PatientLoginHandler(_patientRepository);
|
||||
var response = await handler.Handle(patientLoginDto).ConfigureAwait(false);
|
||||
/*
|
||||
if (response.Data != null)
|
||||
{
|
||||
Patient patient = (Patient)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(patient.Email);
|
||||
|
||||
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
}
|
||||
*/
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("refresh_token")]
|
||||
public async Task<ActionResult<BaseResponse>> RefreshToken()
|
||||
{
|
||||
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
|
||||
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Invalid request header format.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
|
||||
|
||||
if (!_jwtService.ValidateJwtToken(oldToken))
|
||||
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Unauthorized,
|
||||
Message = "Invalid JWT token.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var newToken = _jwtService.RefreshToken(oldToken);
|
||||
return StatusCode(HttpStatusCodes.OK, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Token refreshed successfully.",
|
||||
Data = new { Token = newToken }
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<BaseResponse>> Register(PatientRegistrationDto patientRegistrationDto)
|
||||
{
|
||||
Console.WriteLine("Esti aici");
|
||||
var handler = new PatientRegistrationHandler(_patientRepository, _hashingAlgorithms);
|
||||
var response = await handler.Handle(patientRegistrationDto).ConfigureAwait(false);
|
||||
|
||||
@@ -111,12 +52,12 @@ public class PatientsController : ControllerBase
|
||||
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("reset_password")]
|
||||
public async Task<ActionResult<BaseResponse>> ResetPassword(PatientResetPasswordDto patientResetPasswordDto)
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<BaseResponse>> GetPatient(Guid id)
|
||||
{
|
||||
var handler = new PatientResetPasswordHandler(_patientRepository, _hashingAlgorithms);
|
||||
var response = await handler.Handle(patientResetPasswordDto).ConfigureAwait(false);
|
||||
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
|
||||
var response = await handler.HandleGet(id);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user