From be864f564b5788b45b472ee6f890e2c7b1be6d81 Mon Sep 17 00:00:00 2001 From: lianpi3 Date: Sat, 3 Aug 2024 17:35:24 +0330 Subject: [PATCH] ticket and respond --- Controllers/responsesController.cs | 99 ++++++++++++++++++++++-------- Controllers/ticketsController.cs | 13 ++-- Models/Login.cs | 16 ----- Models/Model1.edmx | 15 +++-- Models/ticket.cs | 1 + Models/ticketlist.cs | 18 ++++++ Views/responses/Index.cshtml | 51 +++++++++------ Views/responses/Respond.cshtml | 23 ++++++- Views/responses/test.cshtml | 45 ++++++++++++++ Views/tickets/Edit.cshtml | 67 ++++++++++---------- WebApplication1.csproj | 3 +- 11 files changed, 246 insertions(+), 105 deletions(-) delete mode 100644 Models/Login.cs create mode 100644 Models/ticketlist.cs create mode 100644 Views/responses/test.cshtml diff --git a/Controllers/responsesController.cs b/Controllers/responsesController.cs index 665109d..fd746c1 100644 --- a/Controllers/responsesController.cs +++ b/Controllers/responsesController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data; using System.Data.Entity; +using System.Data.Entity.Validation; using System.Linq; using System.Net; using System.Web; @@ -17,30 +18,38 @@ namespace WebApplication1.Controllers // GET: responses public ActionResult Index() { - if (Convert.ToInt32(Session["Userrole"]) > 0) - { - return View(db.tickets.OrderByDescending(p => p.priority).Where(s=> s.status == 1).ToList()); - } - else - { - @ViewBag.role = "user"; - } - if (Session["User"] != null) { - if (Session["Userid"] == null) + if (Convert.ToInt32(Session["Userrole"]) > 0) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + var models = (from user in db.users // Access Users DbSet + join tickets in db.tickets on user.ID equals tickets.senderid into ticketsgroup // Join News DbSet + from tickets in ticketsgroup.DefaultIfEmpty() // Left outer join + where tickets != null // Filter based on existence of news record + select new ticketlist + { + username = user != null ? user.usename : null, + ticketID = tickets.ID, // Use null-conditional operator for missing news + ticketTitle = tickets.title, + priority = tickets.priority, + status = tickets.status + }).OrderByDescending(p => p.priority).Where(s => s.status == 1 || s.status ==3).ToList(); + + + return View(models); } - user user = db.users.Find(Session["Userid"]); - if (user == null) + else { - return HttpNotFound(); + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); } - return View(user); } else - return RedirectToAction("Login","users"); + return RedirectToAction("Login", "users"); + + } + public ActionResult test() + { + return View(db.responses.ToList()); } @@ -74,7 +83,8 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { - + response.adminid = Convert.ToInt32(Session["Userid"]); + var targetTicket = db.tickets.Find(response.ticketid); db.responses.Add(response); db.SaveChanges(); return RedirectToAction("Index"); @@ -91,7 +101,8 @@ namespace WebApplication1.Controllers return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ViewBag.targetTicket = db.tickets.Find(id); - ViewBag.sender = db.users.Find(db.tickets.Find(id).senderid); + var u = db.users.Find(db.tickets.Find(id).senderid); + ViewBag.sender = u.usename; if (ViewBag.targetTicket == null) { @@ -109,18 +120,58 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { + response.adminid = Convert.ToInt32(Session["Userid"]); - //wtf - db.responses.Add(response); - db.SaveChanges(); + var targetTicket = db.tickets.Find(response.ticketid); - var targetTicket= db.tickets.Find(response.ticketid); - targetTicket.status = 0; - db.SaveChanges(); + if (response.ID == 0) + { + db.responses.Add(response); + targetTicket.status = 2; + targetTicket.responsid = response.ID; + try + { + db.SaveChanges(); + } + catch (DbEntityValidationException ex) + { + foreach (var validationErrors in ex.EntityValidationErrors) + { + foreach (var validationError in validationErrors.ValidationErrors) + { + Console.WriteLine($"Property: {validationError.PropertyName} Error: {validationError.ErrorMessage}"); + } + } + } + } + else + { + var res = db.responses.Find(response.ID); + res.content += "\n" + response.content; + targetTicket.status = 2; + try + { + db.SaveChanges(); + } + catch (DbEntityValidationException ex) + { + foreach (var validationErrors in ex.EntityValidationErrors) + { + foreach (var validationError in validationErrors.ValidationErrors) + { + Console.WriteLine($"Property: {validationError.PropertyName} Error: {validationError.ErrorMessage}"); + } + } + } + } + + + return RedirectToAction("Index"); } return View(response); } + // GET: responses/Delete/5 public ActionResult Delete(int? id) diff --git a/Controllers/ticketsController.cs b/Controllers/ticketsController.cs index 91ac69a..77b1aff 100644 --- a/Controllers/ticketsController.cs +++ b/Controllers/ticketsController.cs @@ -23,7 +23,7 @@ namespace WebApplication1.Controllers return View(db.tickets.Where(n => n.senderid == uid)); } else - return RedirectToAction("Login"); + return RedirectToAction("Login", "users"); } // GET: tickets/Details/5 @@ -58,10 +58,11 @@ namespace WebApplication1.Controllers // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public ActionResult Create([Bind(Include = "ID,title,content,priority,senderid,status")] ticket ticket) + public ActionResult Create([Bind(Include = "ID,title,content,priority,senderid,status,responsid")] ticket ticket) { if (ModelState.IsValid) { + ticket.responsid = 0; ticket.senderid = Convert.ToInt32(Session["Userid"]); ticket.status = 1; @@ -81,6 +82,8 @@ namespace WebApplication1.Controllers return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ticket ticket = db.tickets.Find(id); + ViewBag.response = db.responses.Find(ticket.responsid); + if (ticket == null) { return HttpNotFound(); @@ -93,11 +96,13 @@ namespace WebApplication1.Controllers // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public ActionResult Edit([Bind(Include = "ID,title,content,priority,senderid,status")] ticket ticket) + public ActionResult Edit([Bind(Include = "ID,title,content,priority,senderid,status,responsid")] ticket ticket) { if (ModelState.IsValid) { - db.Entry(ticket).State = EntityState.Modified; + var currentTicket = db.tickets.Find(ticket.ID); + currentTicket.content += "\n" + ticket.content; + currentTicket.status = 3; db.SaveChanges(); return RedirectToAction("Index"); } diff --git a/Models/Login.cs b/Models/Login.cs deleted file mode 100644 index 325f3da..0000000 --- a/Models/Login.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Web; - -namespace WebApplication1.Models -{ - public class Login - { - [Key] - public int ID { get; set; } - public string username { get; set; } - public string password { get; set; } - } -} \ No newline at end of file diff --git a/Models/Model1.edmx b/Models/Model1.edmx index 2aaf1b2..f0437e9 100644 --- a/Models/Model1.edmx +++ b/Models/Model1.edmx @@ -34,8 +34,8 @@ - - + + @@ -52,11 +52,12 @@ - + + @@ -125,11 +126,12 @@ - + + @@ -146,8 +148,8 @@ - - + + @@ -195,6 +197,7 @@ + diff --git a/Models/ticket.cs b/Models/ticket.cs index 6183030..31e9c9f 100644 --- a/Models/ticket.cs +++ b/Models/ticket.cs @@ -20,5 +20,6 @@ namespace WebApplication1.Models public int priority { get; set; } public int senderid { get; set; } public Nullable status { get; set; } + public Nullable responsid { get; set; } } } diff --git a/Models/ticketlist.cs b/Models/ticketlist.cs new file mode 100644 index 0000000..7dcc538 --- /dev/null +++ b/Models/ticketlist.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web; + +namespace WebApplication1.Models +{ + public class ticketlist + { + public int ticketID { get; set; } + public int userID { get; set; } + public string username { get; set; } + public string ticketTitle { get; set; } + public int priority { get; set; } + public int? status { get; set; } + } +} \ No newline at end of file diff --git a/Views/responses/Index.cshtml b/Views/responses/Index.cshtml index f1e1931..169a613 100644 --- a/Views/responses/Index.cshtml +++ b/Views/responses/Index.cshtml @@ -1,7 +1,12 @@ -@model IEnumerable +@using WebApplication1.Models +@model IEnumerable + @{ ViewBag.Title = "Index"; + + string status = ""; + }

