Ejemplo n.º 1
0
        public async Task <Result <object> > Handle(DeletePostCommand request, CancellationToken cancellationToken)
        {
            var post = await _forumDbContext.Posts
                       .IncludePreview()
                       .SingleOrDefaultAsync(p => p.AuthorId == request.UserId && p.Id == request.PostId);

            if (post == null)
            {
                return(Result <object> .Failure(nameof(request.PostId), "Post doesn't exist or you can't edit it"));
            }

            _forumDbContext.Posts.Remove(post);
            await _photoStorage.DeletePhotoAsync(post.Preview.Image.FileName);

            await _forumDbContext.SaveChangesAsync();

            return(Result <object> .Success());
        }
Ejemplo n.º 2
0
        public async Task <Result <object> > Handle(DeleteAvatarCommand request, CancellationToken cancellationToken)
        {
            var profile = await _forumDbContext.Profiles
                          .IncludeUser()
                          .SingleAsync(p => p.UserId == request.UserId);

            var avatar = profile.Avatar;

            if (avatar == null)
            {
                return(Result <object> .Failure(string.Empty, "Avatar doesn't exist"));
            }

            _forumDbContext.Remove(avatar);
            await _photoStorage.DeletePhotoAsync(avatar.FileName);

            await _forumDbContext.SaveChangesAsync();

            return(Result <object> .Success());
        }
Ejemplo n.º 3
0
        public async Task <Result <UserProfileDto> > Handle(EditAvatarCommand request, CancellationToken cancellationToken)
        {
            var profile = await _forumDbContext.Profiles
                          .IncludeUser()
                          .SingleAsync(p => p.UserId == request.UserId);

            var validationResult = _photoValidator.ValidatePhoto(request.Avatar);

            if (!validationResult.Succeeded)
            {
                return(Result <UserProfileDto> .Failure(validationResult.Field, validationResult.Message));
            }

            var oldAvatar = profile.Avatar;

            if (oldAvatar != null)
            {
                _forumDbContext.Photos.Remove(oldAvatar);
                await _photoStorage.DeletePhotoAsync(oldAvatar.FileName);
            }

            var newAvatar = request.Avatar;

            var resizedAvatar = await _imageProcessor.ResizeImage(newAvatar);

            var fileName = await _photoStorage.StorePhotoAsync(resizedAvatar, ".png");

            profile.Avatar = new Photo {
                FileName = fileName
            };
            await _forumDbContext.SaveChangesAsync();

            var result = _mapper.Map <UserProfileDto>(profile);

            return(Result <UserProfileDto> .Success(result));
        }
Ejemplo n.º 4
0
 protected override async Task Handle(DeletePhotoCommand request, CancellationToken cancellationToken)
 {
     await _photoStorage.DeletePhotoAsync(request.FileName);
 }