public async Task <IActionResult> DeleteMessage(int userId, int messageId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var messageFromRepo = await _repo.GetMessage(messageId);

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

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

            if (messageFromRepo.RecipientDeleted && messageFromRepo.SenderDeleted)
            {
                _repo.Delete(messageFromRepo);
            }

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

            throw new Exception("Error deleting the message");
        }
        public async Task <IActionResult> DeleteListingPhoto(int listingId, int id)
        {
            var listingFromRepo = await _repo.GetListing(listingId);

            if (listingFromRepo.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (!listingFromRepo.Photos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            var photoFromRepo = await _repo.GetListingPhoto(id);

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

            if (photoFromRepo.PublicId != null)
            {
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);

                var result = _cloudinary.Destroy(deleteParams);

                if (result.Result == "ok")
                {
                    _repo.Delete(photoFromRepo);
                }
            }
            else if (photoFromRepo.PublicId == null)
            {
                _repo.Delete(photoFromRepo);
            }

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

            return(BadRequest("Failed to delete photo"));
        }
示例#3
0
 public bool Delete(ProductEntity entity)
 {
     try
     {
         _productRepository.Delete(entity);
         return(true);
     }
     catch (Exception e)
     {
         _log.Error(e, "数据库操作出错");
         return(false);
     }
 }
 public bool Delete(CommissionRatioEntity entity)
 {
     try
     {
         _commissionRatioRepository.Delete(entity);
         return(true);
     }
     catch (Exception e)
     {
         _log.Error(e, "数据库操作出错");
         return(false);
     }
 }
示例#5
0
        public async Task <IActionResult> WatchListing(int id, int listingId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (await _repo.GetListing(listingId) == null)
            {
                return(NotFound());
            }

            var listingWatch = await _repo.GetListingWatch(id, listingId);

            string action;

            if (listingWatch != null)
            {
                action = "Unwatch";
                _repo.Delete <ListingWatch>(listingWatch);
            }
            else
            {
                listingWatch = new ListingWatch
                {
                    WatcherId  = id,
                    WatchingId = listingId
                };

                action = "Watch";
                _repo.Add <ListingWatch>(listingWatch);
            }

            var message = $"{action} listing successful. [WatcherId={listingWatch.WatcherId}, WatchingID={listingWatch.WatchingId}]";

            if (await _repo.SaveAll())
            {
                return(Ok(new { action, message }));
            }


            return(BadRequest($"Failed to {action} listing"));
        }
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUserDetails(userId);

            var file = photoForCreationDto.File;

            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(file.Name, stream)
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoForCreationDto.Url      = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

            var photo = _mapper.Map <Avatar>(photoForCreationDto);


            if (userFromRepo.Avatar != null)
            {
                var avatarFromRepo = await _repo.GetAvatar(userFromRepo.Avatar.Id);

                if (avatarFromRepo.PublicId != null)
                {
                    var deleteParams = new DeletionParams(avatarFromRepo.PublicId);

                    var result = _cloudinary.Destroy(deleteParams);

                    if (result.Result == "ok")
                    {
                        _repo.Delete(avatarFromRepo);
                    }
                }
                else if (avatarFromRepo.PublicId == null)
                {
                    _repo.Delete(avatarFromRepo);
                }
            }

            userFromRepo.Avatar = photo;

            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <AvatarForReturnDto>(photo);
                return(CreatedAtRoute("GetAvatar", new { userId, id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Could not save the avatar photo"));
        }