Ejemplo n.º 1
0
        public async Task <FriendDto> Handle(CreateFriendCommand request, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByEmailAsync("*****@*****.**");

            var adress = new Address(request.Number, request.Street, request.Neighborhood, request.City);

            var friend = new Friend(request.Name, request.Email, request.Phone, adress, user);

            var photoUploadResult = _photoAccesor.AddPhoto(request.File);

            friend.AddPhoto(photoUploadResult);

            _entityValidator.Validate(new Entity[] { friend, adress });

            if (_notification.HasNotifications)
            {
                _photoAccesor.DeletePhoto(photoUploadResult?.PublicId);
                return(null);
            }

            _context.Friends.Add(friend);

            await _context.Commit();

            return(_mapper.Map <FriendDto>(friend));
        }
Ejemplo n.º 2
0
        public async Task <ProductViewModel> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var price = Convert.ToDecimal(request.Price);

            if (price < 0)
            {
                _notification.AddNotification("Preço", "Valor digitado é invalido");
                return(null);
            }
            var product = new Product(request.Name, price);

            var photoUploadResult = _photoAccesor.AddPhoto(request.File);

            product.AddPhoto(photoUploadResult);

            _entityValidator.Validate(new Entity[] { product });

            if (_notification.HasNotifications)
            {
                _photoAccesor.DeletePhoto(photoUploadResult?.PublicId);
                return(null);
            }

            _context.Products.Add(product);

            await _context.Commit();

            return(_mapper.Map <ProductViewModel>(product));
        }
Ejemplo n.º 3
0
            public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var product = await _context.Products.Include(p => p.Pictures)
                              .FirstOrDefaultAsync(x => x.Id == request.proId);

                if (product.Pictures == null || product.Pictures.Count < 1)
                {
                    return(ResultVm <Unit> .Success(Unit.Value));
                }
                foreach (var item in product.Pictures)
                {
                    var result = await _photoAccessor.DeletePhoto(item.Id);

                    if (result == null)
                    {
                        return(ResultVm <Unit> .Failure("Problem deleting picture from Cloudinary"));
                    }

                    product.Pictures.Remove(item);
                    _context.Remove(item);
                }

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

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

                return(ResultVm <Unit> .Failure("Problem deleting all picture from API"));
            }
Ejemplo n.º 4
0
        public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
        {
            var experience = await _context.Experiences.Include(p => p.Logo).Where(e => e.Id == request.Id).FirstOrDefaultAsync <Experience>();

            if (experience == null)
            {
                return(null);
            }
            var photo = _photoAccessor.DeletePhoto(experience.Logo.Id);

            if (photo == null)
            {
                return(Result <Unit> .Failure("Failed to delete photo"));
            }

            _context.Photos.Remove(experience.Logo);
            _context.Experiences.Remove(experience);
            var result = await _context.SaveChangesAsync() > 0;

            if (!result)
            {
                return(Result <Unit> .Failure("Failed to delete"));
            }

            return(Result <Unit> .Success(Unit.Value));
        }
Ejemplo n.º 5
0
            public async Task <PhotoUploadResult> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                if (user == null)
                {
                    throw new ErrorException(HttpStatusCode.NotFound, new { User = "******" });
                }

                var photoUploadResult = _photoAccessor.AddPhoto(request.File);

                if (user.photoId != null)
                {
                    if (_photoAccessor.DeletePhoto(user.photoId) == "ok")
                    {
                        System.Console.WriteLine("Profile Image is deleted");
                    }
                    else
                    {
                        System.Console.WriteLine("Fail Destroy Profile Image");
                    }
                }
                user.photoUrl = photoUploadResult.Url;
                user.photoId  = photoUploadResult.PublicId;
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(photoUploadResult);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName());

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

                if (photo == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Photo = "Photo doesn't exist" });
                }
                if (photo.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new{ Photo = "Cannot delete main photo" });
                }

                var status = _photoAccessor.DeletePhoto(request.PublicId);

                if (status == null)
                {
                    throw new Exception("Cloudinary call failed");
                }

                user.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Save Failed");
            }
