public Model.Appointments Insert(Model.Requests.InsertAppointmentRequest app)
        {
            bool validCinemDayMovie = false;

            foreach (var cdm in _context.CinemaDayMovie)
            {
                if (app.CinemaDayMovieId == cdm.CinemaDayMovieId)
                {
                    validCinemDayMovie = true;
                    break;
                }
            }
            if (!validCinemDayMovie)
            {
                throw new UserException("Invalid cinemaDayMovieId");
            }

            bool validHall = false;

            foreach (var hall in _context.Hall)
            {
                if (hall.HallId == app.HallId)
                {
                    validHall = true;
                    break;
                }
            }
            if (!validHall)
            {
                throw new UserException("Invalid hallId");
            }

            var appointment = _mapper.Map <Database.Appointments>(app);

            _context.Appointments.Add(appointment);
            _context.SaveChanges();
            return(_mapper.Map <Model.Appointments>(appointment));
        }
Esempio n. 2
0
        private async void saveBtn_Click(object sender, EventArgs e)
        {
            var messageBox = new CustomMessageBox();

            if (!_helper.ValidateDecimalString(Price.Text, 1, 100))
            {
                messageBox.Show("Enter a valid price (1-100)!", "error");
                return;
            }
            if (HallsBox.SelectedIndex == -1)
            {
                messageBox.Show("Select a hall!", "error");
                return;
            }

            var appApi  = new APIService("Appointments");
            var allApps = await appApi.Get <List <Model.Appointments> >(null);

            allApps.RemoveAll(a => a.HallId != (HallsBox.SelectedItem as dynamic).Value);

            var cdmApi  = new APIService("CinemaDayMovie");
            var allCdms = await cdmApi.Get <List <Model.CinemaDayMovie> >(null);

            var finalApps = new List <Model.Appointments>();

            foreach (var app in allApps)
            {
                foreach (var cdm in allCdms)
                {
                    if (app.CinemaDayMovieId == cdm.CinemaDayMovieId && cdm.AiringDaysOfCinemaId == _airingDayId)
                    {
                        finalApps.Add(app);
                        break;
                    }
                }
            }

            var moviesApi = new APIService("movies");
            var movie     = await moviesApi.GetById <Model.Movie>(_movieId);



            if (_appointmentId.HasValue)
            {
                var app = await appApi.GetById <Model.Appointments>(_appointmentId);

                finalApps.RemoveAll(a => a.AppointmentId == app.AppointmentId);
            }

            bool valid = true;

            foreach (var app in finalApps)
            {
                DateTime newBegins = new DateTime();
                DateTime newEnds   = new DateTime();
                newBegins = DateTime.Parse(StartsAt.Text);
                newEnds   = DateTime.Parse(StartsAt.Text).AddMinutes(TimeSpan.Parse(movie.Duration).TotalMinutes);


                var cdm = await cdmApi.GetById <Model.CinemaDayMovie>(app.CinemaDayMovieId);

                var thisMovie = await moviesApi.GetById <Model.Movie>(cdm.MovieId);

                DateTime begins = new DateTime();
                DateTime ends   = new DateTime();
                begins = DateTime.Parse(app.StartsAt);
                ends   = DateTime.Parse(app.StartsAt).AddMinutes(TimeSpan.Parse(thisMovie.Duration).TotalMinutes);

                bool overlap = begins < newEnds && newBegins < ends;
                if (overlap)
                {
                    valid = false;
                    break;
                }
            }
            if (!valid)
            {
                messageBox.Show("The appointment is overlaping!", "error");
                return;
            }

            Model.Requests.InsertAppointmentRequest Object = new Model.Requests.InsertAppointmentRequest()
            {
                Price            = decimal.Parse(Price.Text),
                CinemaDayMovieId = _cinemaDayMovieId,
                HallId           = (HallsBox.SelectedItem as dynamic).Value,
                SoldSeats        = 0,
                StartsAt         = StartsAt.Value.ToString("HH:mm")
            };

            if (_appointmentId.HasValue)
            {
                await appApi.Update <Model.Appointments>(_appointmentId, Object);

                messageBox.Show("Appointment updated succesfully", "Success");
            }
            else
            {
                await appApi.Insert <Model.Appointments>(Object);

                messageBox.Show("Appointment added succesfully", "Success");
            }

            this.Close();
            _appointmentsForm.Close();
            _cinemaDayMovieForm.Close();
            _scheduleForm.Close();
            ScheduleForm sch = new ScheduleForm(_schedule.Cinema.CinemaId, _menuForm)
            {
                MdiParent = _menuForm,
                Dock      = DockStyle.Fill
            };
            var schedule = await _apiService.CustomGet <Model.Requests.CinemasScheduleRequest>("GetCinemasSchedule", _schedule.Cinema.CinemaId);

            AppointmentsForm form = new AppointmentsForm(sch, schedule, _menuForm, _airingDayId, _date, _day, _cinemaDayMovieId, _movieId, _cinemaDayMovieForm);

            sch.Show();
            _helper.ShowForm(form, 15);
        }
        public Model.Appointments Update(int appointmentId, Model.Requests.InsertAppointmentRequest app)
        {
            bool validAppId = false;

            foreach (var inApp in _context.Appointments.ToList())
            {
                if (appointmentId == inApp.AppointmentId)
                {
                    validAppId = true;
                    break;
                }
            }

            if (!validAppId)
            {
                throw new UserException("Cannot find specified Appointment with that appointmentId!");
            }

            bool validCinemDayMovie = false;

            foreach (var cdm in _context.CinemaDayMovie)
            {
                if (app.CinemaDayMovieId == cdm.CinemaDayMovieId)
                {
                    validCinemDayMovie = true;
                    break;
                }
            }
            if (!validCinemDayMovie)
            {
                throw new UserException("Invalid cinemaDayMovieId");
            }

            bool validHall = false;

            foreach (var hall in _context.Hall)
            {
                if (hall.HallId == app.HallId)
                {
                    validHall = true;
                    break;
                }
            }
            if (!validHall)
            {
                throw new UserException("Invalid hallId");
            }


            var baseAppointment = _context.Appointments.Find(appointmentId);
            var oldPrice        = baseAppointment.Price;

            baseAppointment.Price    = app.Price;
            baseAppointment.StartsAt = app.StartsAt;
            baseAppointment.HallId   = app.HallId;
            _context.SaveChanges();

            var    forNot = _context.Appointments.Include(a => a.CinemaDayMovie).ThenInclude(cdm => cdm.Movie).Single(a => a.AppointmentId == appointmentId);
            Helper helper = new Helper(_context);

            helper.ChangeAppointmentNotification(baseAppointment, $"Appointment information for the movie '{forNot.CinemaDayMovie.Movie.Title}' changed (price: {baseAppointment.Price}, starts at: {baseAppointment.StartsAt}, hall: {_context.Hall.Find(baseAppointment.HallId).HallName} {_context.Hall.Find(baseAppointment.HallId).HallNumber})", "Warning", oldPrice, baseAppointment.Price);


            return(_mapper.Map <Model.Appointments>(baseAppointment));
        }
 public ActionResult <Model.Appointments> UpdateAppointment(int appointmentId, Model.Requests.InsertAppointmentRequest ap)
 {
     return(_service.Update(appointmentId, ap));
 }
 public ActionResult <Model.Appointments> AddAppointmentToCinema(Model.Requests.InsertAppointmentRequest app)
 {
     return(_service.Insert(app));
 }