diff --git a/JKL-HealthCare/Appointment.cs b/JKL-HealthCare/Appointment.cs new file mode 100644 index 0000000..7abc9b2 --- /dev/null +++ b/JKL-HealthCare/Appointment.cs @@ -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 +} diff --git a/JKL-HealthCare/AppointmentController.cs b/JKL-HealthCare/AppointmentController.cs new file mode 100644 index 0000000..66b320f --- /dev/null +++ b/JKL-HealthCare/AppointmentController.cs @@ -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 ScheduleAppointment([FromBody] Appointment appointment) + { + _context.Appointments.Add(appointment); + await _context.SaveChangesAsync(); + return Ok(appointment); + } + + [HttpGet] + public async Task GetAppointments() + { + var appointments = await _context.Appointments.ToListAsync(); + return Ok(appointments); + } +} diff --git a/JKL-HealthCare/Caregiver.cs b/JKL-HealthCare/Caregiver.cs new file mode 100644 index 0000000..0b4fd3a --- /dev/null +++ b/JKL-HealthCare/Caregiver.cs @@ -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; } +} diff --git a/JKL-HealthCare/CaregiverController.cs b/JKL-HealthCare/CaregiverController.cs new file mode 100644 index 0000000..7a73bc0 --- /dev/null +++ b/JKL-HealthCare/CaregiverController.cs @@ -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 AddCaregiver([FromBody] Caregiver caregiver) + { + _context.Caregivers.Add(caregiver); + await _context.SaveChangesAsync(); + return Ok(caregiver); + } + + [HttpGet] + public async Task GetCaregivers() + { + var caregivers = await _context.Caregivers.ToListAsync(); + return Ok(caregivers); + } +} diff --git a/JKL-HealthCare/HealthcareContext.cs b/JKL-HealthCare/HealthcareContext.cs new file mode 100644 index 0000000..d22a665 --- /dev/null +++ b/JKL-HealthCare/HealthcareContext.cs @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; + +public class HealthcareContext : DbContext +{ + public HealthcareContext(DbContextOptions options) : base(options) { } + + public DbSet Users { get; set; } + public DbSet Patients { get; set; } + public DbSet Caregivers { get; set; } + public DbSet Appointments { get; set; } +} diff --git a/JKL-HealthCare/JwtHelper.cs b/JKL-HealthCare/JwtHelper.cs new file mode 100644 index 0000000..1b4db8b --- /dev/null +++ b/JKL-HealthCare/JwtHelper.cs @@ -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 + { + 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); + } +} diff --git a/JKL-HealthCare/Patient.cs b/JKL-HealthCare/Patient.cs new file mode 100644 index 0000000..35caed1 --- /dev/null +++ b/JKL-HealthCare/Patient.cs @@ -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; } +} diff --git a/JKL-HealthCare/PatientController.cs b/JKL-HealthCare/PatientController.cs new file mode 100644 index 0000000..d0ca379 --- /dev/null +++ b/JKL-HealthCare/PatientController.cs @@ -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 AddPatient([FromBody] Patient patient) + { + _context.Patients.Add(patient); + await _context.SaveChangesAsync(); + return Ok(patient); + } + + [HttpPut("{id}")] + public async Task 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 GetPatients() + { + var patients = await _context.Patients.ToListAsync(); + return Ok(patients); + } +} diff --git a/JKL-HealthCare/Startup.cs b/JKL-HealthCare/Startup.cs new file mode 100644 index 0000000..4b4dbdd --- /dev/null +++ b/JKL-HealthCare/Startup.cs @@ -0,0 +1,25 @@ +public void ConfigureServices(IServiceCollection services) +{ + services.AddDbContext(options => + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + + services.AddIdentity() + .AddEntityFrameworkStores() + .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(); +} diff --git a/JKL-HealthCare/User.cs b/JKL-HealthCare/User.cs new file mode 100644 index 0000000..4ebaf15 --- /dev/null +++ b/JKL-HealthCare/User.cs @@ -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 +} diff --git a/JKL-HealthCare/UserController.cs b/JKL-HealthCare/UserController.cs new file mode 100644 index 0000000..b4a7d34 --- /dev/null +++ b/JKL-HealthCare/UserController.cs @@ -0,0 +1,46 @@ +[Route("api/[controller]")] +[ApiController] +public class UserController : ControllerBase +{ + private readonly UserManager _userManager; + private readonly JwtHelper _jwtHelper; + + public UserController(UserManager userManager, JwtHelper jwtHelper) + { + _userManager = userManager; + _jwtHelper = jwtHelper; + } + + [HttpPost("register")] + public async Task 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 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 }); + } +} diff --git a/JKL-HealthCare/appsettings.json b/JKL-HealthCare/appsettings.json new file mode 100644 index 0000000..d92c826 --- /dev/null +++ b/JKL-HealthCare/appsettings.json @@ -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" + } +}