示例#1
0
        public ActionResult Create(PhotoCreateViewModel photo, HttpPostedFileBase image)
        {
            photo.CreatedDate = DateTime.Today;
            if (!ModelState.IsValid)
            {
                var types = photoAPI.GetAllPhotoTypes();
                photo.PhotoTypes = new SelectList(types, "PhotoTypeID", "Description");
                return(View(photo));
            }
            else
            {
                photo.CreatedDate = DateTime.Today;
                if (image != null)
                {
                    var photoFile = new byte[image.ContentLength];
                    image.InputStream.Read(photoFile, 0, image.ContentLength);
                    photoAPI.AddPhoto(photo.ToDataModel(), photoFile, image.ContentType);
                }
                else
                {
                    photoAPI.AddPhoto(photo.ToDataModel());
                }

                return(RedirectToAction("Index"));
            }
        }
示例#2
0
        public IActionResult Create([Bind("Id,Title,TagStr,Photo")] PhotoCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var uniqueFileName = ProcessUploadedFile(model);

                #region Add Photo

                var newPhoto = new Photo
                {
                    Title     = model.Title,
                    PhotoPath = uniqueFileName
                };
                photoService.Add(newPhoto);

                #endregion

                var tags = model.TagStr.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                foreach (var tag in tags)
                {
                    var newTag = new Tag
                    {
                        Name    = tag,
                        PhotoId = newPhoto.Id
                    };
                    tagService.Add(newTag);
                }
                return(RedirectToAction("Details", new { id = newPhoto.Id }));
            }

            return(View());
        }
示例#3
0
        public ActionResult Create()
        {
            var newPhoto = new PhotoCreateViewModel();
            var types    = photoAPI.GetAllPhotoTypes();

            newPhoto.PhotoTypes = new SelectList(types, "PhotoTypeID", "Description");

            return(View(newPhoto));
        }
示例#4
0
        public async Task <IActionResult> Create([Bind("Id,Date,Path,UserId,Photo")] PhotoCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;
                var    currentUser    = await GetCurrentUserAsync();

                string filePath = null;

                if (model.Photo != null)
                {
                    // The image must be uploaded to the images folder in wwwroot
                    // To get the path of the wwwroot folder we are using the inject
                    // HostingEnvironment service provided by ASP.NET Core
                    string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                    // To make sure the file name is unique we are appending a new
                    // GUID value and and an underscore to the file name
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                    filePath       = Path.Combine(uploadsFolder, uniqueFileName);
                    // Use CopyTo() method provided by IFormFile interface to
                    // copy the file to wwwroot/images folder
                    using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        model.Photo.CopyTo(fileStream);
                    }
                }

                Photo photo = new Photo
                {
                    Path   = uniqueFileName,
                    UserId = currentUser.Id
                };

                try
                {
                    using (ExifReader reader = new ExifReader(filePath))
                    {
                        DateTime dateTime;
                        if (reader.GetTagValue <DateTime>(ExifTags.DateTimeDigitized, out dateTime))
                        {
                            photo.Date = dateTime;
                        }
                    }
                }
                catch (ExifLibException)
                {
                }

                _context.Add(photo);
                await _context.SaveChangesAsync();

                // Redirect to edit view for photo being created
                return(RedirectToAction("Edit", new { id = photo.Id }));
            }
            return(View(model));
        }
示例#5
0
        public ActionResult Create(PhotoCreateViewModel photo)
        {
            if (ModelState.IsValid)
            {
                var photoService = new PhotosService();
                photoService.Add(new PhotoRechercheViewModel
                {
                    Photos = photo.ImageFiles
                });
                return(RedirectToAction("Index"));
            }

            return(View(photo));
        }
示例#6
0
        private string ProcessUploadedFile(PhotoCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                string uploadsFolder = Path.Combine(hostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using var fileStream = new FileStream(filePath, FileMode.Create);
                model.Photo.CopyTo(fileStream);
            }

            return(uniqueFileName);
        }
        public IActionResult Create()
        {
            var categories = _categoryRepo.GetCategories().Select(cat => new SelectListItem
            {
                Text  = cat.Name,
                Value = cat.Name,
            }).ToList();

            var model = new PhotoCreateViewModel
            {
                Categories = categories
            };

            return(View(model));
        }
        public void ReturnViewWhenInvalidModelState()
        {
            sut.ModelState.AddModelError("x", "Test Error");

            var viewModel = new PhotoCreateViewModel
            {
                Title = "Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title "
            };

            IActionResult result = sut.Create(viewModel);

            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            var model = Assert.IsType <PhotoCreateViewModel>(viewResult.Model);

            Assert.Equal(viewModel.Title, model.Title);
        }
        public async Task <IActionResult> Create(PhotoCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Image == null)
                {
                    _clientNotification.AddToastNotification("You Have to Upload An Image!", NotificationHelper.NotificationType.error, new ToastNotificationOption
                    {
                        NewestOnTop       = true,
                        CloseButton       = true,
                        PositionClass     = "toast-top-right",
                        PreventDuplicates = true
                    });
                    return(View(model));
                }

                if (model.Image.Length > _options.MaxBytes)
                {
                    _clientNotification.AddToastNotification("Image File size Exceeded", NotificationHelper.NotificationType.error, new ToastNotificationOption
                    {
                        NewestOnTop       = true,
                        CloseButton       = true,
                        PositionClass     = "toast-top-right",
                        PreventDuplicates = true
                    });
                    return(View(model));
                }

                if (!_options.IsSupported(model.Image.FileName))
                {
                    _clientNotification.AddToastNotification("Invalid File Type", NotificationHelper.NotificationType.error, new ToastNotificationOption
                    {
                        NewestOnTop       = true,
                        CloseButton       = true,
                        PositionClass     = "toast-top-right",
                        PreventDuplicates = true
                    });
                }
                else
                {
                    var photo = new Photo
                    {
                        Id            = Guid.NewGuid().ToString().Replace("-", string.Empty),
                        Name          = model.Name,
                        Description   = model.Description,
                        Category      = model.Category,
                        PhotoUrl      = _fileManager.SaveImage(model.Image),
                        FaceBookLink  = model.FacebookLink,
                        InstagramLink = model.InstagramLink,
                        TwitterLink   = model.TwitterLink
                    };
                    await _photoRepo.AddPhoto(photo);

                    await _photoRepo.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            _clientNotification.AddToastNotification("You Have Errors", NotificationHelper.NotificationType.error, new ToastNotificationOption
            {
                NewestOnTop       = true,
                CloseButton       = true,
                PositionClass     = "toast-top-right",
                PreventDuplicates = true
            });
            return(View(model));
        }