Example #1
0
        // request for editing the selected play
        public async Task <IActionResult> EditPlay(int?id)
        {
            //check if it has an id, if not send a 404 NotFound response
            if (id == null)
            {
                return(NotFound());
            }

            // find the play in the database
            var playFound = await _context.Plays
                            .Include(x => x.Performances).FirstOrDefaultAsync(x => x.Id == id);

            //check if playe found, if not send a 404 NotFound response
            if (playFound == null)
            {
                return(NotFound());
            }
            int      count           = 0;
            EditPlay performanceEdit = _mapper.Map <EditPlay>(playFound);

            performanceEdit.LiveDates = new List <DateTime>();
            DateTime newDate = DateTime.Parse(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture));

            while (count < 21)
            {
                count++;
                performanceEdit.LiveDates.Add(newDate);
            }

            return(View(performanceEdit));
        }
Example #2
0
        public async Task <IActionResult> EditPlay(int Id, EditPlay play)
        {
            // check if Ids match, if not return 404 Not Found Exception
            if (Id != play.Id)
            {
                return(NotFound());
            }

            // find play to edit
            var perfEdit = await _context.Plays.Include(x => x.Performances)
                           .FirstOrDefaultAsync(x => x.Id == play.Id);

            // check model
            if (ModelState.IsValid)
            {
                // add new date fields, check if date was changed and
                // it is in the future, starting from the day after
                foreach (var date in play.LiveDates)
                {
                    if (!date.Date.Day.Equals(DateTime.Now.Day))
                    {
                        Performance performanceDate = new Performance()
                        {
                            Date   = date.Date,
                            PlayId = play.Id
                        };
                        // create booked seats for each date and set them as not booked
                        foreach (var seat in _context.Seats)
                        {
                            BookedSeat booked = new BookedSeat()
                            {
                                SeatId = seat.Id,
                                Booked = 0
                            };
                            performanceDate.BookedSeats.Add(booked);
                        }
                        perfEdit.Performances.Add(performanceDate);
                    }
                }

                perfEdit.Name           = play.Name;
                perfEdit.Description    = play.Description;
                perfEdit.AgeRestriction = play.AgeRestriction.ToString();
                // try update database
                try
                {
                    _context.Update(perfEdit);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PlayExists(play.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(IndexPlays)));
            }
            return(View(play));
        }