public async Task <JokeDto> UpdateJokeAsync(UpdateJokeDto jokeDto)
        {
            using (IUnitOfWork uow = unitOfWorkFactory.CreateUnitOfWork())
            {
                var jokeRepository = uow.GetRepository <IJokeRepository>();
                var joke           = (await jokeRepository.FindAndMapAsync(j => j,
                                                                           j => j.Id == jokeDto.Id,
                                                                           null,
                                                                           new List <Expression <Func <Joke, object> > > {
                    j => j.JokeCategories
                }))
                                     .FirstOrDefault();
                if (joke == null)
                {
                    throw new ArgumentException($"Joke with Id = {jokeDto.Id} does't exist");
                }

                joke.Value = jokeDto.Value;
                await jokeRepository.UpdateJokeCategoriesAsync(joke, jokeDto.Categories);

                jokeRepository.Update(joke);
                await uow.SaveChangesAsync();

                var     categoryRepository = uow.GetRepository <ICategoryRepository>();
                JokeDto returnJokeDto      = this.mapper.Map <JokeDto>(joke);
                returnJokeDto.Categories = (await categoryRepository.FindAndMapAsync(c => c.Title, c => c.JokeCategories.Any(jc => jc.JokeId == joke.Id))).ToList();
                return(returnJokeDto);
            }
        }
        public async Task <JokeDto> GetJokeAsync(int jokeId)
        {
            using (IUnitOfWork uow = unitOfWorkFactory.CreateUnitOfWork())
            {
                var     jokeRepository = uow.GetRepository <IJokeRepository>();
                JokeDto returnJokeDto  = await jokeRepository.FindByKeyAndMapAsync(j => j.Id == jokeId, jokeRepository.JokeToJokeDtoSelector());

                return(returnJokeDto);
            }
        }
Example #3
0
        public async Task <JokeDto> GetJoke(int id)
        {
            try
            {
                var json = await client.GetStringAsync($"{apiJokesController}/{id}");

                JokeDto joke = JsonConvert.DeserializeObject <JokeDto>(json);
                return(joke);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error occurred while reading jokes: {ex.ToString()}");
            }

            return(null);
        }
Example #4
0
        public async Task <JokeDto> DeleteJoke(int id)
        {
            try
            {
                var response = await client.DeleteAsync($"{apiJokesController}/{id}");

                var contents = await response.Content.ReadAsStringAsync();

                JokeDto joke = JsonConvert.DeserializeObject <JokeDto>(contents);
                return(joke);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error occurred while deleting jokes: {ex.ToString()}");
            }

            return(null);
        }
Example #5
0
        public async Task <JokeDto> UpdateJoke(JokeDto jokeDto)
        {
            try
            {
                var jokeData = JsonConvert.SerializeObject(jokeDto);
                var response = await client.PutAsync($"{apiJokesController}/{jokeDto.Id}", new StringContent(jokeData, Encoding.UTF8, "application/json"));

                var contents = await response.Content.ReadAsStringAsync();

                JokeDto joke = JsonConvert.DeserializeObject <JokeDto>(contents);
                return(joke);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error occurred while updating jokes: {ex.ToString()}");
            }

            return(null);
        }
Example #6
0
        private void RandomJokeTask(object state, CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            IJoke joke = new JokeDto {
                Joke = FallbackJoke
            };

            try
            {
                joke = GetRandomJoke();
            }
            catch (Exception x)
            {
                _logger.LogError(x, "unable to get a joke from the server");
            }
            finally
            {
                DisplayRandomJoke(HttpUtility.UrlDecode(joke?.Joke ?? FallbackJoke)).Wait(cancellationToken);
            }
        }