Esempio n. 1
0
        public async Task <User> RemoveFavouriteAsync(Guid id, Guid favouriteId)
        {
            try
            {
                var userToUpdate = await userRepository.GetAsync(id).ConfigureAwait(false);

                var filmToFavourite = await filmRepository.GetAsync(favouriteId).ConfigureAwait(false);

                await userValidator.ValidateDeleteFavouriteFilmAsync(userToUpdate, filmToFavourite);

                var associationToRemove = await userFavouriteFilmRepo.GetByConditionAsync(f => f.UserId == id && f.FilmId == favouriteId).ConfigureAwait(false);

                userFavouriteFilmRepo.Remove(associationToRemove);
                userToUpdate.FavouriteFilms.Remove(associationToRemove);
                filmToFavourite.UserFavourites.Remove(associationToRemove);
                userRepository.Update(userToUpdate);
                filmRepository.Update(filmToFavourite);

                await userRepository.SaveChangesAsync().ConfigureAwait(false);

                return(await userRepository.GetAsync(userToUpdate.Id).ConfigureAwait(false));
            }
            catch (Exception e)
            {
                throw new BusinessLogicException(e.Message);
            }
        }