Ejemplo n.º 7
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByNameAsync(userAccessor.GetCurrentUsername());

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

                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" });
                }

                var result = photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting photo");
                }

                context.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Problem saving activity");
            }
Ejemplo n.º 8
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users
                           .SingleOrDefaultAsync(p => p.UserName == userAccessor.GetCurrentUsername());

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

                if (photo == null)
                {
                    throw new RestException(HttpStatusCode.NotFound,
                                            new { activity = "Could not find Photo to delete" });
                }

                if (photo.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest,
                                            new { activity = "You cannot your main photo" });
                }
                var result = photoAccessor.DeletePhoto(request.Id);

                user.Photos.Remove(photo);
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem in delete Photo handler");
            }
Ejemplo n.º 9
0
        public static async Task <ReturnResult> Delete(this Post post, string userId, IPhoto photoUploads,
                                                       IPhotoAccessor photoAccessor, IYoutube youtubeuploader, IVideo videoContext, IPost postContext)
        {
            var keys = await postContext.GetKeys(post.Id);

            var videoResult = await youtubeuploader.DeleteVideo(keys[0].VideoId);

            var imageResult = photoAccessor.DeletePhoto(keys[0].PhotoId);

            if (videoResult && imageResult.ToLower().Equals("ok"))
            {
                await postContext.Delete(post);
            }

            if (await postContext.SaveChanges())
            {
                return new ReturnResult
                       {
                           Succeeded = true
                       }
            }
            ;


            return(new ReturnResult
            {
                Succeeded = false,
                Error = "error deleting value from database"
            });
        }
Ejemplo n.º 10
0
        public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
        {
            var education = await _context.Schools.Include(e => e.Logo).Where(e => e.Id == request.Id).FirstOrDefaultAsync();

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

            if (education.Logo != null)
            {
                var res = await _photoAccessor.DeletePhoto(education.Logo.Id);

                if (res == null)
                {
                    return(Result <Unit> .Failure("Failed to delete photo"));
                }
            }

            _context.Remove(education);
            var result = await _context.SaveChangesAsync() > 0;

            if (!result)
            {
                return(Result <Unit> .Failure("Failed to delete"));
            }

            return(Result <Unit> .Success(Unit.Value));
        }
        public async Task <Unit> Handle(DeletePhotoCommand request, CancellationToken cancellationToken)
        {
            var photo = await _context.Photos
                        .SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken : cancellationToken);

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

            var result = _photoAccessor.DeletePhoto(request.Id);

            if (result == null)
            {
                throw new Exception("Problem deleting the photo");
            }

            _context.Photos.Remove(photo);

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

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes");
        }
Ejemplo n.º 12
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var product = await _context.Products.FindAsync(request.ProductId);

                if (product == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { product = "Not Found" });
                }

                var photo = product.ProductPhotos.FirstOrDefault(x => x.Id == request.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" });
                }

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting photo");
                }

                product.ProductPhotos.Remove(photo);

                var success = await _context.SaveChangesAsync();

                return(success > 0 ? Unit.Value : throw new Exception("Problem saving changes"));
            }
Ejemplo n.º 13
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(), cancellationToken: cancellationToken);

                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("Main photo can not be deleted"));
                }
                var result = await _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    return(Result <Unit> .Failure("Problem deleting photo"));
                }
                user.Photos.Remove(photo);
                var success = await _context.SaveChangesAsync(cancellationToken) > 0;

                return(success ? Result <Unit> .Success(Unit.Value) : Result <Unit> .Failure("Problem deleting photo from API"));
            }
