public async Task <ActionResult <GenreResponse> > Put(int id, [FromBody] GenreResponse input,
                                                              CancellationToken ct = default)
        {
            try
            {
                if (input == null)
                {
                    return(BadRequest());
                }
                if (await _chinookSupervisor.GetGenreByIdAsync(id, ct) == null)
                {
                    return(NotFound());
                }

                var errors = JsonConvert.SerializeObject(ModelState.Values
                                                         .SelectMany(state => state.Errors)
                                                         .Select(error => error.ErrorMessage));
                Debug.WriteLine(errors);

                if (await _chinookSupervisor.UpdateGenreAsync(input, ct))
                {
                    return(Ok(input));
                }

                return(StatusCode(500));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Beispiel #2
0
        public async Task <List <Genre> > GetGenres()
        {
            GenreResponse genreResponse = new GenreResponse();

            genreResponse = await client.GetFromJsonAsync <GenreResponse>("https://api.themoviedb.org/3/genre/movie/list?api_key=580bda8a5ab6a951e45ea4385de5bdd8");

            return(genreResponse.genres);
        }
        public async Task <IEnumerable <Genre> > GetGenres(string query)
        {
            GenreResponse searchGenreResponse = await _spotifyService.GetGenresAsync();

            return(searchGenreResponse.Genres
                   .Select(GetGenreObject)
                   .Where(genre => MatchesQuery(genre, query)));
        }
Beispiel #4
0
        public IActionResult GetFromId(int genreId)
        {
            var genre = _repository.GetFromId(genreId);

            GenreResponse response = GenreResponse.MapFromObject(genre);

            return(Ok(response));
        }
Beispiel #5
0
 private async Task <GenreResponse> _loadGenres(CancellationToken cancellationToken = default(CancellationToken))
 {
     if (_genreResponse == null)
     {
         var genreDataSource = new GenreRemoteDataSource();
         _genreResponse = await genreDataSource.GetAsync(cancellationToken);
     }
     return(_genreResponse);
 }
Beispiel #6
0
        public async Task <IActionResult> GetById(Guid id)
        {
            GenreResponse result = await GenreService.GetGenreAsync(new GetGenreRequest()
            {
                Id = id
            });

            return(Ok(result));
        }
Beispiel #7
0
        private async Task <bool> GenreExists(Guid genreId, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(genreId.ToString()))
            {
                return(false);
            }

            GenreResponse genre = await _genreService.GetGenreAsync(new GetGenreRequest { Id = genreId });

            return(genre != null);
        }
        public async Task <GenreResponse> AddGenreAsync(GenreResponse newGenreViewModel,
                                                        CancellationToken ct = default)
        {
            var genre = new Genre
            {
                Name = newGenreViewModel.Name
            };

            genre = await _genreRepository.AddAsync(genre, ct);

            newGenreViewModel.GenreId = genre.GenreId;
            return(newGenreViewModel);
        }
Beispiel #9
0
        public IActionResult GetImplementedByUser()
        {
            var userId = TokenManager.ExtractUserId(User.Claims);
            var genres = _repository.GetByUser(userId);

            List <GenreResponse> response = new List <GenreResponse>();

            foreach (var genre in genres)
            {
                response.Add(GenreResponse.MapFromObject(genre));
            }

            return(Ok(response));
        }
        private async Task <bool> GenreExists(Guid id, CancellationToken cancelationToken)
        {
            if (string.IsNullOrEmpty(id.ToString()))
            {
                return(false);
            }

            GenreResponse result = await _genreService.GetGenreAsync(new GetGenreRequest()
            {
                Id = id
            });

            return(result != null);
        }
        public async Task <bool> UpdateGenreAsync(GenreResponse genreViewModel,
                                                  CancellationToken ct = default)
        {
            var genre = await _genreRepository.GetByIdAsync(genreViewModel.GenreId, ct);

            if (genre == null)
            {
                return(false);
            }
            genre.GenreId = genreViewModel.GenreId;
            genre.Name    = genreViewModel.Name;

            return(await _genreRepository.UpdateAsync(genre, ct));
        }
Beispiel #12
0
        public async Task GetGenre_should_return_genre_with_specified_id(string guid)
        {
            //Arrange
            GenreService sut = new GenreService(GenreRepo, VideoRepo, Mapper);

            //Act
            GenreResponse result = await sut.GetGenreAsync(new GetGenreRequest()
            {
                Id = new Guid(guid)
            });

            //Assert
            result.GenreId.Should().Be(new Guid(guid));
            Assert.NotNull(result.Name);
        }
        public async Task <ActionResult <GenreResponse> > Post([FromBody] GenreResponse input,
                                                               CancellationToken ct = default)
        {
            try
            {
                if (input == null)
                {
                    return(BadRequest());
                }

                return(StatusCode(201, await _chinookSupervisor.AddGenreAsync(input, ct)));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Beispiel #14
0
        public async Task AddGenre_should_return_the_expected_genre()
        {
            //Arrange
            AddGenreRequest expectedGenre = new AddGenreRequest()
            {
                GenreName = GenData.Create <string>()
            };

            GenreService sut = new GenreService(GenreRepo, VideoRepo, Mapper);

            //Act
            GenreResponse result = await sut.AddGenreAsync(expectedGenre);

            //Assert
            //expectedGenre.Should().BeEquivalentTo(result, o =>
            //    o.Excluding(x => x.GenreId));
            Assert.Equal(expectedGenre.GenreName, result.Name);
        }
Beispiel #15
0
            public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
            {
                if (!(context.ActionArguments["id"] is Guid id))
                {
                    context.Result = new BadRequestResult();
                    return;
                }

                GenreResponse result = await GenreService.GetGenreAsync(new GetGenreRequest { Id = id });

                if (result == null)
                {
                    context.Result = new NotFoundObjectResult(new JsonErrorPayload
                    {
                        DetailedMessage = $"Genre with id {id} not exist."
                    });
                    return;
                }

                await next();
            }
Beispiel #16
0
        //Get's spotify's current genres
        public async Task <string[]> GenresToArray(string auth)
        {
            //cache the genres to limit api calls
            var spotifyGenres = new GenreResponse();

            if (!_cache.TryGetValue("spotifyGenres", out spotifyGenres))
            {
                var baseAddress = "https://api.spotify.com/v1/recommendations/available-genre-seeds";
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + auth);

                    client.BaseAddress = new Uri(baseAddress);
                    var genreResponse = await client.GetAsync(baseAddress);

                    spotifyGenres = JsonSerializer.Deserialize <GenreResponse>(await genreResponse.Content.ReadAsByteArrayAsync(), null);
                }


                _cache.Set("spotifyGenres", spotifyGenres);
            }
            return(spotifyGenres.genres);
        }
Beispiel #17
0
        /// <summary>
        /// Get all genres
        /// </summary>
        /// <param name="language">Genre language</param>
        /// <param name="ct">Used to cancel loading genres</param>
        /// <returns>Genres</returns>
        public async Task <List <GenreJson> > GetGenresAsync(string language, CancellationToken ct)
        {
            var response = new GenreResponse
            {
                Genres = new List <GenreJson>
                {
                    new GenreJson
                    {
                        EnglishName = "Action",
                        Name        = "Action"
                    },
                    new GenreJson
                    {
                        EnglishName = "Adventure",
                        Name        = language == "fr" ? "Aventure" : "Adventure"
                    },
                    new GenreJson
                    {
                        EnglishName = "Animation",
                        Name        = "Animation"
                    },
                    new GenreJson
                    {
                        EnglishName = "Comedy",
                        Name        = language == "fr" ? "Comédie" : "Comedy"
                    },
                    new GenreJson
                    {
                        EnglishName = "Crime",
                        Name        = "Crime"
                    },
                    new GenreJson
                    {
                        EnglishName = "Documentary",
                        Name        = language == "fr" ? "Documentaire" : "Documentary"
                    },
                    new GenreJson
                    {
                        EnglishName = "Drama",
                        Name        = language == "fr" ? "Drame" : "Drama"
                    },
                    new GenreJson
                    {
                        EnglishName = "Family",
                        Name        = language == "fr" ? "Familial" : "Family"
                    },
                    new GenreJson
                    {
                        EnglishName = "Fantasy",
                        Name        = language == "fr" ? "Fantastique" : "Fantasy"
                    },
                    new GenreJson
                    {
                        EnglishName = "History",
                        Name        = language == "fr" ? "Histoire" : "History"
                    },
                    new GenreJson
                    {
                        EnglishName = "Horror",
                        Name        = language == "fr" ? "Horreur" : "Horror"
                    },
                    new GenreJson
                    {
                        EnglishName = "Music",
                        Name        = language == "fr" ? "Musique" : "Music"
                    },
                    new GenreJson
                    {
                        EnglishName = "Mystery",
                        Name        = language == "fr" ? "Mystère" : "Mystery"
                    },
                    new GenreJson
                    {
                        EnglishName = "Romance",
                        Name        = "Romance"
                    },
                    new GenreJson
                    {
                        EnglishName = "Science-Fiction",
                        Name        = "Science-Fiction"
                    },
                    new GenreJson
                    {
                        EnglishName = "Thriller",
                        Name        = "Thriller"
                    },
                    new GenreJson
                    {
                        EnglishName = "War",
                        Name        = language == "fr" ? "Guerre" : "War"
                    },
                    new GenreJson
                    {
                        EnglishName = "Western",
                        Name        = "Western"
                    },
                }
            };

            return(await Task.FromResult(response.Genres));
        }
Beispiel #18
0
        public async Task <IActionResult> Post(AddGenreRequest request)
        {
            GenreResponse result = await GenreService.AddGenreAsync(request);

            return(CreatedAtAction(nameof(GetById), new { id = result.GenreId }, null));
        }