Skip to content

Commit

Permalink
Edited AppointmentController.cs. Updated models: appointments. Modife…
Browse files Browse the repository at this point in the history
…d all the Views/Appointment. Working check appintment for both Manager and Tenant sessions.
  • Loading branch information
tuanh00 committed Nov 26, 2024
1 parent 3bdde07 commit b47be5b
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 194 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/prjRentalManagement/v17/.suo
Binary file not shown.
182 changes: 152 additions & 30 deletions prjRentalManagement/Controllers/AppointmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,30 @@ public class AppointmentController : Controller
{
private DbPropertyRentalEntities db = new DbPropertyRentalEntities();

// GET: Appointment
// GET: Appointment -> both Managers and Tenants should only see their assigned appointments.
public ActionResult Index()
{
var appointments = db.appointments.Include(a => a.manager).Include(a => a.tenant);
return View(appointments.ToList());
if (Session["manager"] != null)
{
int managerId = int.Parse(Session["manager"].ToString());
var appointments = db.appointments
.Include(a => a.manager)
.Include(a => a.tenant)
.Where(a => a.managerId == managerId);
return View(appointments.ToList());
}

if (Session["tenant"] != null)
{
int tenantId = int.Parse(Session["tenant"].ToString());
var appointments = db.appointments
.Include(a => a.manager)
.Include(a => a.tenant)
.Where(a => a.tenantId == tenantId);
return View(appointments.ToList());
}

return RedirectToAction("Index", "Home");
}

// GET: Appointment/Details/5
Expand All @@ -36,16 +55,22 @@ public ActionResult Details(int? id)
return View(appointment);
}

// GET: Appointment/Create -> for Tenants and Managers(optional)
// GET: Appointment/Create -> for Managers, display a ddl for tenants. for tenants, display a ddl for managers
public ActionResult Create()
{
if (Session["tenant"] == null && Session["manager"] == null)
if (Session["manager"] != null)
{
return RedirectToAction("Index");
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name");
return View();
}

if (Session["tenant"] != null)
{
ViewBag.managerId = new SelectList(db.managers, "managerId", "name");
return View();
}
ViewBag.managerId = new SelectList(db.managers, "managerId", "name");
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name");
return View();

return RedirectToAction("Index", "Home");
}

// POST: Appointment/Create -> for Tenants and Managers(optional)
Expand All @@ -55,40 +80,81 @@ public ActionResult Create()
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "appointmentId,managerId,tenantId,appointmentDate")] appointment appointment)
{
if (Session["tenant"] == null && Session["manager"] == null)
if (Session["manager"] != null)
{
return RedirectToAction("Index");
appointment.managerId = int.Parse(Session["manager"].ToString());
}

if (Session["tenant"] != null)
{
appointment.tenantId = int.Parse(Session["tenant"].ToString());
}

if (ModelState.IsValid)
{
db.appointments.Add(appointment);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.managerId = new SelectList(db.managers, "managerId", "name", appointment.managerId);
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name", appointment.tenantId);
if (Session["manager"] != null)
{
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name", appointment.tenantId);
}

if (Session["tenant"] != null)
{
ViewBag.managerId = new SelectList(db.managers, "managerId", "name", appointment.managerId);
}

return View(appointment);
}

// GET: Appointment/Edit/5 -> only for Manager
// GET: Appointment/Edit/5 -> only allow managers to edit their appointments and tenants to edit theirs.
public ActionResult Edit(int? id)
{
if (Session["manager"] == null)
{
return RedirectToAction("Index");
}
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
appointment appointment = db.appointments.Find(id);
if (appointment == null)

appointment appointment = null;

if (Session["manager"] != null)
{
return HttpNotFound();
int managerId = int.Parse(Session["manager"].ToString());
appointment = db.appointments
.Where(a => a.managerId == managerId && a.appointmentId == id)
.FirstOrDefault();

if (appointment == null)
{
return HttpNotFound();
}

// Populate tenants for the dropdown
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name", appointment.tenantId);
}
else if (Session["tenant"] != null)
{
int tenantId = int.Parse(Session["tenant"].ToString());
appointment = db.appointments
.Where(a => a.tenantId == tenantId && a.appointmentId == id)
.FirstOrDefault();

if (appointment == null)
{
return HttpNotFound();
}

// Populate managers for the dropdown (if needed)
ViewBag.managerId = new SelectList(db.managers, "managerId", "name", appointment.managerId);
}
ViewBag.managerId = new SelectList(db.managers, "managerId", "name", appointment.managerId);
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name", appointment.tenantId);
else
{
return RedirectToAction("Index", "Home");
}

return View(appointment);
}

Expand All @@ -99,30 +165,75 @@ public ActionResult Edit(int? id)
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "appointmentId,managerId,tenantId,appointmentDate")] appointment appointment)
{
if (Session["manager"] != null)
{
appointment.managerId = int.Parse(Session["manager"].ToString());
}

if (Session["tenant"] != null)
{
appointment.tenantId = int.Parse(Session["tenant"].ToString());
}

if (ModelState.IsValid)
{
db.Entry(appointment).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.managerId = new SelectList(db.managers, "managerId", "name", appointment.managerId);
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name", appointment.tenantId);

// Repopulate ViewBag for invalid ModelState
if (Session["manager"] != null)
{
ViewBag.tenantId = new SelectList(db.tenants, "tenantId", "name", appointment.tenantId);
}
else if (Session["tenant"] != null)
{
ViewBag.managerId = new SelectList(db.managers, "managerId", "name", appointment.managerId);
}

return View(appointment);
}

// GET: Appointment/Delete/5
// GET: Appointment/Delete/5 -> Only for Manager can delete appointments assigned to them
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
appointment appointment = db.appointments.Find(id);
if (appointment == null)

if (Session["manager"] != null)
{
return HttpNotFound();
int managerId = int.Parse(Session["manager"].ToString());
appointment appointment = db.appointments
.Where(a => a.managerId == managerId && a.appointmentId == id)
.FirstOrDefault();

if (appointment == null)
{
return HttpNotFound();
}

return View(appointment);
}
return View(appointment);

if (Session["tenant"] != null)
{
int tenantId = int.Parse(Session["tenant"].ToString());
appointment appointment = db.appointments
.Where(a => a.tenantId == tenantId && a.appointmentId == id)
.FirstOrDefault();

if (appointment == null)
{
return HttpNotFound();
}

return View(appointment);
}

return RedirectToAction("Index", "Home");
}

