Ejemplo n.º 1
0
        public void Create_PostData_CheckWrongFormatImages()
        {
            var image1 = new Mock <HttpPostedFileBase>();

            image1.Setup(f => f.ContentLength).Returns(1);
            image1.Setup(f => f.FileName).Returns("myFileName1.asd");
            var image2 = new Mock <HttpPostedFileBase>();

            image2.Setup(f => f.ContentLength).Returns(1);
            image2.Setup(f => f.FileName).Returns("myFileName2.asd");
            List <HttpPostedFileBase> images = new List <HttpPostedFileBase>()
            {
                image1.Object, image2.Object
            };
            PlaceViewModels place =
                new PlaceViewModels()
            {
                Name = "ThisIsTestName", Description = "ThisIsTestDescription"
            };

            var result = _controller.Create(place, images);

            Assert.IsTrue(_controller.ViewData.ModelState.Count == 1);
            Assert.IsTrue(_controller.ViewData.ModelState.Keys.Contains("WrongFormatImages"));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(long id, [Bind(Exclude = "tracks")] PlaceViewModels plVModels, IEnumerable <HttpPostedFileBase> fileData)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var   config      = new MapperConfiguration(cfg => cfg.CreateMap <PlaceViewModels, Place>());
                    var   mapper      = config.CreateMapper();
                    Place placeDomain = _placeService.GetItem(id);

                    if (plVModels.IdTrack != 0)
                    {
                        if (!placeDomain.Tracks.Contains(_trackService.GetItem(plVModels.IdTrack)))
                        {
                            placeDomain.Tracks.Add(_trackService.GetItem(plVModels.IdTrack));
                        }
                    }
                    if (fileData != null)
                    {
                        fileData = fileData.Where(f => f != null);
                        foreach (var someFile in fileData)
                        {
                            System.IO.FileInfo file = new System.IO.FileInfo(someFile.FileName);
                            if (file.Extension == ".jpeg" || file.Extension == ".jpg" || file.Extension == ".png" || file.Extension == ".gif" || file.Extension == ".bmp")
                            {
                                string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
                                fname = fname + System.DateTime.Now.ToString("_ddMMyyhhmmssms") + file.Extension;
                                someFile.SaveAs(Server.MapPath("~/Content/Images" + fname));
                                Picture pic = new Picture
                                {
                                    Name = fname,
                                    Path = Server.MapPath("~/Content/Images" + fname),
                                };
                                placeDomain.Pictures.Add(pic);
                            }
                            else
                            {
                                ModelState.AddModelError("WrongFormatImages", "Разрешенные только следующие форматы изображений : jpeg, jpg, png, gif, bmp");
                                return(PartialView(plVModels));
                            }
                        }
                    }
                    placeDomain.Name        = plVModels.Name;
                    placeDomain.Description = plVModels.Description;
                    placeDomain.Latitude    = plVModels.Latitude;
                    placeDomain.Longitude   = plVModels.Longitude;

                    _placeService.Update(placeDomain);
                    _placeService.Save();
                    var returnId = _placeService.GetItemList().FirstOrDefault(track => track == placeDomain).Id;
                    return(Json(returnId));
                }
            }
            catch (Exception e)
            {
                return(View(plVModels));
            }
            return(PartialView(plVModels));
        }
Ejemplo n.º 3
0
        // GET: Place/Edit/5
        public ActionResult Edit(long id)
        {
            PlaceViewModels plvModels = new PlaceViewModels();
            var             config    = new MapperConfiguration(cfg => cfg.CreateMap <Place, PlaceViewModels>());
            var             mapper    = config.CreateMapper();

            plvModels = mapper.Map <Place, PlaceViewModels>(_placeService.GetItem(id));
            return(PartialView(plvModels));
        }
Ejemplo n.º 4
0
 public IActionResult Create(PlaceViewModels placeModel)
 {
     if (ModelState.IsValid)
     {
         // var currentUser = await _userManager.(HttpContext.User);
         var placeId = _placeService.AddPlace(placeModel, _userManager.GetUserId(User));
         return(RedirectToAction(nameof(Index)));
     }
     return(View(placeModel));
 }
Ejemplo n.º 5
0
        public void Create_PostData_CheckWhithoutImages()
        {
            PlaceViewModels place =
                new PlaceViewModels()
            {
                Name = "ThisIsTestName", Description = "ThisIsTestDescription"
            };

            var result = _controller.Create(place, null);

            Assert.IsTrue(_controller.ViewData.ModelState.Count == 1);
            Assert.IsTrue(_controller.ViewData.ModelState.Keys.Contains("NoImage"));
        }
Ejemplo n.º 6
0
        // GET: Place/Create
        public ActionResult Create(long?IdTrack)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Json(3, JsonRequestBehavior.AllowGet));
            }
            PlaceViewModels plvModels = new PlaceViewModels();

            if (IdTrack != null && IdTrack is long)
            {
                plvModels.IdTrack = (long)IdTrack;
            }
            else
            {
                plvModels.IdTrack = 0;
            }
            return(PartialView(plvModels));
        }