Ejemplo n.º 14
0
        public async Task <IActionResult> Delete(string id)
        {
            var currentUser = await _usersRepository.GetCurrentUser();

            if (currentUser == null)
            {
                return(Unauthorized());
            }

            var photo = currentUser.Photos.FirstOrDefault(p => p.Id == id);

            if (photo == null)
            {
                return(NotFound());
            }
            if (photo.IsMain)
            {
                return(BadRequest("You cannot delete your main photo."));
            }
            // if photo is hosted on cloudinary then delete it
            if (photo.Id.Length < 21)
            {
                var result = _photoAccessor.DeletePhoto(id);
                _ = result ?? throw new Exception("Problem delete the photo.");
            }

            currentUser.Photos.Remove(photo);

            if (await _datingRepository.Save())
            {
                return(NoContent());
            }

            return(BadRequest("Problem deleting photo."));
        }
Ejemplo n.º 15
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

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

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

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

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting the photo.");
                }

                user.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("An error occured when saving activity to the database.");
            }
Ejemplo n.º 16
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

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

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

                if (photo.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Photo = "Vous ne pouvez pas supprimer votre photo principale" });
                }

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Erreur survenue pendant la suppression de la photo");
                }

                user.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Erreur survenue pendant la mise à jour des données");
            }
Ejemplo n.º 17
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users
                           .Include(p => p.Photos)
                           .FirstOrDefaultAsync(u => u.UserName == _userAccessor.GetUsername());

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

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

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

                var cloudinaryResult = await _photoAccessor.DeletePhoto(photo.Id);

                if (cloudinaryResult == null)
                {
                    return(Result <Unit> .Failure("Problem deleting photo from cloudinary"));
                }

                user.Photos.Remove(photo);
                var result = await _context.SaveChangesAsync() > 0;

                return(result ? Result <Unit> .Success(Unit.Value) : Result <Unit> .Failure("Failure to delete photo"));
            }
Ejemplo n.º 18
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                var photo = user.Photo;

                if (photo == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Photo = "찾을 수 없습니다." });
                }


                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting photo");
                }

                _context.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 19
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName());

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

                if (photo == null)
                {
                    throw new Exception("NotFound Photo");
                }

                if (photo.IsMain)
                {
                    throw new Exception("You cannot delete your main photo");
                }

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting photo");
                }

                user.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 20
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var skill = await _context.Skills.Include(p => p.Photo).Where(skill => skill.Id == request.Id).FirstOrDefaultAsync <Skill>();

                if (skill == null)
                {
                    return(null);
                }
                var photo = await _photoAccessor.DeletePhoto(skill.Photo.Id);

                if (photo == null)
                {
                    return(Result <Unit> .Failure("Failed to delete photo"));
                }

                _context.Remove(skill);
                _context.Remove(skill.Photo);
                var result = await _context.SaveChangesAsync() > 0;

                if (!result)
                {
                    return(Result <Unit> .Failure("Failed to delete"));
                }

                return(Result <Unit> .Success(Unit.Value));
            }
Ejemplo n.º 21
0
        public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
        {
            var project = await _context.Projects.Include(p => p.Photos).Where(p => p.Id == request.Id).FirstOrDefaultAsync();

            if (project == null)
            {
                return(null);
            }
            if (project.Photos.Count > 0)
            {
                foreach (Photo photo in project.Photos)
                {
                    var photoResult = await _photoAccessor.DeletePhoto(photo.Id);

                    if (photoResult == null)
                    {
                        return(Result <Unit> .Failure("Failed to delete photo"));
                    }
                    _context.Remove(photo);
                }
            }

            _context.Remove(project);

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

            if (!result)
            {
                return(Result <Unit> .Failure("Failed to delete"));
            }

            return(Result <Unit> .Success(Unit.Value));
        }
Ejemplo n.º 22
0
        public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
        {
            var data = await _context.Certificates.Include(c => c.Logo).Where(c => c.Id == request.Id).FirstOrDefaultAsync();

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

            if (data.Logo != null)
            {
                var res = await _photoAccessor.DeletePhoto(data.Logo.Id);

                if (res == null)
                {
                    return(Result <Unit> .Failure("Failed to Delete Photo"));
                }
            }
            _context.Remove(data);

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

            if (!result)
            {
                return(Result <Unit> .Failure("Failed to Delete Certificate"));
            }

            return(Result <Unit> .Success(Unit.Value));
        }