Index

@@ -12,28 +17,38 @@ -@foreach (var item in Model) { - - - - - -} + @foreach (var item in Model) + { + + + + + + + }
- @Html.DisplayNameFor(model => model.title) + Title - @Html.DisplayNameFor(model => model.content) + Sender username
- @Html.DisplayFor(modelItem => item.title) - - @Html.DisplayFor(modelItem => item.content) - - @Html.ActionLink("Respond", "Respond", new { id=item.ID }) | - @Html.ActionLink("Details", "Details", new { id=item.ID }) | - @Html.ActionLink("Delete", "Delete", new { id=item.ID }) -
+ @Html.DisplayFor(modelItem => item.ticketTitle) + + @Html.DisplayFor(modelItem => item.username) + + @if (item.status == 1) + { + status = "awaiting response"; + } + else if (item.status == 3) + { + status = "open"; + } + @status + + @Html.ActionLink("Respond", "Respond", new { id = item.ticketID }) +
diff --git a/Views/responses/Respond.cshtml b/Views/responses/Respond.cshtml index 773269a..dcd6f88 100644 --- a/Views/responses/Respond.cshtml +++ b/Views/responses/Respond.cshtml @@ -15,6 +15,16 @@ { priority = "Normal"; } + string status = ""; + if (ViewBag.targetTicket.status == 1) + { + status = "awaiting response"; + } + else if (ViewBag.targetTicket.status == 3) + { + status = "open"; + } + }