Ejemplo n.º 7
0
        public int AddPlace(PlaceViewModels placeModel, string userId)
        {
            if (placeModel.MainPhoto == null)
            {
                throw new Exception("Main photo is not set");
            }

            var place = new Place()
            {
                Title       = placeModel.Title,
                Description = placeModel.Description,
                UserId      = userId
            };

            _context.Places.Add(place);
            _context.SaveChanges();

            if (placeModel.MainPhoto != null)
            {
                var photo = new Photo()
                {
                    FileName   = placeModel.MainPhoto.FileName,
                    FilePath   = $"images/{placeModel.MainPhoto.FileName}",
                    UploadDate = DateTime.Now,
                    IsMain     = true,
                    UserId     = userId,
                    PlaceId    = place.Id
                };
                _context.Photos.Add(photo);
                _context.SaveChanges();

                string path = Path.Combine(_environment.WebRootPath, $"images\\");

                _fileUploadService.Upload(path, placeModel.MainPhoto.FileName, placeModel.MainPhoto);
            }

            return(place.Id);
        }
Ejemplo n.º 8
0
        public PlaceViewModels GetPlace(int id)
        {
            var place = _context.Places
                        .Include(p => p.User)
                        .Include(p => p.Reviews)
                        .Include(p => p.Photos)
                        .FirstOrDefault(p => p.Id == id);

            if (place == null)
            {
                throw new Exception("Place does not found");
            }

            var placeModel = new PlaceViewModels()
            {
                Id          = place.Id,
                Title       = place.Title,
                Description = place.Description,
                UserId      = place.UserId,
                UserName    = place.User.UserName
            };

            var mainPhoto = place.Photos.FirstOrDefault(p => p.PlaceId == place.Id && p.IsMain);

            if (mainPhoto != null)
            {
                placeModel.MainPhotoPath = mainPhoto.FilePath;
            }

            if (place.Photos.Count > 0)
            {
                var photosList = new List <PhotoViewModels>();
                foreach (var photo in place.Photos)
                {
                    var photoItem = new PhotoViewModels()
                    {
                        Id         = photo.Id,
                        FileName   = photo.FileName,
                        FilePath   = photo.FilePath,
                        IsMain     = photo.IsMain,
                        UploadDate = photo.UploadDate,
                        PlaceId    = photo.PlaceId,
                        UserId     = photo.UserId,
                        UserName   = photo.User.UserName
                    };
                    photosList.Add(photoItem);
                }

                placeModel.Photos = photosList;
            }

            if (place.Reviews.Count() > 0)
            {
                var reviewsList = new List <ReviewViewModels>();
                foreach (var review in place.Reviews)
                {
                    var reviewItem = new ReviewViewModels()
                    {
                        Id       = review.Id,
                        Text     = review.Text,
                        Date     = review.Date,
                        Rating   = review.Rating,
                        UserId   = review.UserId,
                        UserName = review.User.UserName,
                        PlaceId  = review.PlaceId
                    };
                    reviewsList.Add(reviewItem);
                }

                placeModel.Reviews = reviewsList.OrderByDescending(r => r.Date).ToList();
                placeModel.Rating  = Math.Round(place.Reviews.Sum(r => r.Rating) / (double)place.Reviews.Count, 1);
            }
            else
            {
                placeModel.Rating = 0;
            }

            return(placeModel);
        }
Ejemplo n.º 9
0
        public ActionResult Create([Bind(Exclude = "tracks")] PlaceViewModels plVModel,
                                   IEnumerable <HttpPostedFileBase> fileData)
        {
            try
            {
                if (!User.Identity.IsAuthenticated)
                {
                    return(Json(3, JsonRequestBehavior.AllowGet));
                }
                if (ModelState.IsValid)
                {
                    var config     = new MapperConfiguration(cfg => cfg.CreateMap <PlaceViewModels, Place>());
                    var mapper     = config.CreateMapper();
                    var placeModel = mapper.Map <PlaceViewModels, Place>(plVModel);
                    if (plVModel.IdTrack != 0)
                    {
                        placeModel.Tracks.Add(_trackService.GetItem(plVModel.IdTrack));
                    }
                    if (fileData != null)
                    {
                        fileData = fileData.Where(f => f != null);
                        foreach (var someFile in fileData)
                        {
                            System.IO.FileInfo file = new System.IO.FileInfo(someFile.FileName);
                            if (file.Extension == ".jpeg" || file.Extension == ".jpg" || file.Extension == ".png" ||
                                file.Extension == ".gif" || file.Extension == ".bmp")
                            {
                                string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
                                fname = fname + System.DateTime.Now.ToString("_ddMMyyhhmmssms") + file.Extension;
                                someFile.SaveAs(Server.MapPath("../Content/Images/" + fname));
                                Picture pic = new Picture
                                {
                                    Name = fname,
                                    Path = Server.MapPath("../Content/Images/" + fname),
                                };
                                placeModel.Pictures.Add(pic);
                            }
                            else
                            {
                                ModelState.AddModelError("WrongFormatImages",
                                                         "Разрешенные только следующие форматы изображений : jpeg, jpg, png, gif, bmp");
                                return(PartialView(plVModel));
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("NoImage", "Не выбрано не одного фото");
                        return(PartialView(plVModel));
                    }

                    _placeService.Create(placeModel);
                    _placeService.Save();
                    var id = _placeService.GetItemList().FirstOrDefault(track => track == placeModel).Id;
                    return(Json(id));
                }
            }
            catch
            {
                return(PartialView(plVModel));
            }
            return(PartialView(plVModel));
        }