Ejemplo n.º 23
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                     x.UserName == _userAccessor.GetCurrentUserName());

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

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

                if (photo.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Photo = "Unable to delete the main photo" });
                }

                var result = _photoAccessor.DeletePhoto(request.PhotoId);

                if (result == "ok")
                {
                    user.Photos.Remove(photo);
                    var success = await _context.SaveChangesAsync(cancellationToken) > 0;

                    if (success)
                    {
                        return(Unit.Value);
                    }
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 24
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(x => x.Photos).SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

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

                if (photo == null)
                {
                    throw new AppException("Not Found", System.Net.HttpStatusCode.NotFound);
                }

                if (photo.IsMain)
                {
                    throw new AppException("You can not delete your main photo", System.Net.HttpStatusCode.BadRequest);
                }

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new AppException("problem deleting th photo", System.Net.HttpStatusCode.InternalServerError);
                }

                user.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Problem saving chnages");
            }
Ejemplo n.º 25
0
        public async Task <Object> DeletePhoto(string id)
        {
            var photo = _context.Photos.FirstOrDefault(x => x.Id == id);

            if (photo == null)
            {
                return(JsonConvert.SerializeObject(new { Error = "Photo not found" }));
            }
            if (photo.IsMain)
            {
                return(JsonConvert.SerializeObject(new { Error = "Can not delete main photo" }));
            }

            var result = _photoAccessor.DeletePhoto(photo.Id);

            _context.Photos.Remove(photo);
            if (result == null)
            {
                return(JsonConvert.SerializeObject(new { Error = "Problem deleting photo" }));
            }
            //return result
            var isSuccess = await _context.SaveChangesAsync() > 0;

            if (isSuccess)
            {
                return(JsonConvert.SerializeObject(new { Result = "Delete photo success" }));
            }

            throw new Exception("Problem saving changes");
        }
Ejemplo n.º 26
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _userAccessor.GetCurrentUser();

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

                if (photo == default)
                {
                    throw new PhotoNotFoundException();
                }

                if (photo.IsMain)
                {
                    throw new MainPhotoDeletionException();
                }

                _context.Photos.Remove(photo);

                var isSavedSuccess = await _context.SaveChangesAsync(cancellationToken) > 0;

                var deletionResult = await _photoAccessor.DeletePhoto(photo.Id);

                if (isSavedSuccess && deletionResult == PhotoDeletionResult.Ok)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
            Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

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

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

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

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting photo..");
                }

                user.Photos.Remove(photo);

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

                if (successfullyAdded)
                {
                    return(Unit.Value);
                }

                throw new Exception("Change has not been saved...");
            }
Ejemplo n.º 28
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var ticket = await _context.Tickets.SingleOrDefaultAsync(x => x.Id == request.TicketId);

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

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

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

                var result = _photoAccessor.DeletePhoto(photo.Id);

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

                ticket.Photos.Remove(photo);

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

                if (!success)
                {
                    return(Result <Unit> .Failure("Failed deleting photo."));
                }

                return(Result <Unit> .Success(Unit.Value));
            }
Ejemplo n.º 29
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(user => user.UserName == _userAccessor.GetCurrentUsername());

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

                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" });

                var result = _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    throw new Exception("Problem deleting the photo");
                }

                user.Photos.Remove(photo);

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

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 30
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUserId = _userAccessor.GetUserId();
                var userPhoto     = await _context.UserPhotos.FindAsync(request.UserPhotoId);

                if (userPhoto == null)
                {
                    throw new Exception("The photo was not found.");
                }

                if (userPhoto.AppUserId != currentUserId)
                {
                    throw new Exception("You do not have permission to delete this photo.");
                }

                if (_photoAccessor.DeletePhoto(userPhoto.Id) == null)
                {
                    throw new Exception("Problem deleting photo.");
                }

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

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }