Beispiel #1
0
 public IActionResult Create(ListingNewModel listingModel)
 {
     ViewData["Subcategoryid"] = new SelectList(_context.Subcategory, "Id", "Name");
     ViewData["Userid"]        = new SelectList(_context.Users, "Id", "UserName");
     if (TempData.ContainsKey("PictureError"))
     {
         ViewData["PictureError"] = TempData["PictureError"];
     }
     ModelState.Clear();
     return(View(listingModel));
 }
Beispiel #2
0
        public void Create_UserListing_OpenCreationView()
        {
            // Arrange
            var             listingsController = this.CreateListingsController(true);
            ListingNewModel listingModel       = new ListingNewModel();

            var result     = listingsController.Create(listingModel);
            var viewResult = (ViewResult)result;

            Assert.IsType <ViewResult>(result);
        }
Beispiel #3
0
        public async Task CreatePost_NewListingModel_CreateAndRedirectToView()
        {
            // Arrange
            var             listingsController = this.CreateListingsController(true);
            ListingNewModel newListing         = new ListingNewModel()
            {
                Subcategoryid = 0,
                Name          = "Great bike",
                Description   = "Greatest bike there is",
                Price         = 500.0,
            };


            var result = await listingsController.CreatePost(newListing);

            // Assert
            Assert.IsType <RedirectToActionResult>(result);
        }
Beispiel #4
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));
        }