Esempio n. 1
0
        public bool BuyTicket(TicketDTO ticketDTO, ITransactionalData dbContext)
        {
            var list = dbContext.Users.Where(u => u.Id == ticketDTO.User.Id && u.Password == ticketDTO.User.Password).ToList();

            if (list.Count == 0)
            {
                return(false);
            }
            User   currentUser = list.First();
            Ticket ticket      = AutoMapper.Mapper.Map <TicketDTO, Ticket>(ticketDTO);

            for (int i = 0; i < ticket.ConnectionPath.ConnectionsParts.Count; i++)
            {
                var tmp = ticket.ConnectionPath[i];
                ticket.ConnectionPath[i] = dbContext.ConnectionParts.Where(cp => cp.Id == tmp.Id).First();

                int[] userSeat = ticket.Seats[i];

                foreach (var s in userSeat)
                {
                    string seat  = s.ToString();
                    string seats = ticket.ConnectionPath[i].Seats;
                    int    index = seats.IndexOf(seat);
                    string newSeats;
                    if (index < 0)
                    {
                        dbContext.Reject();
                        return(false);
                    }
                    if (seats.Length == seat.Length)
                    {
                        newSeats = "";
                    }
                    else if (index == seats.Length - seat.Length)
                    {
                        newSeats = seats.Remove(index - 1, seat.Length + 1);
                    }
                    else
                    {
                        newSeats = seats.Remove(index, seat.Length + 1);
                    }

                    ticket.ConnectionPath[i].Seats = newSeats;
                    ticket.ConnectionPath[i].FreeSeats--;
                }
            }
            if (currentUser.Tickets.Length == 0)
            {
                currentUser.Tickets = CreateStringTicket(ticket);
            }
            else
            {
                currentUser.Tickets = string.Format(currentUser.Tickets + ";" + CreateStringTicket(ticket));
            }
            dbContext.SaveChanges();
            return(true);
        }
        public ActionResult BuyTicket(string ticket)
        {
            TicketViewModel viewModel;

            try
            {
                viewModel = JsonConvert.DeserializeObject <TicketViewModel>(ticket);
            }
            catch
            {
                return(View("MyError", new ErrorViewModel()
                {
                    ErrorMessage = "Wystąpił nieoczekiwany błąd"
                }));
            }
            if (viewModel == null)
            {
                return(View("MyError", new ErrorViewModel()
                {
                    ErrorMessage = "Wystąpił nieoczekiwany błąd."
                }));
            }
            using (ITransactionalData dbContext = unitOfFactory.CreateTransactionalUnitOfWork())
            {
                ApplicationUserManager userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
                bool ok = buyTicketModel.BuyTicket(viewModel, dbContext, userManager);

                if (ok)
                {
                    dbContext.Accept();
                }
                else
                {
                    return(View("MyError", new ErrorViewModel()
                    {
                        ErrorMessage = "Z przykrością informujemy, że ktoś był szybszy i zajął twoje miejsce"
                    }));
                }
            }

            return(View("MySuccess", new ErrorViewModel()
            {
                ErrorMessage = "Transakcja przebiegła poprawnie. Bilet został zakupiony."
            }));
        }
Esempio n. 3
0
        public bool BuyTicket(TicketViewModel ticketViewModel, ITransactionalData dbContext, ApplicationUserManager userManager)
        {
            ApplicationUser currentUser = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
            Ticket          ticket      = new Ticket()
            {
                ConnectionPath = new ConnectionPath()
                {
                    ConnectionsParts = ticketViewModel.ConnectionsParts
                },
                Seats        = ticketViewModel.Seats,
                ListDiscount = ticketViewModel.ListDiscounts,
                Way          = ticketViewModel.Way,
                Price        = ticketViewModel.Price
            };

            ticket.ConnectionPath.Initialize();

            for (int i = 0; i < ticket.ConnectionPath.ConnectionsParts.Count; i++)
            {
                var tmp = ticket.ConnectionPath[i];
                ticket.ConnectionPath[i] = dbContext.ConnectionParts.Where(cp => cp.Id == tmp.Id).First();

                int[] userSeat = ticket.Seats[i];

                foreach (var s in userSeat)
                {
                    string seat  = s.ToString();
                    string seats = ticket.ConnectionPath[i].Seats;
                    int    index = seats.IndexOf(seat);
                    string newSeats;
                    if (index < 0)
                    {
                        dbContext.Reject();
                        return(false);
                    }
                    if (seats.Length == seat.Length)
                    {
                        newSeats = "";
                    }
                    else if (index == seats.Length - seat.Length)
                    {
                        newSeats = seats.Remove(index - 1, seat.Length + 1);
                    }
                    else
                    {
                        newSeats = seats.Remove(index, seat.Length + 1);
                    }

                    ticket.ConnectionPath[i].Seats = newSeats;
                    ticket.ConnectionPath[i].FreeSeats--;
                }
            }
            if (currentUser.Tickets.Length == 0)
            {
                currentUser.Tickets = CreateStringTicket(ticket);
            }
            else
            {
                currentUser.Tickets = string.Format(currentUser.Tickets + ";" + CreateStringTicket(ticket));
            }

            userManager.Update(currentUser);
            dbContext.SaveChanges();
            return(true);
        }