Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kokkintibi committed Nov 29, 2024
1 parent db01e87 commit b5559a7
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 0 deletions.
8 changes: 8 additions & 0 deletions JKL-HealthCare/Appointment.cs
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
}
27 changes: 27 additions & 0 deletions JKL-HealthCare/AppointmentController.cs
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);
}
}
8 changes: 8 additions & 0 deletions JKL-HealthCare/Caregiver.cs
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; }
}
27 changes: 27 additions & 0 deletions JKL-HealthCare/CaregiverController.cs
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);
}
}
11 changes: 11 additions & 0 deletions JKL-HealthCare/HealthcareContext.cs
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; }
}
29 changes: 29 additions & 0 deletions JKL-HealthCare/JwtHelper.cs
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);
}
}
8 changes: 8 additions & 0 deletions JKL-HealthCare/Patient.cs
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; }
}
44 changes: 44 additions & 0 deletions JKL-HealthCare/PatientController.cs
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);
}
}
25 changes: 25 additions & 0 deletions JKL-HealthCare/Startup.cs
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();
}
7 changes: 7 additions & 0 deletions JKL-HealthCare/User.cs
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
}
46 changes: 46 additions & 0 deletions JKL-HealthCare/UserController.cs
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 });
}
}
10 changes: 10 additions & 0 deletions JKL-HealthCare/appsettings.json
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"
}
}

0 comments on commit b5559a7

Please sign in to comment.