public async Task<ActionResult> Create([Bind(Include = "Id,Title,Description,CreatedDate,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerUserId,AssignedToUserId")] TicketsModel ticketsModel, HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { //Check the file name to make sure it's a image var ext = Path.GetExtension(file.FileName).ToLower(); if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".gif" && ext != ".bmp" && ext != ".txt" && ext != ".doc" && ext != ".docx" && ext != ".pdf" && ext != ".rtf" && ext != ".wps") ModelState.AddModelError("image", "Invalid Image Type."); } TicketHistoriesModel histories = new TicketHistoriesModel(); TicketAttachmentsModel attachments = new TicketAttachmentsModel(); if (ModelState.IsValid) { if (file != null && file.ContentLength > 0) { var filePath = "/Uploads/"; var absPath = Server.MapPath("~" + filePath); attachments.FileUrl = filePath + file.FileName; file.SaveAs(Path.Combine(absPath, file.FileName)); attachments.CreatedDate = DateTimeOffset.Now; //attachments.Description = new at attachments.UserId = ticketsModel.OwnerUserId; db.AttachmentData.Add(attachments); } ticketsModel.CreatedDate = DateTimeOffset.Now; ticketsModel.UpdatedDate = null; if (ticketsModel.AssignedToUserId == null) { //if ticket is unassigned when it is created it is assigned to the Project Manager ticketsModel.AssignedToUserId = "43c6b827-3faf-4afa-bb24-7b937e623052"; } db.TicketsData.Add(ticketsModel); await db.SaveChangesAsync(); return RedirectToAction("Index"); } ViewBag.ProjectId = new SelectList(db.ProjectsData, "Id", "Name", ticketsModel.ProjectId); ViewBag.TicketPriorityId = new SelectList(db.TicketPrioritiesData, "Id", "Name", ticketsModel.TicketPriorityId); ViewBag.TicketStatusId = new SelectList(db.TicketStatusesData, "Id", "Name", ticketsModel.TicketStatusId); ViewBag.TicketTypeId = new SelectList(db.TicketTypesData, "Id", "Name", ticketsModel.TicketTypeId); ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName", ticketsModel.OwnerUserId); ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "DisplayName", ticketsModel.AssignedToUserId); ViewBag.Comments = new SelectList(db.Users, "Id", "Comment", ticketsModel.Comments); ViewBag.Historys = new SelectList(db.TicketHistoriesData, "Id", "Property", ticketsModel.History); return View(ticketsModel); }
public ActionResult Edit(TicketsModel ticketsModel) { if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var user = db.Users.Find(userId); var dateChanged = System.DateTimeOffset.Now; var oldTicket = db.TicketsData.AsNoTracking().FirstOrDefault(t => t.Id == ticketsModel.Id); var assignedUser = db.Users.Find(ticketsModel.AssignedToUserId); //Title Notification if (oldTicket.Title != ticketsModel.Title) { TicketHistoriesModel histTitle = new TicketHistoriesModel { TicketId = ticketsModel.Id, Property = "Title", OldValue = oldTicket.Title, NewValue = ticketsModel.Title, ChangedDate = dateChanged, UserId = userId }; db.TicketHistoriesData.Add(histTitle); } //Ticket Title Description if (oldTicket.Description != ticketsModel.Description) { TicketHistoriesModel histDescription = new TicketHistoriesModel { TicketId = ticketsModel.Id, Property = "Description", OldValue = oldTicket.Description, NewValue = ticketsModel.Description, ChangedDate = dateChanged, UserId = userId }; db.TicketHistoriesData.Add(histDescription); } //Ticket Assigned To Notification if (oldTicket.AssignedToUserId != ticketsModel.AssignedToUserId) { TicketHistoriesModel histAssignedTo = new TicketHistoriesModel { TicketId = ticketsModel.Id, Property = "Assigned To", OldValue = db.Users.Find(oldTicket.AssignedToUserId).DisplayName, NewValue = db.Users.Find(ticketsModel.AssignedToUserId).DisplayName, ChangedDate = dateChanged, UserId = userId }; db.TicketHistoriesData.Add(histAssignedTo); //Send Notification for Assigned To Change TicketNotificationsModel ticketNotification = new TicketNotificationsModel { TicketId = ticketsModel.Id, UserId = User.Identity.GetUserId(), Sender = user.Email, Recipient = assignedUser.Email, NotificationSubject = "Ticket \"" + ticketsModel.Title + "\" has been changed.", NotificationMessage = "The Ticket <b>" + ticketsModel.Title + "</b> was reassigned to " + user.DisplayName + ".<br /><br />" + "Previous Assignee: " + histAssignedTo.OldValue + "<br />" + "New Assignee: " + histAssignedTo.NewValue + ".<br /><br />" + "This change was made by " + user.DisplayName + ".", SentDate = DateTimeOffset.Now }; db.TicketNotificationsData.Add(ticketNotification); assignedUser.SendNotification(ticketNotification.NotificationSubject, ticketNotification.NotificationMessage); } //Ticket Type Notification if (oldTicket.TicketTypeId != ticketsModel.TicketTypeId) { TicketHistoriesModel histTicketType = new TicketHistoriesModel { TicketId = ticketsModel.Id, Property = "Ticket Type", OldValue = db.TicketTypesData.Find(oldTicket.TicketTypeId).Name, NewValue = db.TicketTypesData.Find(ticketsModel.TicketTypeId).Name, ChangedDate = dateChanged, UserId = userId }; db.TicketHistoriesData.Add(histTicketType); } //Ticket Priority Notification if (oldTicket.TicketPriorityId != ticketsModel.TicketPriorityId) { TicketHistoriesModel histPriority = new TicketHistoriesModel { TicketId = ticketsModel.Id, Property = "Ticket Priority", OldValue = db.TicketPrioritiesData.Find(oldTicket.TicketPriorityId).Name, NewValue = db.TicketPrioritiesData.Find(ticketsModel.TicketPriorityId).Name, ChangedDate = dateChanged, UserId = userId }; db.TicketHistoriesData.Add(histPriority); //Send Notification for Priority Change TicketNotificationsModel ticketNotification = new TicketNotificationsModel { TicketId = ticketsModel.Id, UserId = User.Identity.GetUserId(), Sender = user.Email, Recipient = assignedUser.Email, NotificationSubject = "The Priority for the Ticket titled \"" + ticketsModel.Title + "\" has been changed", NotificationMessage = "The Ticket titled <b>" + ticketsModel.Title + "</b> has had its Priority changed. <br /><br />" + "Previous Priority: " + histPriority.OldValue + "<br />" + "New Priority: " + histPriority.NewValue + ".<br /><br />" + "The change was made by " + user.DisplayName + ".", SentDate = DateTimeOffset.Now }; db.TicketNotificationsData.Add(ticketNotification); assignedUser.SendNotification(ticketNotification.NotificationSubject, ticketNotification.NotificationMessage); } //Ticket Status Notification if (oldTicket.TicketStatusId != ticketsModel.TicketStatusId) { TicketHistoriesModel histStatus = new TicketHistoriesModel { TicketId = ticketsModel.Id, Property = "Ticket Status", OldValue = db.TicketStatusesData.Find(oldTicket.TicketStatusId).Name, NewValue = db.TicketStatusesData.Find(ticketsModel.TicketStatusId).Name, ChangedDate = dateChanged, UserId = userId }; db.TicketHistoriesData.Add(histStatus); //Send Notification for Priority Change TicketNotificationsModel ticketNotification = new TicketNotificationsModel { TicketId = ticketsModel.Id, UserId = User.Identity.GetUserId(), Sender = user.Email, Recipient = assignedUser.Email, NotificationSubject = "The Status for the Ticket titled \"" + ticketsModel.Title + "\" has been changed", NotificationMessage = "The Ticket titled <b>" + ticketsModel.Title + "</b> has had its Status changed. <br /><br />" + "Previous Status: " + histStatus.OldValue + "<br />" + "New Status: " + histStatus.NewValue + ".<br /><br />" + "The change was made by " + user.DisplayName + ".", SentDate = DateTimeOffset.Now }; db.TicketNotificationsData.Add(ticketNotification); assignedUser.SendNotification(ticketNotification.NotificationSubject, ticketNotification.NotificationMessage); if ((db.Entry(ticketsModel).Property(p => p.AssignedToUserId).IsModified == true) && (assignedUser.Roles.Any(r => r.RoleId == "c8d38989-258c-484b-9606-6f733c273059"))) { assignedUser.SendNotification( "You have been assigned a Ticket", "The Ticket titled <b>" + ticketsModel.Title + "</b> has been assigned to you. <br /><br />" + "The change was made by " + user.DisplayName + "." ); } } ticketsModel.UpdatedDate = DateTimeOffset.Now; db.TicketsData.Attach(ticketsModel); db.Entry(ticketsModel).Property(p => p.AssignedToUserId).IsModified = true; db.Entry(ticketsModel).Property(p => p.Description).IsModified = true; db.Entry(ticketsModel).Property(p => p.Title).IsModified = true; db.Entry(ticketsModel).Property(p => p.TicketTypeId).IsModified = true; db.Entry(ticketsModel).Property(p => p.TicketStatusId).IsModified = true; db.Entry(ticketsModel).Property(p => p.TicketPriorityId).IsModified = true; if ((ticketsModel.TicketStatusId == 4) && (ticketsModel.TicketPriorityId != 1004)) { ticketsModel.TicketPriorityId = 1004; } db.Entry(ticketsModel).Property(p => p.UpdatedDate).IsModified = true; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName", ticketsModel.OwnerUserId); ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "DisplayName", ticketsModel.AssignedToUserId); ViewBag.ProjectId = new SelectList(db.ProjectsData, "Id", "Name", ticketsModel.ProjectId); ViewBag.TicketPriorityId = new SelectList(db.TicketPrioritiesData, "Id", "Name", ticketsModel.TicketPriorityId); ViewBag.TicketStatusId = new SelectList(db.TicketStatusesData, "Id", "Name", ticketsModel.TicketStatusId); ViewBag.TicketTypeId = new SelectList(db.TicketTypesData, "Id", "Name", ticketsModel.TicketTypeId); return View(ticketsModel); }