예제 #1
0
        public async Task <CreateCinemaResultModel> DeleteCinema(int id)
        {
            var existingCinema = await _cinemasRepository.GetByIdAsync(id);

            var auditoriumsInCinema = _auditoriumService.GetAllOfSpecificCinema(id);

            if (existingCinema == null)
            {
                CreateCinemaResultModel errorModel = new CreateCinemaResultModel
                {
                    ErrorMessage = Messages.CINEMA_DOES_NOT_EXIST,
                    IsSuccessful = false
                };
                return(errorModel);
            }

            foreach (var item in auditoriumsInCinema)
            {
                var existingAuditorium = await _auditoriumsRepository.GetByIdAsync(item.Id);

                if (existingAuditorium == null)
                {
                    return(new CreateCinemaResultModel
                    {
                        ErrorMessage = Messages.AUDITORIUM_DOES_NOT_EXIST,
                        IsSuccessful = false
                    });
                }
                var projectionsInAuditorium = _projectionsRepository.GetAllOfSpecificAuditorium(item.Id);
                if (projectionsInAuditorium != null)
                {
                    foreach (var projection in projectionsInAuditorium)
                    {
                        if (projection.DateTime > DateTime.Now)
                        {
                            return(new CreateCinemaResultModel
                            {
                                ErrorMessage = Messages.PROJECTION_IN_FUTURE,
                                IsSuccessful = false,
                                Cinema = new CinemaDomainModel
                                {
                                    Id = existingCinema.Id,
                                    Name = existingCinema.Name
                                }
                            });
                        }
                        _projectionsRepository.Delete(projection.Id);
                        await _ticketService.DeleteTicketFromProjection(projection.Id);
                    }
                }
                var seatsInAuditorium = _seatsRepository.GetAllOfSpecificAuditorium(item.Id);
                if (seatsInAuditorium != null)
                {
                    foreach (var seat in seatsInAuditorium)
                    {
                        _seatsRepository.Delete(seat.Id);
                    }
                }
                var deleteVariable = _auditoriumsRepository.Delete(item.Id);
                AuditoriumResultModel auditoriumModel = new AuditoriumResultModel
                {
                    ErrorMessage = null,
                    IsSuccessful = true,
                    Auditorium   = new AuditoriumDomainModel
                    {
                        CinemaId = existingAuditorium.CinemaId,
                        Id       = existingAuditorium.Id,
                        Name     = existingAuditorium.Name
                    }
                };

                if (!auditoriumModel.IsSuccessful)
                {
                    CreateCinemaResultModel errorModel = new CreateCinemaResultModel
                    {
                        ErrorMessage = Messages.CINEMA_DELETION_ERROR,
                        IsSuccessful = false,
                        Cinema       = new CinemaDomainModel
                        {
                            Id   = existingCinema.Id,
                            Name = existingCinema.Name
                        }
                    };
                    return(errorModel);
                }
            }
            var data = _cinemasRepository.Delete(id);

            _cinemasRepository.Save();

            CreateCinemaResultModel domainModel = new CreateCinemaResultModel
            {
                ErrorMessage = null,
                IsSuccessful = true,
                Cinema       = new CinemaDomainModel
                {
                    Id   = existingCinema.Id,
                    Name = existingCinema.Name
                }
            };

            return(domainModel);
        }
예제 #2
0
        public async Task <AuditoriumResultModel> UpdateAuditorium(AuditoriumDomainModel auditoriumToUpdate)
        {
            var projectionsInAuditorium = _projectionsRepository.GetAllOfSpecificAuditorium(auditoriumToUpdate.Id);

            if (projectionsInAuditorium != null)
            {
                foreach (var item in projectionsInAuditorium)
                {
                    if (item.DateTime > DateTime.Now)
                    {
                        return(new AuditoriumResultModel
                        {
                            IsSuccessful = false,
                            ErrorMessage = Messages.AUDITORIUM_UPDATE_ERROR_PROJECTION_IN_FUTURE
                        });
                    }
                }
            }
            var seatsInAuditorium = _seatsRepository.GetAllOfSpecificAuditorium(auditoriumToUpdate.Id);

            if (seatsInAuditorium != null)
            {
                foreach (var item in seatsInAuditorium)
                {
                    await _ticketService.DeleteTicket(item.Id);

                    _seatsRepository.Delete(item.Id);
                }
            }

            Auditorium auditorium = new Auditorium()
            {
                Id       = auditoriumToUpdate.Id,
                Name     = auditoriumToUpdate.Name,
                CinemaId = auditoriumToUpdate.CinemaId,
                Seats    = new List <Seat>()
            };

            for (int i = 1; i <= auditoriumToUpdate.SeatRows; i++)
            {
                for (int j = 1; j <= auditoriumToUpdate.NumberOfSeats; j++)
                {
                    Seat newSeat = new Seat()
                    {
                        Row    = i,
                        Number = j
                    };
                    auditorium.Seats.Add(newSeat);
                }
            }

            var data = _auditoriumsRepository.Update(auditorium);

            if (data == null)
            {
                return(new AuditoriumResultModel
                {
                    IsSuccessful = false,
                    ErrorMessage = Messages.AUDITORIUM_UPDATE_ERROR
                });
            }
            _auditoriumsRepository.Save();

            AuditoriumResultModel domainModel = new AuditoriumResultModel
            {
                ErrorMessage = null,
                IsSuccessful = true,
                Auditorium   = new AuditoriumDomainModel
                {
                    CinemaId      = data.CinemaId,
                    Id            = data.Id,
                    Name          = data.Name,
                    NumberOfSeats = data.Seats.Select(x => x.Number).Count(),
                    SeatRows      = data.Seats.Select(x => x.Row).Count(),
                }
            };

            return(domainModel);
        }