// POST: Appointment/Delete/5
Expand All @@ -131,6 +242,17 @@ public ActionResult Delete(int? id)
public ActionResult DeleteConfirmed(int id)
{
appointment appointment = db.appointments.Find(id);

if (Session["manager"] != null && appointment.managerId != int.Parse(Session["manager"].ToString()))
{
return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
}

if (Session["tenant"] != null && appointment.tenantId != int.Parse(Session["tenant"].ToString()))
{
return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
}

db.appointments.Remove(appointment);
db.SaveChanges();
return RedirectToAction("Index");
Expand Down
6 changes: 5 additions & 1 deletion prjRentalManagement/Models/appointment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ namespace prjRentalManagement.Models
{
using System;
using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

public partial class appointment
{
public int appointmentId { get; set; }

public int managerId { get; set; }
public int tenantId { get; set; }

[Display(Name = "Appointment Date")]
public System.DateTime appointmentDate { get; set; }

public virtual manager manager { get; set; }
Expand Down
5 changes: 3 additions & 2 deletions prjRentalManagement/Models/manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public manager()
this.messageManagers = new HashSet<messageManager>();
this.messageOwners = new HashSet<messageOwner>();
}


[Display(Name = "Manager ID")]
public int managerId { get; set; }

[Required(ErrorMessage = "Name is required.")]
Expand All @@ -32,7 +33,7 @@ public manager()

[Required(ErrorMessage = "Email is required.")]
[EmailAddress(ErrorMessage = "Invalid Email Address.")]
[Display(Name = "Email Address")]
[Display(Name = "Email")]
public string email { get; set; }

[Required(ErrorMessage = "Password is required.")]
Expand Down
5 changes: 3 additions & 2 deletions prjRentalManagement/Models/tenant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public tenant()
this.appointments = new HashSet<appointment>();
this.messageManagers = new HashSet<messageManager>();
}


[Display(Name = "Tenant ID")]
public int tenantId { get; set; }

[Display(Name = "Name")]
[Display(Name = "Tenant Name")]
public string name { get; set; }

[Display(Name = "Email")]
Expand Down
2 changes: 1 addition & 1 deletion prjRentalManagement/Views/Apartment/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@model IEnumerable<prjRentalManagement.Models.apartment>
@{
ViewBag.Title = "Apartment";
ViewBag.Title = "Apartments";
}
<h2>Apartments</h2>
@*The "Create New Apartment" link is displayed only if a property manager is logged in.*@
Expand Down
Loading

0 comments on commit b47be5b

Please sign in to comment.