Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalani authored and Kalani committed Nov 29, 2024
1 parent 2d83e2a commit b1e7e65
Show file tree
Hide file tree
Showing 120 changed files with 78,657 additions and 0 deletions.
25 changes: 25 additions & 0 deletions JKLHealthcareApp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JKLHealthcareApp", "JKLHealthcareApp\JKLHealthcareApp.csproj", "{92B4811E-DDCF-4081-AEBF-4C6624469313}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{92B4811E-DDCF-4081-AEBF-4C6624469313}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92B4811E-DDCF-4081-AEBF-4C6624469313}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92B4811E-DDCF-4081-AEBF-4C6624469313}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92B4811E-DDCF-4081-AEBF-4C6624469313}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DCAA4E0C-B0AC-463E-BA25-6D944A2B5AB7}
EndGlobalSection
EndGlobal
176 changes: 176 additions & 0 deletions JKLHealthcareApp/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using JKLHealthcareApp.Data;
using JKLHealthcareApp.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace JKLHealthcareApp.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly ApplicationDbContext _context;

public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, RoleManager<IdentityRole> roleManager, ApplicationDbContext context)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
_context = context;
}




[HttpGet]
public IActionResult Register()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new User
{
UserName = model.Username,
Email = model.Email
};

// Create the user in the database
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// Ensure the selected role exists, and create it if it doesn't
if (!await _roleManager.RoleExistsAsync(model.Role))
{
await _roleManager.CreateAsync(new IdentityRole(model.Role));
}

// Assign the user to the selected role
await _userManager.AddToRoleAsync(user, model.Role);

// If the role is "Caregiver", create an entry in the Caregiver table
if (model.Role == "Caregiver")
{
var caregiver = new Caregiver
{
Name = model.Username, // Or model.Name if you have a Name field in RegisterViewModel
UserId = user.Id, // Link the caregiver to the user
IsAvailable = true // Default availability status, adjust as needed
};

_context.Caregivers.Add(caregiver);
}
// If the role is "Patient", create an entry in the Patient table
else if (model.Role == "Patient")
{
var patient = new Patient
{
Name = model.Username, // Or model.Name if you have a Name field in RegisterViewModel
UserId = user.Id // Link the patient to the user
};

_context.Patients.Add(patient);
}

await _context.SaveChangesAsync();

// Set a success message and sign in the user
TempData["SuccessMessage"] = "Registration successful! You are now logged in.";
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction("Login", "Account");
}

// Log and display specific errors if registration fails
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
Console.WriteLine($"Error Code: {error.Code} - Description: {error.Description}"); // Debugging
}

// General error message
TempData["ErrorMessage"] = "Registration failed. Please check the errors below.";
}
else
{
foreach (var modelError in ModelState.Values.SelectMany(v => v.Errors))
{
Console.WriteLine($"Model Error: {modelError.ErrorMessage}");
ModelState.AddModelError(string.Empty, modelError.ErrorMessage);
}
TempData["ErrorMessage"] = "Invalid data provided. Please correct the errors below.";
}

return View(model);
}

[HttpGet]
public IActionResult Login(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Username);
if (user != null && await _userManager.IsInRoleAsync(user, model.Role))
{
var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
// Redirect based on the role
if (model.Role == "Admin")
{
return RedirectToAction("AssignmentDashboard", "Admin");
}
else if (model.Role == "Caregiver")
{
return RedirectToAction("Index", "Caregiver");
}
else if (model.Role == "Patient")
{
return RedirectToAction("Dashboard", "Patient");
}
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
}
}
else
{
ModelState.AddModelError(string.Empty, "Invalid role or username.");
}
}

TempData["ErrorMessage"] = "Login failed. Please check your credentials.";
return View(model);
}





[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Login", "Account");
}
}
}
88 changes: 88 additions & 0 deletions JKLHealthcareApp/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JKLHealthcareApp.Data;
using JKLHealthcareApp.Models;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace JKLHealthcareApp.Controllers
{
[Authorize(Roles = "Admin")]
public class AdminController : Controller
{
private readonly ApplicationDbContext _context;

public AdminController(ApplicationDbContext context)
{
_context = context;
}

// GET: Admin/AssignmentDashboard
public async Task<IActionResult> AssignmentDashboard()
{
var patients = await _context.Patients.Include(p => p.AssignedCaregiver).ToListAsync();
ViewBag.Caregivers = await _context.Caregivers.ToListAsync(); // Load all caregivers
return View(patients);
}

// POST: Admin/AssignCaregiver
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AssignCaregiver(int patientId, int caregiverId)
{
var patient = await _context.Patients.FindAsync(patientId);
if (patient == null)
{
return NotFound("Patient not found.");
}

var caregiver = await _context.Caregivers.FindAsync(caregiverId);
if (caregiver == null || !caregiver.IsAvailable)
{
ModelState.AddModelError("", "Selected caregiver is not available.");
return RedirectToAction(nameof(AssignmentDashboard));
}

patient.CaregiverId = caregiverId;
_context.Update(patient);
await _context.SaveChangesAsync();

return RedirectToAction(nameof(AssignmentDashboard));
}

// POST: Admin/RemoveAssignment
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RemoveAssignment(int patientId)
{
var patient = await _context.Patients.FindAsync(patientId);
if (patient == null)
{
return NotFound("Patient not found.");
}

patient.CaregiverId = null;
_context.Update(patient);
await _context.SaveChangesAsync();

return RedirectToAction(nameof(AssignmentDashboard));
}

// GET: Admin/ToggleAvailability
public async Task<IActionResult> ToggleAvailability(int caregiverId)
{
var caregiver = await _context.Caregivers.FindAsync(caregiverId);
if (caregiver == null)
{
return NotFound("Caregiver not found.");
}

caregiver.IsAvailable = !caregiver.IsAvailable;
_context.Update(caregiver);
await _context.SaveChangesAsync();

return RedirectToAction(nameof(AssignmentDashboard));
}
}
}
Loading

0 comments on commit b1e7e65

Please sign in to comment.