diff --git a/FIT5032-Assignment/Controllers/AppointmentsController.cs b/FIT5032-Assignment/Controllers/AppointmentsController.cs index c1635c4..2dc401d 100644 --- a/FIT5032-Assignment/Controllers/AppointmentsController.cs +++ b/FIT5032-Assignment/Controllers/AppointmentsController.cs @@ -633,5 +633,90 @@ namespace FIT5032_Assignment.Controllers { return View(); } + + [HttpPost] + public ActionResult Review(FormCollection collection) { + Trace.WriteLine(collection["appointment"]); + + // Verify 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", "Home"); + } + + // Detect if user logined is patient + var role = db.Users.Find(userProfile.uuid).role; + if (role != 1) { + // Redirect to home page + TempData["tip"] = "Operation invalid"; + return Redirect("/Appointments/Index"); + } + + // Check if the appointment is belong to the doctor or patient + var appointment = db.Appointments.Find(collection["appointment"]); + if (appointment == null) { + TempData["tip"] = "The appointment does not exist."; + return Redirect("/Appointments/Index"); + } + if (appointment.patient != userProfile.uuid && appointment.responsibleBy != userProfile.uuid) { + TempData["tip"] = "The appointment does not exist."; + return Redirect("/Appointments/Index"); + } + + // Check status == 2 + if (appointment.status != 2) { + TempData["tip"] = "Operation invalid"; + return Redirect("/Appointments/Index"); + } + + // Check if the review exist + try { + var review = db.Reviews.Find(collection["appointment"]); + Trace.WriteLine(review); + if (review != null) { + TempData["tip"] = "You cannot append a new review to this appointment."; + return Redirect("/Appointments/Index"); + } + } catch(Exception e) { + Trace.WriteLine(e); + TempData["tip"] = "System error"; + return Redirect("/Appointments/Index"); + } + + // Create review + var uuid = Guid.NewGuid().ToString(); + Reviews newReview = new Reviews { + appointment = collection["appointment"], + patient = userProfile.uuid, + doctor = appointment.responsibleBy, + score = Convert.ToInt32(collection["score"]), + comment = collection["comment"], + reviewAt = DateTime.Now, + }; + db.Reviews.Add(newReview); + db.SaveChanges(); + + ViewBag.role = role; + ViewBag.appointment = appointment; + ViewBag.doctor = db.Doctors.Where(d => d.user == appointment.responsibleBy); + ViewBag.doctorUser = db.Users.Find(appointment.responsibleBy); + ViewBag.patient = db.Users.Find(appointment.patient); + ViewBag.review = newReview; + ViewBag.reviewAvailable = true; + ViewBag.tip = "Thanks for submit your review!"; + return View(); + } } } diff --git a/FIT5032-Assignment/Views/Appointments/Review.cshtml b/FIT5032-Assignment/Views/Appointments/Review.cshtml index b1595dc..e49a7bb 100644 --- a/FIT5032-Assignment/Views/Appointments/Review.cshtml +++ b/FIT5032-Assignment/Views/Appointments/Review.cshtml @@ -4,11 +4,18 @@ Layout = "~/Views/Shared/_Layout.cshtml"; } +@if (ViewBag.tip != null) { +
You are about to review the Doctor @ViewBag.doctorUser.displayName