Ejemplo n.º 1
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var user = await _repo.GetUserAsync(userId);

            var photo = user.Photos.FirstOrDefault(f => f.Id == id);

            if (photo == null || photo.IsMain)
            {
                return(BadRequest("Photo doesent exist or is the main photo"));
            }

            //za slike koje su dodane na pocetku i nisu postavnjene na cloudinary(one nece imati vrijednost PublicId-a)
            if (string.IsNullOrEmpty(photo.PublicId))
            {
                _repo.Delete(photo);
            }

            var delParams = new DeletionParams(photo.PublicId);
            var result    = _cloudinary.Destroy(delParams);

            if (result.Result == "ok")
            {
                _repo.Delete(photo);
            }
            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to delete the photo"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> DeleteMessage(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var messageFromRepo = await _repo.GetMessage(id);

            if (messageFromRepo.SenderId == userId)
            {
                messageFromRepo.SenderDelete = true;
            }

            if (messageFromRepo.RecipientId == userId)
            {
                messageFromRepo.RecipientDelete = true;
            }

            if (messageFromRepo.SenderDelete && messageFromRepo.RecipientDelete)
            {
                _repo.Delete(messageFromRepo);
            }

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception("Error deleting the message");
        }