Edit

@@ -49,11 +59,17 @@ +
+ Status +
+ +
+
Content
- +

@@ -71,7 +87,7 @@
@Html.LabelFor(model => model.content, htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.TextAreaFor(model => model.content, new { @class = "form-control" } ) + @Html.TextAreaFor(model => model.content, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.content, "", new { @class = "text-danger" })
@@ -80,7 +96,8 @@
- + +
diff --git a/Views/responses/test.cshtml b/Views/responses/test.cshtml new file mode 100644 index 0000000..9b2432d --- /dev/null +++ b/Views/responses/test.cshtml @@ -0,0 +1,45 @@ +@model IEnumerable + +

+ @Html.ActionLink("Create New", "Create") +

+ + + + + + + + + +@foreach (var item in Model) { + + + + + + + +} + +
+ @Html.DisplayNameFor(model => model.title) + + @Html.DisplayNameFor(model => model.content) + + @Html.DisplayNameFor(model => model.adminid) + + @Html.DisplayNameFor(model => model.ticketid) +
+ @Html.DisplayFor(modelItem => item.title) + + @Html.DisplayFor(modelItem => item.content) + + @Html.DisplayFor(modelItem => item.adminid) + + @Html.DisplayFor(modelItem => item.ticketid) + + @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | + @Html.ActionLink("Details", "Details", new { id=item.ID }) | + @Html.ActionLink("Delete", "Delete", new { id=item.ID }) +
diff --git a/Views/tickets/Edit.cshtml b/Views/tickets/Edit.cshtml index 920973e..b1dfc8e 100644 --- a/Views/tickets/Edit.cshtml +++ b/Views/tickets/Edit.cshtml @@ -2,6 +2,7 @@ @{ ViewBag.Title = "Edit"; + }

Edit

@@ -10,50 +11,50 @@ { @Html.AntiForgeryToken() -
-

ticket

-
- @Html.ValidationSummary(true, "", new { @class = "text-danger" }) - @Html.HiddenFor(model => model.ID) - +
+

ticket

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + @Html.HiddenFor(model => model.ID) +
- @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) + Title
- @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) +
- @Html.LabelFor(model => model.content, htmlAttributes: new { @class = "control-label col-md-2" }) + Content
- @Html.EditorFor(model => model.content, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.content, "", new { @class = "text-danger" }) +
- -
- @Html.LabelFor(model => model.priority, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.priority, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.priority, "", new { @class = "text-danger" }) -
-
- -
- @Html.LabelFor(model => model.senderid, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.senderid, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.senderid, "", new { @class = "text-danger" }) -
-
- -
-
- -
+
+
+
+ @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
+ +
+ @Html.LabelFor(model => model.content, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.TextAreaFor(model => model.content, new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.content, "", new { @class = "text-danger" }) +
+
+ + +
+
+ +
+
+
}
diff --git a/WebApplication1.csproj b/WebApplication1.csproj index 115f1b0..02774ab 100644 --- a/WebApplication1.csproj +++ b/WebApplication1.csproj @@ -154,7 +154,7 @@ Model1.tt - + @@ -291,6 +291,7 @@ +