Ejemplo n.º 1
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(p => p.Photos)
                           .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id);

                if (photo == null)
                {
                    return(null);
                }

                if (photo.IsMain)
                {
                    return(Result <Unit> .Failure("You cannot delete your main photo"));
                }

                var result = await _photoAccessor.deletePhoto(photo.Id);

                if (result == null)
                {
                    return(Result <Unit> .Failure("Problem ddeleting photo from Cloudinary"));
                }

                user.Photos.Remove(photo);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Result <Unit> .Success(Unit.Value));
                }

                return(Result <Unit> .Failure("Problem ddeleting photo from API"));
            }