public async Task <ActionResult> Create([Bind(Include = "Id,ProjectId,Title,Body,Priority,Type")] Ticket ticket)
        {
            if (ModelState.IsValid)
            {
                if (ticket.Title == null)
                {
                    ticket.Title = StringUtilities.Shorten(ticket.Body, 50);
                }
                ticket.Created = System.DateTimeOffset.Now;
                ticket.OwnerId = User.Identity.GetUserId();
                // Adding status and priority field
                var st = db.Statuses.ToList();
                foreach (Status s in st)
                {
                    if (s.Name == TicketStatus.Open.ToString())
                    {
                        ticket.StatusId = s.Id;
                    }
                }
                //ticket.StatusId = db.Statuses.Find(TicketStatus.Open.ToString()).Id;
                // 2017.03.22 (ericwat) - Added priority field to take the lookup value
                var pr = db.Priorities.ToList();
                foreach (Priority p in pr)
                {
                    if (p.Name == ticket.Priority.ToString())
                    {
                        ticket.PriorityId = p.Id;
                    }
                }

                ticket.Status = 0;
                History history = new History();
                history.Date = ticket.Created;
                var historyBody = "Ticket created. <br> Title: " + ticket.Title + "<br> Body: " + ticket.Body + "<br>" + "Priority: " + ticket.Priority + ", Type: " + ticket.Type.Name + ", Status: " + ticket.Status;
                history.Body     = historyBody;
                history.TicketId = ticket.Id;
                db.Tickets.Add(ticket);
                db.History.Add(history);
                db.SaveChanges();

                var user = db.Users.Find(ticket.OwnerId);
                //Send email to project managers
                ProjectUsersHelper helper = new ProjectUsersHelper(db);
                var projectManagers       = helper.ListProjectManagers(ticket.ProjectId);
                foreach (var pm in projectManagers)
                {
                    var svc = new EmailService();
                    var msg = new IdentityMessage();
                    msg.Destination = pm;
                    msg.Subject     = "Bug Tracker: Ticket Created";
                    msg.Body        = user.FullName + " has created the ticket '" + ticket.Title + "'. To assign this ticket to a user, please visit https://meyasu-bugtracker.azurewebsites.net/Tickets/Details/" + ticket.Id;
                    await svc.SendAsync(msg);
                }

                return(RedirectToAction("Index"));
            }

            return(View(ticket));
        }
        public async Task <ActionResult> Resolve(int id)
        {
            //Ticket Information
            Ticket ticket = db.Tickets.Find(id);

            ticket.Status   = Status.Resolved;
            ticket.Modified = System.DateTimeOffset.Now;
            var userId = User.Identity.GetUserId();
            var user   = db.Users.Find(userId);
            //Add to ticket history
            History history = new History();

            history.Date = System.DateTimeOffset.Now;
            var historyBody = "Ticket marked as Resolved by " + user.FullName;

            history.Body     = historyBody;
            history.TicketId = ticket.Id;
            db.History.Add(history);
            //Save ticket changes to database
            db.Tickets.Attach(ticket);
            db.Entry(ticket).Property("Status").IsModified   = true;
            db.Entry(ticket).Property("Modified").IsModified = true;
            db.SaveChanges();
            //Send email to project managers
            ProjectUsersHelper helper = new ProjectUsersHelper(db);
            var projectManagers       = helper.ListProjectManagers(ticket.ProjectId);

            foreach (var pm in projectManagers)
            {
                var svc = new EmailService();
                var msg = new IdentityMessage();
                msg.Destination = pm;
                msg.Subject     = "Bug Tracker: Ticket Resolved";
                msg.Body        = user.FullName + " has marked the ticket '" + ticket.Title + "' as Resolved. To close this ticket, please visit https://epalmer-bugtracker.azurewebsites.net/Tickets/Details/" + ticket.Id + " If there are still issues left to resolve on the ticket, the ticket's developer can be reached at " + ticket.Assignee.Email;
                await svc.SendAsync(msg);
            }

            return(RedirectToAction("Details", new { id = ticket.Id }));
        }