コード例 #1
0
        public async Task GetPicture_Picture_ShouldReturnPictureFileResult()
        {
            // Arrange
            var listingPicturesController = this.CreateListingPicturesController();

            var pic = new ListingPictures()
            {
                PictureId   = 1,
                FileName    = "TEST_PICTURE.png",
                ContentType = "image/png"
            };

            mockadvert_siteContext.ListingPictures.Add(pic);
            mockadvert_siteContext.SaveChanges();

            // Act
            var result = await listingPicturesController.GetPicture(pic.PictureId);

            // Assert
            Assert.IsType <PhysicalFileResult>(result);
            var fileResult = (PhysicalFileResult)result;

            Assert.Equal(pic.FileName, Path.GetFileName(fileResult.FileName));
            Assert.Equal(pic.ContentType, fileResult.ContentType);
        }
コード例 #2
0
        public async Task <IActionResult> CreatePost([Bind("Subcategoryid,Name,Description,Price,GoogleLatitude,GoogleLongitude,GoogleRadius,ListingPictures")] ListingNewModel newListing)
        {
            if (ModelState.IsValid)
            {
                Listings listings = new Listings()
                {
                    Subcategoryid   = newListing.Subcategoryid,
                    Name            = newListing.Name,
                    Description     = newListing.Description,
                    Price           = newListing.Price,
                    GoogleLatitude  = newListing.GoogleLatitude,
                    GoogleLongitude = newListing.GoogleLongitude,
                    GoogleRadius    = newListing.GoogleRadius * 1000
                };

                listings.Userid = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

                listings.Date     = DateTime.Now;
                listings.Display  = 1;
                listings.Verified = 0;

                /*
                 * listings.GoogleLongitude = 0;// newListing.GoogleLongitude;
                 * listings.GoogleLatitude = 0;// newListing.GoogleLatitude;
                 * listings.GoogleRadius = 10000;// newListing.GoogleRadius;
                 */
                _context.Add(listings);
                _context.SaveChanges();


                if (newListing.ListingPictures != null && newListing.ListingPictures.Count() > 4) //jei nuotrauku daugiau nei 4 atmetam
                {
                    TempData["PictureError"] = "Nuotraukų negali būti daugiau nei 4!";
                    return(RedirectToAction(nameof(Create), newListing));
                }

                if (newListing.ListingPictures != null)
                {
                    foreach (var picture in newListing.ListingPictures)
                    {
                        if (picture.Length > ImageMaximumBytes) //jei dydid didesnis uz 10MB atmeta
                        {
                            TempData["PictureError"] = "Nuotraukos dydis negali būti didesnis nei 10Mb!";
                            return(RedirectToAction(nameof(Create), newListing));
                        }

                        if (!IsImage(picture))
                        {
                            TempData["PictureError"] = "Failas nėra nuotrauka!";
                            return(RedirectToAction(nameof(Create), newListing));
                        }
                    }

                    foreach (var picture in newListing.ListingPictures)
                    {
                        if (picture.Length > 0)
                        {
                            var pic = new ListingPictures {
                                ListingId = listings.Id, ContentType = picture.ContentType
                            };
                            _context.Add(pic);
                            await _context.SaveChangesAsync();

                            string[] filenameAndExtension = picture.FileName.Split('.');
                            filenameAndExtension[0] = pic.PictureId.ToString();

                            string fileName = filenameAndExtension[0] + "." + filenameAndExtension[1];

                            string path = "UserPictures" + "\\" + fileName;
                            path = Path.GetFullPath(path);

                            pic.FileName = fileName;
                            _context.ListingPictures.Update(pic);
                            using (var stream = new FileStream(path, FileMode.Create))
                            {
                                await picture.CopyToAsync(stream);
                            }
                        }
                    }
                }
                await _context.SaveChangesAsync();

                //TempData["Success"] = "Jūsų skelbimas bus patalpintas, kai administratorius jį patikrins";
                return(RedirectToAction(nameof(Index)));
            }

            ViewData["Subcategoryid"] = new SelectList(_context.Subcategory, "Id", "Id");
            ViewData["Userid"]        = new SelectList(_context.Users, "Id", "UserName");
            return(View(newListing));
        }