Approve appointment

This commit is contained in:
Astrian Zheng 2023-10-18 15:43:39 +11:00
parent 12530059ee
commit 80f8026d09
2 changed files with 56 additions and 1 deletions

View File

@ -117,11 +117,13 @@ namespace FIT5032_Assignment.Controllers {
if (userProfile.role == 1) { // patient
var dbData = db.Appointments.Where(a => a.patient == userProfile.uuid).OrderByDescending(a => a.createdAt).ToList();
foreach (var item in dbData) {
Trace.WriteLine(item.uuid);
appointments.Add(new Tuple<Appointments, Users>(item, db.Users.Find(item.responsibleBy)));
}
} else if (userProfile.role == 2) { // doctor
var dbData = db.Appointments.Where(a => a.responsibleBy == userProfile.uuid).OrderByDescending(a => a.createdAt).ToList();
foreach (var item in dbData) {
Trace.WriteLine(item.uuid);
appointments.Add(new Tuple<Appointments, Users>(item, db.Users.Find(item.patient)));
}
}
@ -135,6 +137,59 @@ namespace FIT5032_Assignment.Controllers {
return View();
}
public ActionResult Approve(string id) {
// Check login
if (Request.Cookies["psg_auth_token"] == null) {
// Redirect to home page
return RedirectToAction("Index");
}
var user = psgCredentialVerify(Request.Cookies["psg_auth_token"].Value);
if (user == null) {
// Redirect to home page
Response.Cookies["psg_auth_token"].Expires = DateTime.Now.AddDays(-1);
return RedirectToAction("Index");
}
var userProfile = loginInfo(user);
if (userProfile == null) {
// Redirect to home page, and remove cookies
Response.Cookies["psg_auth_token"].Expires = DateTime.Now.AddDays(-1);
return RedirectToAction("Index");
}
// Detect user role
ViewBag.role = db.Users.Find(userProfile.uuid).role;
// Only doctor can approve appointment
if (userProfile.role != 2) {
TempData["tip"] = "This operation is not allowed.";
return Redirect("/Appointments/Index");
}
// Check if the appointment is belong to the doctor or patient
var appointment = db.Appointments.Find(id);
if (appointment == null) {
TempData["tip"] = "The appointment does not exist.";
return Redirect("/Appointments/Index");
}
if (appointment.responsibleBy != userProfile.uuid) {
TempData["tip"] = "The appointment does not exist.";
return Redirect("/Appointments/Index");
}
// Check status == 0
if (appointment.status != 0) {
TempData["tip"] = "Operation invalid";
return Redirect("/Appointments/Index");
}
// Update status
appointment.status = 1;
db.Entry(appointment).State = EntityState.Modified;
db.SaveChanges();
TempData["tip"] = "The appointment has been approved.";
return Redirect("/Appointments/Index");
}
// GET: Appointments/Create
public ActionResult Create(string id) {
if (Request.Cookies["psg_auth_token"] == null) {

View File

@ -85,7 +85,7 @@
</td>
<td>
@if (item.Item1.status == 0) {
<button class="btn btn-sm btn-primary">Approve</button>
<a href='./Approve/@item.Item1.uuid' class="btn btn-sm btn-primary">Approve</a>
<button class="btn btn-sm btn-danger">Cancel</button>
}
</td>