Exemplo n.º 1
0
        private void SeedHallsAndSeats()
        {
            using (var context = new CinemaTicketsDbContext())
            {
                if (!context.Database.Exists())
                {
                    for (int i = 1; i <= 8; i++)
                    {
                        var hall = new Hall
                        {
                            HallNumber = i
                        };
                        context.Halls.Add(hall);

                        for (int row = 1; row <= 10; row++)
                        {
                            for (int col = 1; col <= 10; col++)
                            {
                                var seat = new Seat()
                                {
                                    Hall   = hall,
                                    Row    = row,
                                    Column = col
                                };
                                context.Seats.Add(seat);
                            }
                        }
                    }

                    context.SaveChanges();
                }
            }
        }
        public ActionResult CreateProjection(FormCollection data)
        {
            var hallID  = int.Parse(data["hallID"]);
            var movieID = int.Parse(data["movieID"]);
            var year    = int.Parse(data["year"]);
            var month   = int.Parse(data["month"]);
            var day     = int.Parse(data["day"]);
            var hours   = int.Parse(data["hours"]);
            var minutes = int.Parse(data["minutes"]);
            var date    = new DateTime(year, month, day, hours, minutes, 0);

            var context = new CinemaTicketsDbContext();

            using (context)
            {
                var projectionWithSameDate = context.Projections.FirstOrDefault(p => p.TimeOfProjection == date && p.HallID == hallID);

                if (!(projectionWithSameDate == null))
                {
                    return(new HttpStatusCodeResult(400, "There already is a projection set for that time for this hall"));
                }

                var hallFromDb  = context.Halls.FirstOrDefault(h => h.HallID == hallID);
                var movieFromDb = context.Movies.FirstOrDefault(m => m.MovieID == movieID);
                if (hallFromDb == null || movieFromDb == null)
                {
                    return(new HttpStatusCodeResult(400, "Something went wrong! :("));
                }

                var projection = new Projection
                {
                    Hall             = hallFromDb,
                    Movie            = movieFromDb,
                    TimeOfProjection = date
                };
                context.Projections.Add(projection);

                var seats = projection.Hall.Seats.ToList();
                foreach (var seat in seats)
                {
                    context.Tickets.Add(new Ticket
                    {
                        IsSold     = false,
                        Projection = projection,
                        Seat       = seat
                    });
                }

                context.SaveChanges();
            }

            return(new HttpStatusCodeResult(200, "OK"));
        }
        public ActionResult CreateHall(int hallNumber)
        {
            var hallFromDb = this._hallService.GetHallByHallNumber(hallNumber);

            if (!(hallFromDb == null))
            {
                return(new HttpStatusCodeResult(400, "There already is a hall with that number"));
            }

            var hallToAdd = new Hall
            {
                HallNumber = hallNumber
            };

            try
            {
                using (var context = new CinemaTicketsDbContext())
                {
                    for (int row = 1; row <= 10; row++)
                    {
                        for (int col = 1; col <= 10; col++)
                        {
                            var seat = new Seat()
                            {
                                Hall   = hallToAdd,
                                Row    = row,
                                Column = col
                            };
                            context.Seats.Add(seat);
                        }
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(400, e.Message));
            }

            return(new HttpStatusCodeResult(200, "Success"));
        }
Exemplo n.º 4
0
        public ActionResult BuyTickets(FormCollection data)
        {
            var kidsRetirees = int.Parse(data["kidsRetirees"]);
            var students     = int.Parse(data["students"]);
            var adults       = int.Parse(data["adults"]);
            var projectionID = int.Parse(data["projectionID"]);
            var seats        = data["seats"].Split(',').Select(int.Parse).ToArray();

            List <decimal> ticketTypesPrices = new List <decimal>();
            var            ticketsBought     = new Dictionary <Ticket, int[]>();

            if (kidsRetirees != 0)
            {
                for (int i = 1; i <= kidsRetirees; i++)
                {
                    ticketTypesPrices.Add(TicketPrices.kidsRetirees);
                }
            }

            if (students != 0)
            {
                for (int i = 1; i <= students; i++)
                {
                    ticketTypesPrices.Add(TicketPrices.students);
                }
            }

            if (adults != 0)
            {
                for (int i = 1; i <= adults; i++)
                {
                    ticketTypesPrices.Add(TicketPrices.adults);
                }
            }


            using (var context = new CinemaTicketsDbContext())
            {
                try
                {
                    var tickets       = context.Projections.FirstOrDefault(p => p.ProjectionID == projectionID).Tickets.ToList();
                    var counter       = 0;
                    var usernameEmail = (string)Session["usernameEmail"];
                    foreach (var ticket in tickets)
                    {
                        foreach (var seat in seats)
                        {
                            if (ticket.SeatID == seat)
                            {
                                var userSoldCurrentTicket = context.Employees.FirstOrDefault(e => e.Email == usernameEmail);
                                var currentSeat           = ticket.Seat;
                                ticket.IsSold   = true;
                                ticket.Price    = ticketTypesPrices[counter];
                                ticket.Employee = userSoldCurrentTicket;
                                counter++;
                                ticketsBought[ticket] = new int[] { currentSeat.Row, currentSeat.Column };
                            }
                        }
                    }
                    context.SaveChanges();
                }
                catch (OptimisticConcurrencyException ex)
                {
                }
            }
            CacheViewModel.CacheModel(ticketsBought);
            return(new HttpStatusCodeResult(200, "OK"));
        }