public static PhotoEntity ToPhotoEntity(NewPhotoViewModel photoViewModel)
        {
            var photoEntity = ToPhotoEntity((PhotoViewModel)photoViewModel);

            photoEntity.Image     = photoViewModel.Image.Data;
            photoEntity.ImageType = photoViewModel.Image.Type;

            return(photoEntity);
        }
Example #2
0
        public ActionResult Create([Bind(Include = "AlbumId, Title, Image, CreationDate")] NewPhotoViewModel photoViewModel)
        {
            if (ModelState.IsValid)
            {
                _photoRepository.AddPhoto(photoViewModel.ToPhotoEntity());
                return(RedirectToAlbumBrowse(photoViewModel.AlbumId));
            }

            photoViewModel.Albums = CreateAlbumSelectList();
            return(View(photoViewModel));
        }
Example #3
0
        public ActionResult Add(int?albumId)
        {
            if (albumId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var photo = new NewPhotoViewModel {
                AlbumId = (int)albumId
            };

            return(PartialView(photo));
        }
Example #4
0
        public void AddPostActionDoesNotAddPhotoIfModelErrorAndRedirectsToAlbumView()
        {
            var photo = new NewPhotoViewModel();

            _photosController.ModelState.AddModelError("Title", "NewErrorMsg");
            var result = _photosController.Add(photo, _file) as RedirectToRouteResult;

            _photoRepository.DidNotReceive().Insert(Arg.Any <Photo>());
            _unitOfWork.DidNotReceive().Save();

            Assert.IsNotNull(result);
            Assert.AreEqual("Album", result.RouteValues["action"]);
        }
Example #5
0
        public void AddPostActionRepoReceivedInsertCallAndUnitOfWorkSaveCall()
        {
            var photo = new NewPhotoViewModel();

            using (var stream = new MemoryStream())
                using (var bmp = new Bitmap(1, 1))
                {
                    var graphics = Graphics.FromImage(bmp);
                    graphics.FillRectangle(Brushes.Black, 0, 0, 1, 1);
                    bmp.Save(stream, ImageFormat.Jpeg);
                    //Set up correct file format
                    _file.FileName.Returns("testFileName.jpeg");
                    _file.InputStream.Returns(stream);
                    _file.ContentLength.Returns((int)stream.Length);

                    _photosController.Add(photo, _file);
                    _photoRepository.Received().Insert(Arg.Any <Photo>());
                    _unitOfWork.Received().Save();
                }
        }
Example #6
0
        public void AddPostActionDoesNotAddPhotoIfFileHasWrongFormat()
        {
            var photo = new NewPhotoViewModel();

            using (var stream = new MemoryStream())
                using (var bmp = new Bitmap(1, 1))
                {
                    var graphics = Graphics.FromImage(bmp);
                    graphics.FillRectangle(Brushes.Black, 0, 0, 1, 1);
                    bmp.Save(stream, ImageFormat.Tiff);
                    //Set up wrong file format
                    _file.FileName.Returns("someFileName.tiff");
                    _file.InputStream.Returns(stream);
                    _file.ContentLength.Returns((int)stream.Length);

                    _photosController.Add(photo, _file);
                    _photoRepository.DidNotReceive().Insert(Arg.Any <Photo>());
                    _unitOfWork.DidNotReceive().Save();
                }
        }
Example #7
0
        public ActionResult Add(NewPhotoViewModel model, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid || file == null)
            {
                TempData["result"] = ErrorMsg;
                return(RedirectToAction("Album", "Albums",
                                        new { id = model.AlbumId }));
            }

            var extensions = new List <string>()
            {
                ".jpg", ".jpeg", ".png", ".bmp", ".gif"
            };

            try
            {
                var extension = Path.GetExtension(file.FileName)?.ToLower();
                if (!extensions.Contains(extension))
                {
                    TempData["result"] = "Only jpg, jpeg, gif, png and bmp formats are acceptable.";
                    return(RedirectToAction("Album", "Albums",
                                            new { id = model.AlbumId }));
                }

                var photo = _mapper.Map <Photo>(model);
                photo.FileName = model.PhotoTitle +
                                 DateTime.UtcNow.ToString("s") + extension;
                photo.Content = new byte[file.ContentLength];
                file.InputStream.Read(photo.Content, 0, file.ContentLength);
                _photoRepository.Insert(photo);
                _unitOfWork.Save();
                TempData["result"] = $"Photo {model.PhotoTitle} has been saved.";
            }
            catch (Exception e)
            {
                TempData["result"] = ErrorMsg + e.Message;
            }

            return(RedirectToAction("Album", "Albums",
                                    new { id = model.AlbumId }));
        }
Example #8
0
 public static PhotoEntity ToPhotoEntity(this NewPhotoViewModel photoViewModel)
 {
     return(PhotoConverter.ToPhotoEntity(photoViewModel));
 }