public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Ticket ticket = _db.Tickets.Find(id); if (ticket == null) { return(HttpNotFound()); } var users = _roleHelper.UsersInRole("Developer").ToList(); ViewBag.AssignedToUserId = new SelectList(users, "Id", "DisplayName", ticket.AssignedToId); var userId = User.Identity.GetUserId(); ViewBag.CanAddContent = _ticketHelper.CanAddContent(userId, ticket); ViewBag.CanEdit = _ticketHelper.CanEdit(userId, ticket); var allowableFileExtensions = WebConfigurationManager.AppSettings["AllowableFileExtensions"]; string[] allowableFileExtensionsArr = allowableFileExtensions.Split(','); foreach (var ticketAttachment in ticket.Attachments) { var fileExt = Path.GetExtension(ticketAttachment.MediaPath); if (allowableFileExtensionsArr.Contains(fileExt)) { ticketAttachment.MediaPath = $"/Uploads/file.png"; } } return(View(ticket)); }
public ActionResult Create([Bind(Include = "TicketId,CommentBody")] TicketComment comment, int id) { var ticket = _db.Tickets.Find(id); if (ticket == null) { ModelState.AddModelError("Ticket", @"Failed to find associated Ticket"); return(RedirectToAction("Index", "Tickets")); } if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var canAddContent = _ticketHelper.CanAddContent(userId, ticket); if (canAddContent) { comment.TicketId = id; comment.AuthorId = User.Identity.GetUserId(); comment.Created = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); _db.TicketComments.Add(comment); var res = _db.SaveChanges(); if (ticket.AssignedToId != null) { comment.Author = _db.Users.Find(userId); var commentAuthorName = comment.Author == null ? "UNKNOWN" : comment.Author.DisplayName; var notificationCreationDateTime = DateTime.Now; var notification = new TicketNotification { TicketId = ticket.Id, Created = notificationCreationDateTime, Subject = $"A ticket you're Assigned to '{ticket.Title}' has a new Comment!", IsRead = false, RecipientId = ticket.AssignedToId, NotificationBody = $"Ticket '{ticket.Title}' ({ticket.DisplayableId}) has a new Comment from {commentAuthorName}" }; _db.TicketNotifications.Add(notification); _db.SaveChanges(); } } } return(RedirectToAction("Dashboard", "Tickets", new { id })); }
public ActionResult Create(TicketAttachment attachment, int id, HttpPostedFileBase attachmentFile, string attachmentDescription) { if (attachmentFile == null) { // TODO: Need a message in the View telling user to pick a file first... ModelState.AddModelError("validation-summary-errors", "Please select a File First"); TempData["CustomError"] = "The item is removed from your cart"; return(RedirectToAction("Dashboard", "Tickets", new { id })); } var ticket = _db.Tickets.Find(id); var userId = User.Identity.GetUserId(); if (ticket == null) { return(RedirectToAction("Index", "Tickets")); } if (userId == null) { return(RedirectToAction("Index", "Tickets")); } if (ModelState.IsValid) { var canAddContent = _ticketHelper.CanAddContent(userId, ticket); if (canAddContent) { if (HelperMethods.IsWebFriendlyImage(attachmentFile) || HelperMethods.IsWebFriendlyFile(attachmentFile)) { attachment.TicketId = id; attachment.CreatedById = userId; attachment.CreatedDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); if (!String.IsNullOrWhiteSpace(attachmentDescription)) { int maxDescLen = 250; if (attachmentDescription.Length > maxDescLen) { attachment.Description = attachmentDescription.Substring(0, maxDescLen); } else { attachment.Description = attachmentDescription; } } // Run filename through URL Friendly method then Apply a timestamp to filename to avoid naming collisions var fileName = attachmentFile.FileName.GenerateSlug(); var massagedFileName = fileName.ApplyDateTimeStamp(); var dirPath = Server.MapPath("~/Uploads/"); if (HelperMethods.EnsureDirectoryExists(dirPath)) { var filePath = Path.Combine(dirPath, massagedFileName); attachmentFile.SaveAs(filePath); attachment.MediaPath = $"/Uploads/{massagedFileName}"; ticket.Attachments.Add(attachment); var res = _db.SaveChanges(); } if (ticket.AssignedToId != null) { attachment.CreatedBy = _db.Users.Find(attachment.CreatedById); var attachmentCreatedByName = attachment.CreatedBy.DisplayName == null ? "UNKNOWN" : attachment.CreatedBy.DisplayName; var notificationCreationDateTime = DateTime.Now; var notification = new TicketNotification { TicketId = ticket.Id, Created = notificationCreationDateTime, Subject = $"A ticket you're Assigned to '{ticket.Title}' has a new Attachment!", IsRead = false, RecipientId = ticket.AssignedToId, NotificationBody = $"Ticket '{ticket.Title}' ({ticket.DisplayableId}) has a new Attachment from {attachmentCreatedByName}" }; _db.TicketNotifications.Add(notification); _db.SaveChanges(); } } } } return(RedirectToAction("Dashboard", "Tickets", new { id })); }