コード例 #1
0
        public IHttpActionResult uploadPhoto(int productionId)
        {
            var dbcontext         = new BroadwayBuilderContext();
            var productionService = new ProductionService(dbcontext);

            //try to upload pdf and save to server filesystem
            try
            {
                //get the content, headers, etc the full request of the current http request
                var httpRequest    = HttpContext.Current.Request;
                var fileCollection = httpRequest.Files;

                var fileValidator = new FileValidator();

                int MaxContentLength = 1 * 1024 * 1024 * 5; //Size = 5 MB
                var validExtensions  = new List <string>()
                {
                    ".jpg"
                };
                int maxFileCount = 5;

                var validationResult = fileValidator.ValidateFiles(fileCollection, validExtensions, MaxContentLength, maxFileCount);

                if (!validationResult.ValidationSuccessful)
                {
                    var errorMessage = string.Join("\n", validationResult.Reasons);
                    return(BadRequest(errorMessage));
                }

                // Used for loop since foreach did not handle cycling through multiple files well.
                for (int i = 0; i < httpRequest.Files.Count; i++)
                {
                    // Grab current file of the request
                    HttpPostedFileBase putFile = new HttpPostedFileWrapper(httpRequest.Files[i]);

                    // Send to production service where functinality to save the file is
                    productionService.SavePhoto(productionId, putFile);
                }

                return(Ok("Photo Uploaded"));
            }

            catch (Exception ex) // Todo: log error
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #2
0
        public void ProductionService_SavePhotos_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("jpg", 5000000, "productionPhotoTestFile.jpg");

            var extension = Path.GetExtension(mockedPostedPdfFile.FileName);

            var currentDirectory = ConfigurationManager.AppSettings["FileDir"];

            var dir      = Path.Combine(currentDirectory, "Photos/");
            var subdir   = Path.Combine(dir, $"Production{production.ProductionID}/");
            var filePath = Path.Combine(subdir, $"{production.ProductionID}-0{extension}");


            var expected = true;
            var actual   = false;

            // Act
            productionService.SavePhoto(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);
        }
コード例 #3
0
        public void ProductionController_GetPhotos_Pass()
        {
            // Arrange
            var dbcontext      = new BroadwayBuilderContext();
            var theaterService = new TheaterService(dbcontext);

            var theater = new Theater()
            {
                TheaterName   = "Some Theater",
                StreetAddress = "Theater St",
                State         = "CA",
                City          = "LA",
                CompanyName   = "Regal",
                Country       = "US",
                PhoneNumber   = "123456789"
            };

            theaterService.CreateTheater(theater);
            dbcontext.SaveChanges();

            var productionService = new ProductionService(dbcontext);

            var production = new Production
            {
                ProductionName    = "The Pajama Game",
                DirectorFirstName = "Doris",
                DirectorLastName  = "Day",
                City          = "San Diego",
                StateProvince = "California",
                Country       = "U.S",
                TheaterID     = theater.TheaterID,
                Street        = "123 Sesame St",
                Zipcode       = "91911"
            };

            productionService.CreateProduction(production);
            dbcontext.SaveChanges();

            var productionController = new ProductionController();

            var mockPostedPdfFile1 = new MockPostedFile("jpg", 5000000, "1.jpg");
            var mockPostedPdfFile2 = new MockPostedFile("jpg", 5000000, "2.jpg");

            productionService.SavePhoto(production.ProductionID, mockPostedPdfFile1);
            productionService.SavePhoto(production.ProductionID, mockPostedPdfFile2);

            var currentDirectory = ConfigurationManager.AppSettings["FileDir"];

            var dir    = Path.Combine(currentDirectory, "Photos/");
            var subdir = Path.Combine(dir, $"Production{production.ProductionID}/");

            // Act
            var actionResult = productionController.GetPhotos(production.ProductionID);

            // Need access to contet of the response
            var response = actionResult as OkNegotiatedContentResult <List <string> >;

            var photoUrls = response.Content;

            var expected = true;
            var actual   = false;

            if (photoUrls.Count == 2)
            {
                actual = true;
            }

            productionService.DeleteProduction(production.ProductionID);
            dbcontext.SaveChanges();
            theaterService.DeleteTheater(theater);
            dbcontext.SaveChanges();
            Directory.Delete(subdir, true);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(expected, actual);
        }