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); }
public async Task <ReservationResultModel> CreateReservation(ReservationDomainModel requestedReservation) { // get taken seats for show var takenSeats = await GetTakenSeats(requestedReservation.ShowId); var seats = await _seatRepository.GetAllAsync(); // check if show id is valid var show = await _showRepository.GetByIdAsync(requestedReservation.ShowId); if (show == null) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.SHOW_DOES_NOT_EXIST_FOR_RESERVATION }); } // check if user id is valid var user = await _userRepository.GetByIdAsync(requestedReservation.UserId); if (user == null) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.USER_FOR_RESERVATION_DOES_NOT_EXIST }); } // get all seats for auditorium seats = seats.Where(seat => seat.AuditoriumId == show.AuditoriumId); //check if requested seats exist in the auditorium var duplicateSeats = seats .Select(x => x.Id) .Intersect(requestedReservation.SeatsList .Select(x => x.Id)); if (duplicateSeats.Count() != requestedReservation.SeatsList.Count()) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.SEAT_DOES_NOT_EXIST_FOR_AUDITORIUM }); } // check if requested seats are more than 1 and in the same row List <SeatDomainModel> seatModels = new List <SeatDomainModel>(); foreach (var seat in requestedReservation.SeatsList) { var reqSeat = await _seatRepository.GetByIdAsync(seat.Id); SeatDomainModel seatDomain = new SeatDomainModel { Id = reqSeat.Id, Number = reqSeat.Number, Row = reqSeat.Row, AuditoriumId = reqSeat.AuditoriumId }; seatModels.Add(seatDomain); } // check if seats are duplicates var row = seatModels[0].Number; var differentRows = seatModels.Select(x => x.Number).Distinct(); if (differentRows.Count() < seatModels.Count) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.SEATS_CANNOT_BE_DUPLICATES }); } // check if seats are in the same row if (requestedReservation.SeatsList.Count() > 1) { var singleSeat = seatModels[0]; foreach (var x in seatModels) { if (singleSeat.Row != x.Row) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.SEATS_NOT_IN_THE_SAME_ROW }); } } } // check if seats are next to each other if (requestedReservation.SeatsList.Count() > 1) { seatModels = seatModels.OrderByDescending(x => x.Number).ToList(); var singleSeat2 = seatModels[0]; var counter = 1; foreach (var x in seatModels.Skip(1)) { if (x.Number + counter != singleSeat2.Number) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.SEATS_MUST_BE_NEXT_TO_EACH_OTHER }); } else { counter++; } } } // check if requested seats are already taken if (takenSeats != null) { foreach (var takenSeat in takenSeats) { foreach (var requestedSeat in requestedReservation.SeatsList) { if (takenSeat.Id == requestedSeat.Id) { return(new ReservationResultModel { isSuccessful = false, ErrorMessage = Messages.SEATS_ALREADY_TAKEN_ERROR }); } } } } // insert in database Reservation reservationToInsert = new Reservation { ShowId = requestedReservation.ShowId, UserId = requestedReservation.UserId, ReservationSeats = new List <ReservationSeat>() }; var insertedReservation = _reservationsRepository.Insert(reservationToInsert); foreach (var rs in requestedReservation.SeatsList) { reservationToInsert.ReservationSeats.Add(new ReservationSeat { SeatId = rs.Id, ReservationId = insertedReservation.Id }); } _reservationsRepository.Save(); return(new ReservationResultModel { isSuccessful = true, ReservationDomainModel = new ReservationDomainModel { Id = insertedReservation.Id, ShowId = insertedReservation.ShowId, UserId = insertedReservation.UserId } }); }