Exemple #1
0
        public async System.Threading.Tasks.Task <ActionResult> Create([Bind(Include = "Id,FileBase,FileDescription")] List <TicketAttachmentViewModel> ticketAttachments, int TicketId, int ProjectId)
        {
            if (ModelState.IsValid)
            {
                var dateTimeNow = DateTime.Now;
                var userDB      = UserHelper.GetUserById(User.Identity.GetUserId());
                var ticketDB    = db.Tickets.FirstOrDefault(t => t.Id == TicketId);

                if (User.IsInRole("Admin") ||
                    (User.IsInRole("Project Manager") && userDB.ProjectsManage.Any(p => p.Id == ProjectId)) ||
                    (User.IsInRole("Developer") && ticketDB.DeveloperId == userDB.Id) ||
                    (User.IsInRole("Submitter") && userDB.CreatedTickets.Any(t => t.Id == TicketId)))
                {
                    foreach (var attach in ticketAttachments)
                    {
                        if (attach.FileBase != null)
                        {
                            var attachmentDB = new TicketAttachment();
                            attachmentDB.AuthorId    = User.Identity.GetUserId();
                            attachmentDB.Created     = dateTimeNow;
                            attachmentDB.Description = attach.FileDescription;
                            attachmentDB.TicketId    = TicketId;
                            var hash = attach.GetHashCode();
                            attachmentDB.Name = SlugConverter.URLFriendly(Path.GetFileNameWithoutExtension(attach.FileBase.FileName)) + "-" + hash.ToString() + Path.GetExtension(attach.FileBase.FileName);
                            var content = Server.MapPath("~/uploads/tickets/");
                            var path    = Path.Combine(content, Convert.ToString(TicketId));
                            Directory.CreateDirectory(path);

                            attach.FileBase.SaveAs(Path.Combine(Server.MapPath("~/uploads/tickets/" + TicketId + "/"), attachmentDB.Name));
                            attachmentDB.FilePath = "/uploads/tickets/" + TicketId + "/" + attachmentDB.Name;
                            db.TicketAttachments.Add(attachmentDB);
                        }
                        else
                        {
                            return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                        }
                    }


                    db.SaveChanges();

                    var devDB = ticketDB.Developer;

                    if (devDB != null)
                    {
                        var newMail = new MailMessage(userDB.Email, devDB.Email);
                        newMail.Subject    = $"Ticket {ticketDB.Title} has new attachment";
                        newMail.Body       = $"<h3>This is email from {userDB.DisplayName}. <p>Ticket attach to you have new attachment.<p/>";
                        newMail.IsBodyHtml = true;

                        await PersonalEmail.SendAsync(newMail);
                    }
                    return(RedirectToAction("Details", "Tickets", new { id = TicketId }));
                }
            }

            return(RedirectToAction("Details", "Tickets", new { id = TicketId }));
        }
        public ActionResult Create([Bind(Include = "Id,ProjectId,Title,Description,FileBase,FileDescription,TicketTypeId")] Ticket ticket, List <TicketAttachmentViewModel> ticketAttachments)
        {
            var projectDB = db.Projects.Find(ticket.ProjectId);

            if (projectDB == null)
            {
                return(HttpNotFound());
            }


            var userAssignedProjectsDB = UserHelper.GetAllProjectsAssignedToUser(User.Identity.GetUserId());

            if (!userAssignedProjectsDB.Any(proj => proj.Id == ticket.ProjectId))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (ModelState.IsValid)
            {
                var dateTimeNow = DateTime.Now;
                if (ticketAttachments != null)
                {
                    foreach (var attach in ticketAttachments)
                    {
                        if (attach.FileBase != null)
                        {
                            var attachmentDB = new TicketAttachment();
                            attachmentDB.AuthorId    = User.Identity.GetUserId();
                            attachmentDB.Created     = dateTimeNow;
                            attachmentDB.Description = attach.FileDescription;
                            attachmentDB.TicketId    = ticket.Id;
                            var hash = attach.GetHashCode();
                            attachmentDB.Name = SlugConverter.URLFriendly(Path.GetFileNameWithoutExtension(attach.FileBase.FileName)) + "-" + hash.ToString() + Path.GetExtension(attach.FileBase.FileName);
                            var content = Server.MapPath("~/uploads/tickets/");
                            var path    = Path.Combine(content, Convert.ToString(ticket.Id));
                            Directory.CreateDirectory(path);

                            attach.FileBase.SaveAs(Path.Combine(Server.MapPath("~/uploads/tickets/" + ticket.Id + "/"), attachmentDB.Name));
                            attachmentDB.FilePath = "/uploads/tickets/" + ticket.Id + "/" + attachmentDB.Name;
                            db.TicketAttachments.Add(attachmentDB);
                        }
                        else
                        {
                            return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                        }
                    }
                }

                ticket.Created      = dateTimeNow;
                ticket.AuthorId     = User.Identity.GetUserId();
                ticket.TicketTypeId = ticket.TicketTypeId;
                db.Tickets.Add(ticket);
                db.SaveChanges();
                return(RedirectToAction("Index", "Projects"));
            }

            var modelTicket = new CreateTicketListModel();

            modelTicket.ProjectId = ticket.ProjectId;
            return(View(modelTicket));
        }