Пример #1
0
        public async Task <AuditoriumDomainModel> GetAuditoriumByIdAsync(int id)
        {
            var data = await _auditoriumsRepository.GetByIdAsync(id);

            if (data == null)
            {
                return(null);
            }
            AuditoriumDomainModel domainModel = new AuditoriumDomainModel
            {
                Id       = data.Id,
                Name     = data.Name,
                CinemaId = data.CinemaId
            };

            return(domainModel);
        }
        public async Task <AuditoriumDomainModel> GetAuditoriumByIdAsync(int id)
        {
            var data = await _auditoriumRepository.GetByIdAsync(id);

            if (data == null)
            {
                return(null);
            }

            AuditoriumDomainModel result;

            result = new AuditoriumDomainModel
            {
                AuditoriumId     = data.AuditoriumId,
                NameOfAuditorium = data.AuditoriumName,
                NumberOfSeats    = data.NumberOfSeats,
                MuseumId         = data.MuseumId
            };
            return(result);
        }
Пример #3
0
        public async Task <DeleteAuditoriumResultModel> DeleteAuditorium(int Id)
        {
            var auditorium = await _auditoriumsRepository.GetByIdAsync(Id);

            if (auditorium == null)
            {
                return(new DeleteAuditoriumResultModel
                {
                    IsSuccessful = false,
                    ErrorMessage = Messages.AUDITORIUM_NOT_FOUND
                });
            }

            var futureShows = auditorium.Shows.Any(show => show.ShowTime > DateTime.Now);

            if (futureShows)
            {
                return(new DeleteAuditoriumResultModel
                {
                    IsSuccessful = false,
                    ErrorMessage = Messages.AUDITORIUM_HAS_FUTURE_SHOWS
                });
            }

            var seats = await _seatsRepository.GetSeatsByAuditoriumId(auditorium.Id);

            foreach (var seat in seats)
            {
                await _seatsRepository.Delete(seat.Id);
            }

            foreach (var show in auditorium.Shows)
            {
                await _showsRepository.Delete(show.Id);
            }

            await _auditoriumsRepository.Delete(auditorium.Id);

            _showsRepository.Save();
            _seatsRepository.Save();
            _auditoriumsRepository.Save();

            return(new DeleteAuditoriumResultModel
            {
                IsSuccessful = true,
                Auditorium = new AuditoriumDomainModel
                {
                    Id = auditorium.Id,
                    Name = auditorium.Name,
                    TheatreId = auditorium.TheatreId
                }
            });
        }
        public async Task <AuditoriumDomainModel> GetByIdAsync(int id)
        {
            var data = await _auditoriumsRepository.GetByIdAsync(id);

            if (data == null)
            {
                return(null);
            }

            List <SeatDomainModel> domainModelList = new List <SeatDomainModel>();

            if (data.Seats != null)
            {
                foreach (Seat seat in data.Seats)
                {
                    SeatDomainModel domainModel = new SeatDomainModel()
                    {
                        Id           = seat.Id,
                        AuditoriumId = seat.AuditoriumId,
                        Number       = seat.Number,
                        Row          = seat.Row
                    };
                    domainModelList.Add(domainModel);
                }
            }

            AuditoriumDomainModel result = new AuditoriumDomainModel()
            {
                Id        = data.Id,
                CinemaId  = data.CinemaId,
                Name      = data.Name,
                SeatsList = domainModelList
            };

            return(result);
        }
Пример #5
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);
        }
