public ActionResult CreateTicket(int?id) { if (!id.HasValue) { return(RedirectToAction(nameof(Index))); } var userId = User.Identity.GetUserId(); //var userName = User.Identity.GetUserName(); //string userName = DbContext.Users.ToList()[0].UserName; var ticket = DbContext.TicketsDatabase.FirstOrDefault(p => p.Id == id); var ticketType = DbContext.TicketsTypeDatabase.ToList(); var ticketPriority = DbContext.TicketsPriorityDatabase.ToList(); var ticketAttachment = new CreateAttachmetsViewModel() { }; var model = new CreateTicketViewModel { TicketType = ticketType.Select(p => new SelectListItem() { Text = p.Name, Value = p.Id.ToString(), }).ToList(), TicketPriority = ticketPriority.Select(p => new SelectListItem() { Text = p.Name, Value = p.Id.ToString(), }).ToList(), ProjectId = id.Value, }; return(View(model)); }
public ActionResult Attachments(CreateAttachmetsViewModel formData) { string fileExtension; //Validating file upload if (formData.Media != null) { fileExtension = Path.GetExtension(formData.Media.FileName); if (!Constants.AllowedFileExtensions.Contains(fileExtension)) { ModelState.AddModelError("", "File extension is not allowed."); return(View()); } } return(RedirectToAction(nameof(TicketsController.Tickets))); }
private ActionResult SaveTicket(int?id, CreateTicketViewModel formData) { var userId = User.Identity.GetUserId(); var ticketPriority = DbContext.TicketsPriorityDatabase.ToList(); var tickets = DbContext.TicketsDatabase.FirstOrDefault(p => p.Id == id); if (!ModelState.IsValid) { //var userName = User.Identity.GetUserName(); //string userName = DbContext.Users.ToList()[0].UserName; var ticketType = DbContext.TicketsTypeDatabase.ToList(); var ticketAttachment = new CreateAttachmetsViewModel() { }; var model = new CreateTicketViewModel { TicketType = ticketType.Select(p => new SelectListItem() { Text = p.Name, Value = p.Id.ToString(), Selected = (tickets?.TicketTypeId ?? -1) == p.Id, }).ToList(), TicketPriority = ticketPriority.Select(p => new SelectListItem() { Text = p.Name, Value = p.Id.ToString(), Selected = (tickets?.TicketPriorityId ?? -1) == p.Id, }).ToList(), ProjectId = id.HasValue ? id.Value : formData.ProjectId, }; return(View(model)); } string fileExtension; //Validating file upload if (formData.Media != null) { fileExtension = Path.GetExtension(formData.Media.FileName); if (!Constants.AllowedFileExtensions.Contains(fileExtension.ToLower())) { ModelState.AddModelError("", "File extension is not allowed."); return(RedirectToAction(nameof(AllTickets))); } } Ticket ticket; //var userId = User.Identity.GetUserId(); if (!id.HasValue) { ticket = new Ticket(); var applicationUser = DbContext.Users.FirstOrDefault(user => user.Id == userId); if (applicationUser == null) { return(RedirectToAction(nameof(HomeController.Index))); } ticket.CreatedBy = applicationUser; ticket.ProjectId = formData.ProjectId; ticket.TicketStatusId = DbContext.TicketsStatusDatabase.First(p => p.Name == "Open").Id; var userMe = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); userMe.SendEmail(userId, "Notification", "There is a new Ticket Created for project You Belong To"); DbContext.TicketsDatabase.Add(ticket); } else { ticket = DbContext.TicketsDatabase.FirstOrDefault(p => p.Id == id); ticket.DateUpdated = DateTime.Now; var status = DbContext.TicketsStatusDatabase.FirstOrDefault(p => p.Id == formData.StatusId); ticket.TicketStatusId = status?.Id ?? DbContext.TicketsStatusDatabase.First(p => p.Name == "Open").Id; if (ticket == null) { return(RedirectToAction(nameof(HomeController.Index))); } } if (formData.Media != null) { if (!Directory.Exists(Constants.MappedUploadFolder)) { Directory.CreateDirectory(Constants.MappedUploadFolder); } var fileName = formData.Media.FileName; var fullPathWithName = Constants.MappedUploadFolder + fileName; formData.Media.SaveAs(fullPathWithName); TicketAttachments attachment; if (true) { attachment = new TicketAttachments() { MediaUrl = Constants.UploadFolder + fileName, TicketId = ticket.Id, UserId = ticket.CreatedById, }; var userMa = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); userMa.SendEmail(userId, "Notification", "There is a new Attachment for Ticket You Belong To"); ticket.Attachments.Add(attachment); DbContext.TicketsAttachmentsDatabase.Add(attachment); } } //var t = DbContext.TicketsPriorityDatabase.FirstOrDefault(p => p.Name.ToList(); == formData.TicketPriority); var tp = DbContext.TicketsPriorityDatabase.FirstOrDefault(p => p.Id == formData.PriorityId); var type = DbContext.TicketsTypeDatabase.FirstOrDefault(p => p.Id == formData.TypeId); ticket.Title = formData.Title; ticket.Description = formData.Description; ticket.TicketPriorityId = tp.Id; ticket.TicketPriorityId = formData.PriorityId; ticket.TicketTypeId = formData.TypeId; ticket.TicketTypeId = type.Id; var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); if (id.HasValue) { TicketHistory(ticket); } foreach (var user in ticket.SendNotification) { var usermanager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); userManager.SendEmail(userId, "Notification", "There Are some changes in a Ticket"); } var userM = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); userM.SendEmail(userId, "Notification", "There is achange in a Ticket You Belong To"); DbContext.SaveChanges(); return(RedirectToAction(nameof(TicketsController.Tickets))); }