Пример #1
0
        //se creaza sala
        public async Task <IActionResult> Create(CreateHallViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }


            //tranzactie->ori se creaza sala cu toate locurile ori nimic
            using var transaction = _context.Database.BeginTransaction();

            try
            {
                Hall hall = new Hall();
                hall.name    = model.name;
                hall.rows    = model.rows;
                hall.columns = model.cols;
                hall.active  = true;

                _context.Halls.Add(hall);
                await _context.SaveChangesAsync();

                //lista de culoare-: se transforma string-ul trimis intr-o lista de ints
                List <int> paths = model.paths.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();

                //primul rand de scaune
                int row = 1;
                for (int i = 1; i <= model.rows; i++)
                {
                    //numarul de ordine al primului element al matricii de pe randul curent
                    int firstRowElement = i * model.cols - model.cols + 1;

                    //lista numerelor de ordine a elementelor din matrice de pe randul curent
                    List <int> rowElementsNumbers = Enumerable.Range(firstRowElement, model.cols).ToList();

                    //se verifica daca in lista de elemenete exista doar culoare
                    if (rowElementsNumbers.Except(paths).ToList().Count == 0)
                    {
                        //se creaza un rand intreg de culoare cu row=0 si seat.no=null
                        for (int j = 1; j <= model.cols; j++)
                        {
                            Seat seat = new Seat();
                            //numarul elementului din matrice
                            var order = j + (i - 1) * model.cols;
                            seat.hallId = hall.id;

                            //randurile complet goale nu sunt considerate randuri de scaune
                            seat.row    = 0;
                            seat.seatNo = null;
                            seat.order  = order;
                            _context.Seats.Add(seat);
                        }
                    }
                    else
                    {   //numarul de scaune pe randul respectiv
                        int noseats = model.cols - rowElementsNumbers.AsQueryable().Intersect(paths).Count();
                        for (int j = 1; j <= model.cols; j++)
                        {
                            Seat seat = new Seat();

                            //numarul elementului din matrice
                            var order = j + (i - 1) * model.cols;
                            //se verifica daca elementul curent e culoar
                            if (paths.Contains(firstRowElement + j - 1))
                            {
                                seat.hallId = hall.id;
                                seat.row    = row;
                                seat.seatNo = null;
                                seat.order  = order;
                                _context.Seats.Add(seat);
                            }
                            else
                            {
                                seat.hallId = hall.id;
                                seat.row    = row;
                                seat.seatNo = noseats;
                                seat.order  = order;
                                _context.Seats.Add(seat);
                                noseats--;
                            }
                        }
                        //se incrementeaza randurile de scaune doar daca randul contine cel putin un scaun
                        row++;
                    }
                }

                //se incheie tranzactia

                await _context.SaveChangesAsync();

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                TempData["ErrorMessage"] = "O eroare a intervenit în crearea sălii și procesul a eșuat! Mai încercați odată!";
                return(RedirectToAction("List"));
            }

            TempData["SuccesMessage"] = "Sala a fost creată cu succes!";
            return(RedirectToAction("List"));
        }
Пример #2
0
        //returneaza pagina de creare sala
        public IActionResult Create()
        {
            CreateHallViewModel model = new CreateHallViewModel();

            return(View(model));
        }