-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db01e87
commit b5559a7
Showing
12 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class Appointment | ||
{ | ||
public int AppointmentId { get; set; } | ||
public int PatientId { get; set; } | ||
public int CaregiverId { get; set; } | ||
public DateTime ScheduledDateTime { get; set; } | ||
public string Status { get; set; } // Scheduled, Completed, Canceled | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
[Authorize] | ||
public class AppointmentController : ControllerBase | ||
{ | ||
private readonly HealthcareContext _context; | ||
|
||
public AppointmentController(HealthcareContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> ScheduleAppointment([FromBody] Appointment appointment) | ||
{ | ||
_context.Appointments.Add(appointment); | ||
await _context.SaveChangesAsync(); | ||
return Ok(appointment); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> GetAppointments() | ||
{ | ||
var appointments = await _context.Appointments.ToListAsync(); | ||
return Ok(appointments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class Caregiver | ||
{ | ||
public int CaregiverId { get; set; } | ||
public string Name { get; set; } | ||
public string Specialty { get; set; } | ||
public DateTime AvailableFrom { get; set; } | ||
public DateTime AvailableUntil { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
[Authorize(Roles = "Admin")] | ||
public class CaregiverController : ControllerBase | ||
{ | ||
private readonly HealthcareContext _context; | ||
|
||
public CaregiverController(HealthcareContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> AddCaregiver([FromBody] Caregiver caregiver) | ||
{ | ||
_context.Caregivers.Add(caregiver); | ||
await _context.SaveChangesAsync(); | ||
return Ok(caregiver); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> GetCaregivers() | ||
{ | ||
var caregivers = await _context.Caregivers.ToListAsync(); | ||
return Ok(caregivers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class HealthcareContext : DbContext | ||
{ | ||
public HealthcareContext(DbContextOptions<HealthcareContext> options) : base(options) { } | ||
|
||
public DbSet<User> Users { get; set; } | ||
public DbSet<Patient> Patients { get; set; } | ||
public DbSet<Caregiver> Caregivers { get; set; } | ||
public DbSet<Appointment> Appointments { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
public class JwtHelper | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public JwtHelper(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public string GenerateJwtToken(User user) | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, user.Email), | ||
new Claim(ClaimTypes.Role, user.Role) | ||
}; | ||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"])); | ||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); | ||
var token = new JwtSecurityToken( | ||
_configuration["Jwt:Issuer"], | ||
_configuration["Jwt:Audience"], | ||
claims, | ||
expires: DateTime.Now.AddHours(1), | ||
signingCredentials: creds); | ||
|
||
return new JwtSecurityTokenHandler().WriteToken(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class Patient | ||
{ | ||
public int PatientId { get; set; } | ||
public string Name { get; set; } | ||
public string Address { get; set; } | ||
public string MedicalRecords { get; set; } // Encrypted | ||
public DateTime DateOfBirth { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
[Authorize(Roles = "Admin")] | ||
public class PatientController : ControllerBase | ||
{ | ||
private readonly HealthcareContext _context; | ||
|
||
public PatientController(HealthcareContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> AddPatient([FromBody] Patient patient) | ||
{ | ||
_context.Patients.Add(patient); | ||
await _context.SaveChangesAsync(); | ||
return Ok(patient); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public async Task<IActionResult> UpdatePatient(int id, [FromBody] Patient patient) | ||
{ | ||
var existingPatient = await _context.Patients.FindAsync(id); | ||
if (existingPatient == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
existingPatient.Name = patient.Name; | ||
existingPatient.Address = patient.Address; | ||
existingPatient.MedicalRecords = patient.MedicalRecords; | ||
|
||
await _context.SaveChangesAsync(); | ||
return NoContent(); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> GetPatients() | ||
{ | ||
var patients = await _context.Patients.ToListAsync(); | ||
return Ok(patients); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddDbContext<HealthcareContext>(options => | ||
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | ||
|
||
services.AddIdentity<User, IdentityRole>() | ||
.AddEntityFrameworkStores<HealthcareContext>() | ||
.AddDefaultTokenProviders(); | ||
|
||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||
.AddJwtBearer(options => | ||
{ | ||
options.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateIssuer = true, | ||
ValidateAudience = true, | ||
ValidateLifetime = true, | ||
ValidIssuer = Configuration["Jwt:Issuer"], | ||
ValidAudience = Configuration["Jwt:Audience"], | ||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])) | ||
}; | ||
}); | ||
|
||
services.AddControllers(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class User | ||
{ | ||
public int UserId { get; set; } | ||
public string Email { get; set; } | ||
public string PasswordHash { get; set; } | ||
public string Role { get; set; } // Admin, Caregiver, Patient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class UserController : ControllerBase | ||
{ | ||
private readonly UserManager<User> _userManager; | ||
private readonly JwtHelper _jwtHelper; | ||
|
||
public UserController(UserManager<User> userManager, JwtHelper jwtHelper) | ||
{ | ||
_userManager = userManager; | ||
_jwtHelper = jwtHelper; | ||
} | ||
|
||
[HttpPost("register")] | ||
public async Task<IActionResult> Register([FromBody] User model) | ||
{ | ||
var user = new User | ||
{ | ||
UserName = model.Email, | ||
Email = model.Email, | ||
Role = model.Role | ||
}; | ||
|
||
var result = await _userManager.CreateAsync(user, model.PasswordHash); | ||
if (!result.Succeeded) | ||
{ | ||
return BadRequest(result.Errors); | ||
} | ||
|
||
var token = _jwtHelper.GenerateJwtToken(user); | ||
return Ok(new { token }); | ||
} | ||
|
||
[HttpPost("login")] | ||
public async Task<IActionResult> Login([FromBody] User model) | ||
{ | ||
var user = await _userManager.FindByEmailAsync(model.Email); | ||
if (user == null || !await _userManager.CheckPasswordAsync(user, model.PasswordHash)) | ||
{ | ||
return Unauthorized("Invalid credentials"); | ||
} | ||
|
||
var token = _jwtHelper.GenerateJwtToken(user); | ||
return Ok(new { token }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DefaultConnection": "Server=localhost;Database=HealthcareDB;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
}, | ||
"Jwt": { | ||
"Key": "your-secret-key", | ||
"Issuer": "http://localhost:5000", | ||
"Audience": "http://localhost:5000" | ||
} | ||
} |