public void AddShow_NotSuccessful_Piece_Does_Not_Exist() { // Arrange string expectedMessage = Messages.CANNOT_CREATE_SHOW_PIECE_DOES_NOT_EXIST; _showDomainModel = new ShowDomainModel { ShowTime = DateTime.Now.AddDays(5) }; _mockAuditoriumsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Auditorium()); _mockPiecesRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(null as Piece); // Act var resultObject = _showService .AddShow(_showDomainModel) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Assert Assert.IsFalse(resultObject.isSuccessful); Assert.AreEqual(expectedMessage, resultObject.ErrorMessage); }
public void AddShow_NotSuccessful_Show_In_The_Past() { // Arrange _showDomainModel = new ShowDomainModel { ShowTime = DateTime.Now.AddDays(-5) }; string expectedMessage = Messages.SHOW_IN_THE_PAST; // Act var resultObject = _showService .AddShow(_showDomainModel) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Assert Assert.IsFalse(resultObject.isSuccessful); Assert.AreEqual(expectedMessage, resultObject.ErrorMessage); }
public void AddShow_NotSuccessful_Shows_At_The_Same_Time_Error() { // Arrange string expectedMessage = Messages.SHOWS_AT_THE_SAME_TIME; bool expectedBool = false; _showDomainModel = new ShowDomainModel { ShowTime = DateTime.Now.AddMinutes(5) }; _mockAuditoriumsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Auditorium()); _mockPiecesRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Piece()); _mockShowsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Show>() { new Show { ShowTime = DateTime.Now.AddHours(-1) } }); // Act var objectResult = _showService .AddShow(_showDomainModel) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Assert Assert.IsNotNull(objectResult); Assert.AreEqual(expectedMessage, objectResult.ErrorMessage); Assert.AreEqual(expectedBool, objectResult.isSuccessful); }
public async Task <ActionResult <ShowDomainModel> > CreateShowAsync([FromBody] ShowModel showModel) { ShowDomainModel domainModel = new ShowDomainModel { ShowTime = showModel.ShowTime, AuditoriumId = showModel.AuditoriumId, PieceId = showModel.PieceId, TicketPrice = showModel.TicketPrice, ActorsList = showModel.ActorIds }; ShowResultModel createShow; try { createShow = await _showService.AddShow(domainModel); } catch (DbUpdateException e) { ErrorResponseModel errorResponse = new ErrorResponseModel { ErrorMessage = e.InnerException.Message ?? e.Message, StatusCode = System.Net.HttpStatusCode.BadRequest }; return(BadRequest(errorResponse)); } if (!createShow.isSuccessful) { ErrorResponseModel errorResponseModel = new ErrorResponseModel { ErrorMessage = createShow.ErrorMessage }; return(BadRequest(errorResponseModel)); } return(CreatedAtAction(nameof(GetShowById), new { id = createShow.ShowDomainModel.Id }, createShow.ShowDomainModel)); }
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 void AddShow_Successful_Insert_In_Database() { // Arrange string expectedMessage = null; bool expectedBool = true; _showDomainModel = new ShowDomainModel { ShowTime = DateTime.Now.AddMinutes(5), ActorsList = new List <ActorDomainModel> { new ActorDomainModel { Id = 1 }, new ActorDomainModel { Id = 2 } } }; _mockAuditoriumsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Auditorium()); _mockPiecesRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Piece()); _mockShowsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Show>() { new Show { ShowTime = DateTime.Now.AddHours(5) } }); _mockActorsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Actor>() { new Actor { Id = 1 }, new Actor { Id = 2 } }); _mockActorsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Actor { ShowActors = new List <ShowActor>() { new ShowActor { Show = new Show { ShowTime = DateTime.Now } }, new ShowActor { Show = new Show { ShowTime = DateTime.Now } } } }); _show = new Show { Id = 1, ShowTime = DateTime.Now, TicketPrice = 20, ShowActors = new List <ShowActor> { new ShowActor { Actor = new Actor { Id = 1, FirstName = "ime", LastName = "prezime", ShowActors = new List <ShowActor>() } } }, Piece = new Piece { Id = 1, Title = "naslov123", Year = 1995, Description = "neki opis", IsActive = true } }; _mockShowsRepository .Setup(x => x.Insert(It.IsAny <Show>())) .Returns(new Show { Id = 1, ShowTime = DateTime.Now }); _mockShowsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(_show); // Act var objectResult = _showService .AddShow(_showDomainModel) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Assert Assert.AreEqual(expectedBool, objectResult.isSuccessful); Assert.IsNull(expectedMessage, objectResult.ErrorMessage); Assert.IsNotNull(objectResult.ShowDomainModel); }
public void Add_Show_NotSuccessful_Some_Actors_Have_Two_Shows_On_Same_Day() { // Arrange string expectedMessage = Messages .CANNOT_CREATE_SHOW_SOME_ACTORS_HAVE_MORE_THAN_TWO_SHOWS_PER_DAY; bool expectedBool = false; _showDomainModel = new ShowDomainModel { ShowTime = DateTime.Now.AddMinutes(5), ActorsList = new List <ActorDomainModel> { new ActorDomainModel { Id = 1 }, new ActorDomainModel { Id = 2 } } }; _mockAuditoriumsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Auditorium()); _mockPiecesRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Piece()); _mockShowsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Show>() { new Show { ShowTime = DateTime.Now.AddHours(5) } }); _mockActorsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Actor>() { new Actor { Id = 1 }, new Actor { Id = 2 } }); _mockActorsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Actor { ShowActors = new List <ShowActor>() { new ShowActor { Show = new Show { ShowTime = DateTime.Now } }, new ShowActor { Show = new Show { ShowTime = DateTime.Now } }, new ShowActor { Show = new Show { ShowTime = DateTime.Now } } } }); // Act var objectResult = _showService .AddShow(_showDomainModel) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Assert Assert.IsNotNull(objectResult); Assert.AreEqual(expectedBool, objectResult.isSuccessful); Assert.AreEqual(expectedMessage, objectResult.ErrorMessage); }
public void Add_Show_NotSuccessful_Actors_Does_Not_Exist_In_DataBase() { // Arrange string expectedMessage = Messages.CANNOT_CREATE_SHOW_ACTORS_DOES_NOT_EXIST; bool expectedBool = false; _showDomainModel = new ShowDomainModel { ShowTime = DateTime.Now.AddMinutes(5), ActorsList = new List <ActorDomainModel> { new ActorDomainModel { Id = 1 }, new ActorDomainModel { Id = 2 } } }; _mockAuditoriumsRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Auditorium()); _mockPiecesRepository .Setup(x => x.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(new Piece()); _mockShowsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Show>() { new Show { ShowTime = DateTime.Now.AddHours(5) } }); _mockActorsRepository .Setup(x => x.GetAllAsync()) .ReturnsAsync(new List <Actor>() { new Actor { Id = 2 }, new Actor { Id = 3 } }); // Act var objectResult = _showService .AddShow(_showDomainModel) .ConfigureAwait(false) .GetAwaiter() .GetResult(); // Assert Assert.IsNotNull(objectResult); Assert.AreEqual(expectedMessage, objectResult.ErrorMessage); Assert.IsInstanceOfType(objectResult, typeof(ShowResultModel)); Assert.AreEqual(expectedBool, objectResult.isSuccessful); }