Exemplo n.º 1
0
        public IEnumerable <TicketDisplayViewModel> GetTickets()
        {
            using (var context = _context)
            {
                List <Ticket> tickets = new List <Ticket>();
                tickets = context.Tickets.AsNoTracking()
                          .Include(x => x.Employee)
                          .Include(x => x.Employee.Department)
                          .ToList();

                if (tickets != null)
                {
                    List <TicketDisplayViewModel> ticketsDisplay = new List <TicketDisplayViewModel>();
                    foreach (var x in tickets)
                    {
                        var ticketDisplay = new TicketDisplayViewModel()
                        {
                            TicketId      = x.TicketId,
                            TDescription  = x.TDescription,
                            ProjectName   = x.ProjectName,
                            TDateCreation = x.TDateCreation,
                            Name          = x.Employee.Name,
                            DName         = x.Employee.Department.DName
                        };
                        ticketsDisplay.Add(ticketDisplay);
                    }
                    return(ticketsDisplay);
                }
                return(null);
            }
        }
        public ActionResult UserViewTickets(string userID)
        {
            string userid = userID;

            if (userID == null)
            {
                userid = User.Identity.GetUserId();
            }
            List <TicketDisplayViewModel> tickets1 = new List <TicketDisplayViewModel>();
            ApplicationUser user = db.Users.Include(m => m.tickets).SingleOrDefault(m => m.Id == userid);

            foreach (var item in user.tickets)
            {
                List <GameViewModelT> viewGames1 = new List <GameViewModelT>();
                Ticket temp  = db.tickets.Find(item.ID);
                bool   iswon = false;
                for (int j = 0; j < temp.gameIDs.Length; j++)
                {
                    Game tempGame = db.Games.Find(temp.gameIDs[j]);
                    if (tempGame != null)
                    {
                        if (tempGame.Completed && tempGame.Result == item.guesses[j])
                        {
                            iswon = true;
                        }
                        else
                        {
                            iswon = false;
                        }
                        GameViewModelT gameView = new GameViewModelT()
                        {
                            gameId       = tempGame.ID,
                            HalfTime     = tempGame.HalfTime,
                            team1        = db.Teams.Find(tempGame.Team1ID),
                            team2        = db.Teams.Find(tempGame.Team2ID),
                            start        = tempGame.StartTime,
                            end          = tempGame.EndTime,
                            team1Score   = tempGame.team1Score,
                            team2Score   = tempGame.team2Score,
                            completed    = tempGame.Completed,
                            Coefficient1 = tempGame.Coefficient1,
                            Coefficient2 = tempGame.Coefficient2,
                            Coefficient3 = tempGame.Coefficient3,
                            outcome      = tempGame.Result,
                            Time         = tempGame.Time
                        };
                        viewGames1.Add(gameView);
                    }
                }
                if (temp.win != iswon)
                {
                    temp.win             = iswon;
                    db.Entry(temp).State = EntityState.Modified;
                    db.SaveChanges();
                }
                TicketDisplayViewModel tv = new TicketDisplayViewModel()
                {
                    TicketID         = temp.ID,
                    totalCoefficient = temp.totalCoefficient,
                    guesses          = temp.guesses,
                    games            = viewGames1,
                    moneyInvested    = temp.moneyInvested,
                    Result           = temp.win,
                    futureWinnings   = temp.WinMoney,
                    Time             = temp.time
                };
                tickets1.Add(tv);
            }
            return(View("ViewTickets", tickets1));
        }
        public ActionResult CreateTicket([Bind(Include = "moneyInvested,totalCoefficient,guesses,gameIds")] TicketCreateModel ticketCreate)
        {
            Ticket ticket = new Ticket();

            ticket.time             = DateTime.Now;
            ticket.totalCoefficient = ticketCreate.totalCoefficient;
            ticket.moneyInvested    = ticketCreate.moneyInvested;
            ticket.guesses          = ticketCreate.guesses;
            List <GameViewModelT> gameviews = new List <GameViewModelT>();
            bool win = false;

            int[] realIds = new int[ticketCreate.gameIds.Length];
            for (int i = 0; i < ticketCreate.gameIds.Length; i++)
            {
                int tempID = Convert.ToInt32(ticketCreate.gameIds[i]);
                realIds[i] = tempID;
                Game tempGame = db.Games.Find(tempID);
                if (tempGame.Completed && tempGame.Result == ticketCreate.guesses[i])
                {
                    win = true;
                }
                else
                {
                    win = false;
                }
                if (tempGame != null)
                {
                    GameViewModelT gameView = new GameViewModelT()
                    {
                        gameId       = tempGame.ID,
                        HalfTime     = tempGame.HalfTime,
                        team1        = db.Teams.Find(tempGame.Team1ID),
                        team2        = db.Teams.Find(tempGame.Team2ID),
                        start        = tempGame.StartTime,
                        end          = tempGame.EndTime,
                        team1Score   = tempGame.team1Score,
                        team2Score   = tempGame.team2Score,
                        completed    = tempGame.Completed,
                        Coefficient1 = tempGame.Coefficient1,
                        Coefficient2 = tempGame.Coefficient2,
                        Coefficient3 = tempGame.Coefficient3,
                        outcome      = tempGame.Result,
                        Time         = tempGame.Time
                    };
                    gameviews.Add(gameView);
                }
            }
            ticket.win     = win;
            ticket.gameIDs = realIds;
            db.tickets.Add(ticket);
            db.SaveChanges();
            List <TicketDisplayViewModel> tickets1 = new List <TicketDisplayViewModel>();

            string          userID = User.Identity.GetUserId();
            ApplicationUser user   = db.Users.Include(m => m.tickets).SingleOrDefault(m => m.Id == userID);

            user.tickets.Add(ticket);

            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();
            foreach (var item in user.tickets)
            {
                List <GameViewModelT> viewGames1 = new List <GameViewModelT>();
                Ticket temp  = db.tickets.Find(item.ID);
                bool   iswon = false;
                for (int j = 0; j < temp.gameIDs.Length; j++)
                {
                    Game tempGame = db.Games.Find(temp.gameIDs[j]);
                    if (tempGame != null)
                    {
                        if (tempGame.Completed && tempGame.Result == item.guesses[j])
                        {
                            iswon = true;
                        }
                        else
                        {
                            iswon = false;
                        }
                        GameViewModelT gameView = new GameViewModelT()
                        {
                            gameId       = tempGame.ID,
                            HalfTime     = tempGame.HalfTime,
                            team1        = db.Teams.Find(tempGame.Team1ID),
                            team2        = db.Teams.Find(tempGame.Team2ID),
                            start        = tempGame.StartTime,
                            end          = tempGame.EndTime,
                            team1Score   = tempGame.team1Score,
                            team2Score   = tempGame.team2Score,
                            completed    = tempGame.Completed,
                            Coefficient1 = tempGame.Coefficient1,
                            Coefficient2 = tempGame.Coefficient2,
                            Coefficient3 = tempGame.Coefficient3,
                            outcome      = tempGame.Result,
                            Time         = tempGame.Time
                        };
                        viewGames1.Add(gameView);
                    }
                }
                if (temp.win != iswon)
                {
                    temp.win             = iswon;
                    db.Entry(temp).State = EntityState.Modified;
                    db.SaveChanges();
                }
                TicketDisplayViewModel tv = new TicketDisplayViewModel()
                {
                    TicketID         = temp.ID,
                    totalCoefficient = temp.totalCoefficient,
                    guesses          = temp.guesses,
                    games            = viewGames1,
                    moneyInvested    = temp.moneyInvested,
                    Result           = temp.win,
                    futureWinnings   = temp.WinMoney,
                    Time             = temp.time
                };
                tickets1.Add(tv);
            }


            return(View("ViewTickets", tickets1));
        }