Exemplo n.º 1
0
        public ActionResult Update(UpdatePhotoModel model, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var userId = User.Identity.GetUserId();

            var photo = Mapper.Map <UpdatePhotoModel, Photo>(model);

            photo.UserId = userId;

            file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                if (Path.GetExtension(file.FileName).ToLower() == ".jpg" ||
                    Path.GetExtension(file.FileName).ToLower() == ".jpeg")
                {
                    if (file.ContentLength <= 1048576)
                    {
                        _service.Update(photo, file);
                        return(RedirectToAction("UserPhotos"));
                    }
                    else
                    {
                        ModelState.AddModelError("Size", "Current image takes more than 1 MB");
                    }
                }
                else
                {
                    ModelState.AddModelError("IsAnImage", "Current file isn’t an image");
                }
            }
            else
            {
                _service.Update(photo);
                return(RedirectToAction("UserPhotos"));
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Update(int id, UpdatePhotoModel model)
        {
            var photo = await _photoService.GetEntityByIdAsync(id);

            if (photo == null)
            {
                return(NotFound());
            }

            if (!(await _authorizationService.AuthorizeAsync(User, photo.Album, Policies.AlbumOwnerOrMod)).Succeeded)
            {
                return(Forbid());
            }

            Location location = null;

            if (model.LocationId.HasValue)
            {
                location = await _locationService.GetEntityByIdAsync(model.LocationId.Value);

                if (location == null)
                {
                    ModelState.AddModelError(nameof(model.LocationId), "Invalid locationId");
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            photo.Author       = model.Author;
            photo.LocationName = model.LocationName;
            photo.Location     = location;
            photo.Title        = model.Title;
            photo.Description  = model.Description;
            photo.PhotoDate    = model.PhotoDate;

            await _photoService.UpdateAsync(photo);

            return(NoContent());
        }