public IHttpActionResult GetProductions(DateTime?currentDate = null, DateTime?previousDate = null, int?theaterID = null, int pageNum = 1, int pageSize = 10) { try { using (var dbcontext = new BroadwayBuilderContext()) { var productionService = new ProductionService(dbcontext); if (previousDate != null || currentDate == null) { var productionResponses = productionService.GetProductionsByPreviousDate((DateTime)previousDate, theaterID, pageNum, pageSize) .Select(production => new ProductionResponseModel() { DirectorFirstName = production.DirectorFirstName, DirectorLastName = production.DirectorLastName, ProductionID = production.ProductionID, ProductionName = production.ProductionName, StateProvince = production.StateProvince, Street = production.Street, TheaterID = production.TheaterID, Zipcode = production.Zipcode, City = production.City, Country = production.Country, DateTimes = production.ProductionDateTime.Select(datetime => new ProductionDateTimeResponseModel() { Date = datetime.Date, ProductionDateTimeId = datetime.ProductionDateTimeId, Time = datetime.Time }).ToList() }).ToList(); return(Ok(productionResponses)); } else { var productionResponses = productionService.GetProductionsByCurrentAndFutureDate((DateTime)currentDate, theaterID, pageNum, pageSize) .Select(production => new ProductionResponseModel() { City = production.City, DirectorFirstName = production.DirectorFirstName, DirectorLastName = production.DirectorLastName, Country = production.Country, ProductionID = production.ProductionID, ProductionName = production.ProductionName, StateProvince = production.StateProvince, Street = production.Street, TheaterID = production.TheaterID, Zipcode = production.Zipcode, DateTimes = production.ProductionDateTime.Select(datetime => new ProductionDateTimeResponseModel() { Date = datetime.Date, ProductionDateTimeId = datetime.ProductionDateTimeId, Time = datetime.Time }).ToList() }).ToList(); return(Ok(productionResponses)); } } } catch (DbUpdateException e) // Todo: Log Error { return(Content((HttpStatusCode)500, e.Message)); } // Todo: Add proper exception handling for getting a production catch (Exception e) // Todo: Log Error Might not catch this since okay with returning [] emtpy list { return(BadRequest()); } }
public void ProductionService_GetProductionsByCurrentDate_Pass() { // Arrange // Arrange var dbcontext = new BroadwayBuilderContext(); var theaterService = new TheaterService(dbcontext); var theater = new Theater() { TheaterName = "The Language", StreetAddress = "Pantene", State = "CA", City = "LA", CompanyName = "123 Sesame St", Country = "US", PhoneNumber = "123456789" }; theaterService.CreateTheater(theater); dbcontext.SaveChanges(); var productionService = new ProductionService(dbcontext); var production1 = new Production() { ProductionName = "The Lion King 14", DirectorFirstName = "Joan", DirectorLastName = "Doe", Street = "123 Anahiem St", City = "Long Beach", StateProvince = "California", Country = "United States", Zipcode = "919293", TheaterID = theater.TheaterID }; productionService.CreateProduction(production1); dbcontext.SaveChanges(); var production2 = new Production() { ProductionName = "The Lion King 15", DirectorFirstName = "Joan", DirectorLastName = "Doe", Street = "123 Anahiem St", City = "Long Beach", StateProvince = "California", Country = "United States", Zipcode = "919293", TheaterID = theater.TheaterID }; productionService.CreateProduction(production2); dbcontext.SaveChanges(); var productionDateTime1 = new ProductionDateTime() { Date = DateTime.Parse("3/23/2019 3:22:29 PM"), Time = TimeSpan.Parse("10:30:00"), ProductionID = production1.ProductionID }; productionService.CreateProductionDateTime(production1.ProductionID, productionDateTime1); dbcontext.SaveChanges(); var productionDateTime2 = new ProductionDateTime() { Date = DateTime.Parse("3/29/2019 3:22:29 PM"), Time = TimeSpan.Parse("5:30:00"), ProductionID = production2.ProductionID }; productionService.CreateProductionDateTime(production2.ProductionID, productionDateTime2); dbcontext.SaveChanges(); var expected = true; var actual = false; // Act var readProductionsList = productionService.GetProductionsByCurrentAndFutureDate(new DateTime(2019, 3, 1), null, 1, 10); // Theater id is meant to be null if (readProductionsList != null) { actual = true; } // Assert productionService.DeleteProductionDateTime(productionDateTime2); dbcontext.SaveChanges(); productionService.DeleteProductionDateTime(productionDateTime1); dbcontext.SaveChanges(); productionService.DeleteProduction(production1.ProductionID); dbcontext.SaveChanges(); productionService.DeleteProduction(production2.ProductionID); dbcontext.SaveChanges(); theaterService.DeleteTheater(theater); dbcontext.SaveChanges(); Assert.AreEqual(expected, actual); }
public IHttpActionResult GetProductions(DateTime?currentDate = null, DateTime?previousDate = null, int?theaterID = null) { try { using (var dbcontext = new BroadwayBuilderContext()) { var productionService = new ProductionService(dbcontext); try { if (previousDate != null) { var productionResponses = productionService.GetProductionsByPreviousDate((DateTime)previousDate, theaterID) .Select(production => new ProductionResponseModel() { DirectorFirstName = production.DirectorFirstName, DirectorLastName = production.DirectorLastName, ProductionID = production.ProductionID, ProductionName = production.ProductionName, StateProvince = production.StateProvince, Street = production.Street, TheaterID = production.TheaterID, Zipcode = production.Zipcode, City = production.City, Country = production.Country, DateTimes = production.ProductionDateTime.Select(datetime => new ProductionDateTimeResponseModel() { Date = datetime.Date, ProductionDateTimeId = datetime.ProductionDateTimeId, Time = datetime.Time }).ToList() }).ToList(); return(Ok(productionResponses)); } else if (currentDate != null) { var productionResponses = productionService.GetProductionsByCurrentAndFutureDate((DateTime)currentDate) .Select(production => new ProductionResponseModel() { City = production.City, DirectorFirstName = production.DirectorFirstName, DirectorLastName = production.DirectorLastName, Country = production.Country, ProductionID = production.ProductionID, ProductionName = production.ProductionName, StateProvince = production.StateProvince, Street = production.Street, TheaterID = production.TheaterID, Zipcode = production.Zipcode, DateTimes = production.ProductionDateTime.Select(datetime => new ProductionDateTimeResponseModel() { Date = datetime.Date, ProductionDateTimeId = datetime.ProductionDateTimeId, Time = datetime.Time }).ToList() }).ToList(); return(Ok(productionResponses)); } // none of the if conditions were met therfore... return(BadRequest("PreviousDate and Current date were both null")); } // Todo: Add proper exception handling for getting a production catch (Exception e) { return(BadRequest()); } } } catch (Exception e) // Todo: Catch a SqlException ... or Sqlconnection exception? { return(BadRequest("Something big went bad!")); } }