public IHttpActionResult UploadProductionProgram(int productionId) { var dbcontext = new BroadwayBuilderContext(); var productionService = new ProductionService(dbcontext); // Try to upload pdf and save to server filesystem try { // Get the full request of the current http request var httpRequest = HttpContext.Current.Request; var fileCollection = httpRequest.Files; var fileValidator = new FileValidator(); int MaxContentLength = 1024 * 1024 * 1; int maxFileCount = 1; var extensions = new List <string>() { ".pdf" }; // Validate files for valid extension, and size var validationResult = fileValidator.ValidateFiles(fileCollection, extensions, MaxContentLength, maxFileCount); if (!validationResult.ValidationSuccessful) { var errorMessage = string.Join("\n", validationResult.Reasons); return(BadRequest(errorMessage)); } // Send file to be saved to server foreach (string filename in fileCollection) { HttpPostedFileBase putFile = new HttpPostedFileWrapper(fileCollection[filename]); productionService.SaveProgram(productionId, putFile); } return(Ok("Pdf Uploaded")); } catch (Exception e) { // Todo: log error // Todo: add proper error handling in production service return(BadRequest(e.Message)); } }
public void ProductionService_UploadProgram_Pass() { // Arrange var dbcontext = new BroadwayBuilderContext(); var theaterService = new TheaterService(dbcontext); var theater = new Theater() { TheaterName = "Some Theater 1", StreetAddress = "Theater St", State = "CA", City = "LA", CompanyName = "Regal", Country = "US", PhoneNumber = "123456789" }; theaterService.CreateTheater(theater); dbcontext.SaveChanges(); var production = new Production() { ProductionName = "The Lion King 2", DirectorFirstName = "Jane", DirectorLastName = "Doe", Street = "Anahiem", City = "Long Beach", StateProvince = "California", Country = "United States", Zipcode = "919293", TheaterID = theater.TheaterID }; var productionService = new ProductionService(dbcontext); productionService.CreateProduction(production); dbcontext.SaveChanges(); var mockedPostedPdfFile = new MockPostedFile("pdf", 5000000, "productionProgramTestFile.pdf"); var extension = Path.GetExtension(mockedPostedPdfFile.FileName); var currentDirectory = ConfigurationManager.AppSettings["FileDir"]; var dir = Path.Combine(currentDirectory, "Programs/"); var subdir = Path.Combine(dir, $"Production{production.ProductionID}/"); var filePath = Path.Combine(subdir, $"{production.ProductionID}{extension}"); var expected = true; var actual = false; // Act productionService.SaveProgram(production.ProductionID, mockedPostedPdfFile); if (File.Exists(filePath)) { actual = true; } // Assert productionService.DeleteProduction(production.ProductionID); dbcontext.SaveChanges(); theaterService.DeleteTheater(theater); dbcontext.SaveChanges(); File.Delete(filePath); Directory.Delete(subdir); Assert.AreEqual(expected, actual); }