public IActionResult EditPicture([FromBody] PictureChange changeData)
        {
            if (userGet.HaveUser(HttpContext))
            {
                User    user    = userGet.GetUser(HttpContext);
                Picture picture = pictureRepository.GetPicture(changeData.Id);
                if (picture == null)
                {
                    return(BadRequest("Not existing picture!"));
                }
                if (picture.Owner == user)
                {
                    changeData.Owner = user;
                    Success result = ValidateAccess(changeData, picture);
                    switch (result)
                    {
                    case Success.Successfull:
                        pictureRepository.ChangePictureData(changeData);
                        return(Ok());

                    case Success.FailedByWrongAccessFolder:
                        return(BadRequest("Wrong access picture for " + picture.Folder.FolderName + " folder!"));

                    case Success.FailedByWrongAccessNewFolder:
                        return(BadRequest("Wrong access picture for " + changeData.FolderName + " folder!"));

                    case Success.FailedByNotExistFolderName:
                        return(BadRequest("Don't have " + changeData.FolderName + " folder"));
                    }
                }
                return(BadRequest("Not your picture!"));
            }
            return(Unauthorized());
        }
Beispiel #2
0
        public bool ChangePictureData(PictureChange changeData)
        {
            Picture picture = context.Pictures.Find(changeData.Id);

            if (picture == null)
            {
                return(false);
            }
            else
            {
                if (changeData.FolderName != null)
                {
                    picture.Folder = context.Folders.Where(a => a.Owner == changeData.Owner && a.FolderName == changeData.FolderName).SingleOrDefault();
                }
                if (changeData.Access != null)
                {
                    picture.Access = (AccessType)changeData.Access;
                }
                if (changeData.Description != null)
                {
                    picture.Description = changeData.Description;
                }
                context.SaveChanges();
            }
            return(true);
        }
 private Success ValidateAccess(PictureChange changeData, Picture picture)
 {
     if (changeData.FolderName != null && changeData.Access != null)
     {
         Folder newFolder = folderRepository.GetFolder(picture.Owner, changeData.FolderName);
         if (newFolder == null)
         {
             return(Success.FailedByNotExistFolderName);
         }
         else if (!(changeData.Access <= newFolder.Access))
         {
             return(Success.FailedByWrongAccessNewFolder);
         }
         else
         {
             return(Success.Successfull);
         }
     }
     else if (changeData.FolderName != null && changeData.Access == null)
     {
         Folder newFolder = folderRepository.GetFolder(picture.Owner, changeData.FolderName);
         if (newFolder == null)
         {
             return(Success.FailedByNotExistFolderName);
         }
         else if (!(picture.Access <= newFolder.Access))
         {
             return(Success.FailedByWrongAccessNewFolder);
         }
         else
         {
             return(Success.Successfull);
         }
     }
     else if (changeData.FolderName == null && changeData.Access != null)
     {
         if (!(changeData.Access <= picture.Folder.Access))
         {
             return(Success.FailedByWrongAccessFolder);
         }
         else
         {
             return(Success.Successfull);
         }
     }
     else
     {
         return(Success.Successfull);
     }
 }