public ActionResult Delete(Guid?id, TicketAttachmentDeleteViewModel formData) { if (!id.HasValue) { return(RedirectToAction(nameof(Index))); } try { string userId = User.Identity.GetUserId(); TicketAttachment foundTicketAttachment = TicketAttachmentRepository.GetTicketAttachment(formData.Id); if (foundTicketAttachment == null) { return(RedirectToAction(nameof(HomeController.UnauthorizedRequest), "Home", new { error = $"Ticket Attachment wasn't deleted (Not Found)" })); } else if (!TicketRepository.CanUserEditTicket(userId, foundTicketAttachment.TicketId)) { return(RedirectToAction(nameof(HomeController.UnauthorizedRequest), "Home", new { error = $"You don't have the appropriate permissions to add a attachment to this ticket ({foundTicketAttachment.Ticket.Title})" })); } DbContext.TicketAttachments.Remove(foundTicketAttachment); int numberOfSavedEntities = DbContext.SaveChanges(); if (numberOfSavedEntities <= 0) { return(RedirectToAction(nameof(HomeController.UnauthorizedRequest), "Home", new { error = $"Ticket Attachment wasn't deleted (Database Error)" })); } return(RedirectToAction(nameof(Index), new { ticketId = formData.TicketId })); } catch { return(RedirectToAction(nameof(Delete), new { id = formData.Id })); } }
// GET: TicketAttachment/Delete/{id} public ActionResult Delete(Guid?id) { if (!id.HasValue) { return(RedirectToAction(nameof(Index))); } string userId = User.Identity.GetUserId(); TicketAttachment foundTicketAttachment = TicketAttachmentRepository.GetTicketAttachment(id.Value); if (foundTicketAttachment == null) { return(RedirectToAction(nameof(HomeController.UnauthorizedRequest), "Home", new { error = $"Ticket Attachment wasn't deleted (Not Found)" })); } else if (!TicketRepository.CanUserEditTicket(userId, foundTicketAttachment.TicketId)) { return(RedirectToAction(nameof(HomeController.UnauthorizedRequest), "Home", new { error = $"You don't have the appropriate permissions to add a attachment to this ticket ({foundTicketAttachment.Ticket.Title})" })); } return(View(TicketAttachmentDeleteViewModel.CreateNewViewModel(foundTicketAttachment))); }