jwt v1.0
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HealthcareManager.API.Controllers;
|
||||
namespace API.Controllers;
|
||||
|
||||
[Route("api/v1/[controller]")]
|
||||
[ApiController]
|
||||
|
||||
@@ -1,13 +1,38 @@
|
||||
using Infrastructure.Services.MongoDB;
|
||||
using Application.Endpoints;
|
||||
using Application.Endpoints.Chats;
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HealthcareManager.API.Controllers;
|
||||
namespace API.Controllers;
|
||||
|
||||
public class ChatController : BaseApiController
|
||||
{
|
||||
private readonly MongoDbService _mongoDbService;
|
||||
private readonly IChatMongoDbService _chatMongoDbService;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public ChatController(MongoDbService mongoDbService)
|
||||
public ChatController(IChatMongoDbService chatMongoDbService, IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository)
|
||||
{
|
||||
_mongoDbService = mongoDbService;
|
||||
_chatMongoDbService = chatMongoDbService;
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
}
|
||||
|
||||
[HttpPost("send_message")]
|
||||
public async Task<ActionResult<BaseResponse>> SendMessage(SendMessageDto sendMessageDto)
|
||||
{
|
||||
var handler = new ChatHandler(_chatMongoDbService, _patientRepository, _doctorRepository);
|
||||
var response = await handler.HandleSendMessage(sendMessageDto).ConfigureAwait(false);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("get_conversation")]
|
||||
public async Task<ActionResult<BaseResponse>> GetConversation(GetConversationDto getConversationDto)
|
||||
{
|
||||
var handler = new ChatHandler(_chatMongoDbService, _patientRepository, _doctorRepository);
|
||||
var response = await handler.HandleGetConversation(getConversationDto).ConfigureAwait(false);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ using Application.Endpoints.Doctors.Registration;
|
||||
using Application.Endpoints.Doctors.ResetPassword;
|
||||
using Application.Services.Database;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Jwt;
|
||||
using Core.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
@@ -15,29 +17,77 @@ public class DoctorsController : ControllerBase
|
||||
{
|
||||
private readonly IDoctorRepository _database;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IJwtService _jwtService;
|
||||
|
||||
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms)
|
||||
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms,
|
||||
IJwtService jwtService)
|
||||
{
|
||||
_database = database;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
_jwtService = jwtService;
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctor)
|
||||
public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctorRegistrationDto)
|
||||
{
|
||||
var handler = new DoctorRegistrationHandler(_database, _hashingAlgorithms);
|
||||
var response = await handler.Handle(doctor).ConfigureAwait(false);
|
||||
var response = await handler.Handle(doctorRegistrationDto).ConfigureAwait(false);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<BaseResponse>> Login(DoctorLoginDto doctor)
|
||||
public async Task<ActionResult<BaseResponse>> Login(DoctorLoginDto doctorLoginDto)
|
||||
{
|
||||
var handler = new DoctorLoginHandler(_database, _hashingAlgorithms);
|
||||
var response = await handler.Handle(doctor).ConfigureAwait(false);
|
||||
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
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Application.Endpoints;
|
||||
using Application.Endpoints.MedicalHistories;
|
||||
using Application.Endpoints.MedicalHistories.FileManagement;
|
||||
using Application.Endpoints.MedicalHistories.ManageAuthorization;
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -13,19 +14,23 @@ public class MedicalHistoryController : ControllerBase
|
||||
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
|
||||
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public MedicalHistoryController(IMedicalHistoryRepository medicalHistoryRepository,
|
||||
IPatientRepository patientRepository, IMedicalHistoryMongoDbService mongoDbService)
|
||||
IPatientRepository patientRepository, IMedicalHistoryMongoDbService mongoDbService,
|
||||
IDoctorRepository doctorRepository)
|
||||
{
|
||||
_medicalHistoryRepository = medicalHistoryRepository;
|
||||
_patientRepository = patientRepository;
|
||||
_medicalHistoryMongoDbService = mongoDbService;
|
||||
_doctorRepository = doctorRepository;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<BaseResponse>> GetAsync(Guid id)
|
||||
{
|
||||
var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _medicalHistoryMongoDbService);
|
||||
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
|
||||
_medicalHistoryMongoDbService);
|
||||
var response = await handler.HandleGet(id);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -33,7 +38,8 @@ public class MedicalHistoryController : ControllerBase
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<BaseResponse>> GetAllDoctors()
|
||||
{
|
||||
var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _medicalHistoryMongoDbService);
|
||||
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
|
||||
_medicalHistoryMongoDbService);
|
||||
var response = await handler.HandleGetAll();
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -41,7 +47,8 @@ public class MedicalHistoryController : ControllerBase
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<BaseResponse>> PostAsync(MedicalHistoryCreateDto medicalHistoryCreateDto)
|
||||
{
|
||||
var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _medicalHistoryMongoDbService);
|
||||
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
|
||||
_medicalHistoryMongoDbService);
|
||||
var response = await handler.HandleCreate(medicalHistoryCreateDto);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -49,7 +56,8 @@ public class MedicalHistoryController : ControllerBase
|
||||
[HttpPut]
|
||||
public async Task<ActionResult<BaseResponse>> UpdateAsync(MedicalHistoryUpdateDto medicalHistoryUpdateDto)
|
||||
{
|
||||
var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _medicalHistoryMongoDbService);
|
||||
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
|
||||
_medicalHistoryMongoDbService);
|
||||
var response = await handler.HandleUpdate(medicalHistoryUpdateDto).ConfigureAwait(false);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
@@ -57,16 +65,30 @@ public class MedicalHistoryController : ControllerBase
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult<BaseResponse>> DeleteAsync(Guid id)
|
||||
{
|
||||
var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _medicalHistoryMongoDbService);
|
||||
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
|
||||
_medicalHistoryMongoDbService);
|
||||
var response = await handler.HandleDelete(id);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
[HttpPut("grant_access")]
|
||||
public async Task<ActionResult<BaseResponse>> GrantAccessToMedicalHistory(GrantAccessMedicalHistoryDto)
|
||||
public async Task<ActionResult<BaseResponse>> GrantAccessToMedicalHistory(
|
||||
MedicalHistoryManageAuthorizationDoctorDto infoDto)
|
||||
{
|
||||
return NotFound();
|
||||
var handler = new MedicalHistoryManageAuthorizationHandler(_medicalHistoryRepository,
|
||||
_medicalHistoryMongoDbService, _doctorRepository);
|
||||
var response = await handler.HandleGrantDoctorAccess(infoDto);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPut("revoke_access")]
|
||||
public async Task<ActionResult<BaseResponse>> RevokeAccessToMedicalHistory(
|
||||
MedicalHistoryManageAuthorizationDoctorDto infoDto)
|
||||
{
|
||||
var handler = new MedicalHistoryManageAuthorizationHandler(_medicalHistoryRepository,
|
||||
_medicalHistoryMongoDbService, _doctorRepository);
|
||||
var response = await handler.HandleRevokeDoctorAccess(infoDto);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -5,6 +5,8 @@ using Application.Endpoints.Patients.Registration;
|
||||
using Application.Endpoints.Patients.ResetPassword;
|
||||
using Application.Services.Database;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Jwt;
|
||||
using Core.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
@@ -15,11 +17,14 @@ public class PatientsController : ControllerBase
|
||||
{
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IJwtService _jwtService;
|
||||
|
||||
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
|
||||
IJwtService jwtService)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
_jwtService = jwtService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -43,8 +48,52 @@ public class PatientsController : ControllerBase
|
||||
{
|
||||
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
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user