Пример #6
0
        public async Task <ShowResultModel> AddShow(ShowDomainModel requestedShow)
        {
            // check if requested show time is in the past
            if (requestedShow.ShowTime < DateTime.Now)
            {
                return(new ShowResultModel
                {
                    isSuccessful = false,
                    ErrorMessage = Messages.SHOW_IN_THE_PAST
                });
            }

            // check if auditorium exist
            var existingAuditorium = await _auditoriumsRepository.GetByIdAsync(requestedShow.AuditoriumId);

            if (existingAuditorium == null)
            {
                return(new ShowResultModel
                {
                    isSuccessful = false,
                    ErrorMessage = Messages.CANNOT_CREATE_SHOW_AUDITORIUM_DOES_NOT_EXIST,
                });
            }

            // check if piece exist
            var existingPiece = await _piecesRepository.GetByIdAsync(requestedShow.PieceId);

            if (existingPiece == null)
            {
                return(new ShowResultModel
                {
                    isSuccessful = false,
                    ErrorMessage = Messages.CANNOT_CREATE_SHOW_PIECE_DOES_NOT_EXIST
                });
            }

            // check if shows are at the same time
            int showTime = 3;
            var allShows = await _showsRepository.GetAllAsync();

            var allShowsInAuditorium = allShows
                                       .Where(show => show.AuditoriumId == requestedShow.AuditoriumId);

            var showsAtTheSameTime = allShowsInAuditorium
                                     .Where(x => x.ShowTime <requestedShow.ShowTime.AddHours(showTime) && x.ShowTime> requestedShow.ShowTime.AddHours(-showTime));

            if (showsAtTheSameTime.Count() > 0)
            {
                return(new ShowResultModel
                {
                    ErrorMessage = Messages.SHOWS_AT_THE_SAME_TIME,
                    isSuccessful = false
                });
            }

            // check if actors are provided
            if (requestedShow.ActorsList.Count() == 0)
            {
                return(new ShowResultModel
                {
                    ErrorMessage = Messages.ACTORS_NOT_PROVIDED,
                    isSuccessful = false
                });
            }

            // check if actors exist in database
            var allActors = await _actorsRepository.GetAllAsync();

            IEnumerable <int> actorIds          = allActors.Select(actor => actor.Id);
            IEnumerable <int> requestedActorIds = requestedShow.ActorsList.Select(actor => actor.Id);

            var actorsInBoth = actorIds.Intersect(requestedActorIds);

            if (actorsInBoth.Count() < requestedActorIds.Count())
            {
                return(new ShowResultModel
                {
                    isSuccessful = false,
                    ErrorMessage = Messages.CANNOT_CREATE_SHOW_ACTORS_DOES_NOT_EXIST
                });
            }

            //check if any actor has more than 2 shows on the same day
            var requestedActorsInDatabase = new List <Actor>();

            foreach (var requested in requestedShow.ActorsList)
            {
                var actor = await _actorsRepository.GetByIdAsync(requested.Id);

                requestedActorsInDatabase.Add(actor);
            }

            foreach (var actor in requestedActorsInDatabase)
            {
                if (actor.ShowActors.Any(showactor => showactor.Show.ShowTime.Date == requestedShow.ShowTime.Date))
                {
                    if (actor.ShowActors.Count(showActor => showActor.Show.ShowTime.Date == requestedShow.ShowTime.Date) > 2)
                    {
                        return(new ShowResultModel
                        {
                            ErrorMessage = Messages
                                           .CANNOT_CREATE_SHOW_SOME_ACTORS_HAVE_MORE_THAN_TWO_SHOWS_PER_DAY,
                            isSuccessful = false,
                        });
                    }
                }
            }

            // insert in database
            Show showToInsert = new Show
            {
                ShowTime     = requestedShow.ShowTime,
                AuditoriumId = requestedShow.AuditoriumId,
                PieceId      = requestedShow.PieceId,
                TicketPrice  = requestedShow.TicketPrice
            };

            var insertedShow = _showsRepository.Insert(showToInsert);

            insertedShow.ShowActors = requestedShow.ActorsList.Select(actorDomainModel => new ShowActor
            {
                ActorId = actorDomainModel.Id,
                ShowId  = insertedShow.Id
            }).ToList();

            _showsRepository.Save();

            // return created show to user
            var createdShow = await _showsRepository.GetByIdAsync(insertedShow.Id);

            ShowResultModel resultModel = new ShowResultModel
            {
                ErrorMessage    = null,
                isSuccessful    = true,
                ShowDomainModel = new ShowDomainModel
                {
                    Id           = createdShow.Id,
                    AuditoriumId = createdShow.AuditoriumId,
                    PieceId      = createdShow.PieceId,
                    TicketPrice  = createdShow.TicketPrice,
                    ShowTime     = createdShow.ShowTime,
                    PieceTitle   = createdShow.Piece.Title,
                    ActorsList   = createdShow.ShowActors.Select(showActor => new ActorDomainModel
                    {
                        Id        = showActor.Actor.Id,
                        FirstName = showActor.Actor.FirstName,
                        LastName  = showActor.Actor.LastName
                    }).ToList()
                }
            };

            return(resultModel);
        }