コード例 #1
0
        public ActionResult DeletePhoto(int photoId)
        {
            bool         res               = false;
            string       message           = string.Empty;
            Photo        photo             = _photoRepository.GetById(photoId);
            Announcement announcement      = photo.Announcement;
            User         owner             = photo.Owner;
            User         authenticatedUser = _userRepository.GetUserByPhone(User.Identity.Name);

            if (authenticatedUser == photo.Owner)
            {
                deletePhoto(photo.Url);
                _photoRepository.Delete(photo);
                _photoRepository.SaveChanges();
                res = true;
            }

            if (announcement.Photos.Count < owner.Profile.MaxPhotosPerAnnouncements)
            {
                message = string.Format(Translation.Translation.PhotosLeft, owner.Profile.MaxPhotosPerAnnouncements - announcement.Photos.Count);
            }
            else
            {
                message = string.Format(Translation.Translation.LimitForPhotosIsReachedLabel, "/");
            }

            return(Json(new { Done = res, Message = message }, "text/json"));
        }
コード例 #2
0
        public async Task <IActionResult> DeletePhoto(int id, int photoId)
        {
            if (checkUser(id))
            {
                return(Unauthorized());
            }

            var userFromRepo  = _usersRepository.GetUser(id);
            var photoFromRepo = _photosRepository.GetPhoto(photoId);
            await Task.WhenAll(userFromRepo, photoFromRepo);

            var currentMainPhoto = userFromRepo.Result.Photos.Where(p => p.IsMain).FirstOrDefault();

            if (!checkIfPhotoExists(photoId, userFromRepo.Result))
            {
                return(Unauthorized());
            }

            if (photoFromRepo.Result.IsMain)
            {
                return(BadRequest("You cannot delete your main photo"));
            }

            if (photoFromRepo.Result.PublicId != null)
            {
                var result = DeleteFileInCloudinary(photoFromRepo.Result.PublicId);
                if (result)
                {
                    _photosRepository.Delete(photoFromRepo.Result);
                }
            }
            else
            {
                _photosRepository.Delete(photoFromRepo.Result);
            }

            if (await _photosRepository.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to delete the photo"));
        }
コード例 #3
0
        public async Task Delete(Guid userId, Guid photoId)
        {
            var photoDB = await _photosRepository.GetById(photoId);

            if (photoDB == null)
            {
                throw new ArgumentException("Photo not found");
            }
            if (photoDB.UserId != userId)
            {
                throw new ArgumentException("Photo doesn't belong to user");
            }

            await Task.WhenAll(_photosRepository.Delete(photoDB),
                               _feedService.DeletePhotoFromFeed(userId, photoDB));
        }
コード例 #4
0
ファイル: PhotosService.cs プロジェクト: everas7/dating-app
        public async Task Delete(int userId, int photoId)
        {
            var user = await _usersRepo.Get(userId);

            var photo = user.Photos.FirstOrDefault(p => p.Id == photoId);

            if (photo == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not found" });
            }

            if (photo.IsMain)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { Photo = "You cannot delete your main photo" });
            }

            await _repo.Delete(photo);
        }
コード例 #5
0
 public void Delete(int photoId)
 {
     _repository.Delete(photoId);
 }