// https://localhost:44334/api/SearchRest/searchArtistsByGenres?term=1&term=2 public IActionResult SearchArtistsByGenres() { try { string term = HttpContext.Request.Query["term"].ToString(); IEnumerable <string> terms = term.Split(','); int c = terms.Count(); // ** for brevity > only allow up to 3 genres picked ** if (c > 3) { c = 3; } // ** CONTAINS returns genres 10, 11, 12, etc if passed in value = 1 ** // ** this is NOT the graceful way I hoped to achieve this, however other attempts failed to work ** if (c == 1) { var artists = _artistGenre.GetAll() .Where(p => p.Genre.Id.ToString().Equals(((string[])terms)[0])) .Select(p => p.Artist).OrderBy(p => p.ArtistName).ToList(); return(Ok(artists.Distinct())); } if (c == 2) { var artists = _artistGenre.GetAll() .Where(p => p.Genre.Id.ToString().Equals(((string[])terms)[0]) || p.Genre.Id.ToString().Equals(((string[])terms)[1])) .Select(p => p.Artist).OrderBy(p => p.ArtistName).ToList(); return(Ok(artists.Distinct())); } if (c == 3) { var artists = _artistGenre.GetAll() .Where(p => p.Genre.Id.ToString().Equals(((string[])terms)[0]) || p.Genre.Id.ToString().Equals(((string[])terms)[1]) || p.Genre.Id.ToString().Equals(((string[])terms)[2])) .Select(p => p.Artist).OrderBy(p => p.ArtistName).ToList(); return(Ok(artists.Distinct())); } return(BadRequest()); } catch { return(BadRequest()); } }
public async Task <IActionResult> Index(int artistId, MainIndexModel srch) { var url = "https://localhost:44334/api/SearchRest/searchArtistsByGenres?"; // put in appSettings //var url = "https://wikivox.azurewebsites.net/api/SearchRest/searchArtistsByGenres?"; // put in appSettings var terms = string.Empty; if (srch.GenreListing == null) { // default genre search terms = "term=1"; } else { //loop thru list and obtain search terms foreach (var g in srch.GenreListing) { if (g.isMarked) { if (terms == string.Empty) { terms += "term=" + g.Id; } else { terms += "&term=" + g.Id; } } } } List <Artist> artistModel = new List <Artist>(); using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync(url + terms)) { string apiResponse = await response.Content.ReadAsStringAsync(); artistModel = JsonConvert.DeserializeObject <List <Artist> >(apiResponse); } } // by default, load one artist in object for launch page Random rnd = new Random(); int artistNum = _artist.GetAll().Count(); //var x = x - 1; int defaultArtistId = rnd.Next(1, artistNum - 1); var artistInfoModel = _artist.Get(defaultArtistId); // if an artist has been selected, gather details to display if (artistId != 0) { artistInfoModel = _artist.Get(artistId); } var genreModel = _genre.GetAll(); var artistGenreModel = _artistGenre.GetAll(); // genre list var genres = genreModel.Select (g => new GenreListingModel { Id = g.Id, Name = g.Name } ).ToList(); // artist list var artists = artistModel.Select (a => new ArtistListingModel { Id = a.Id, ArtistName = a.ArtistName } ).ToList(); var artistGenres = artistGenreModel.Select (r => new ArtistGenreModel { Id = r.Id, Artist = r.Artist, Genre = r.Genre } ).ToList(); var artistInfo = new ArtistListingModel { Id = artistInfoModel.Id, ArtistName = artistInfoModel.ArtistName, Bio = artistInfoModel.Bio, YrFormed = artistInfoModel.YrFormed, YrEnded = artistInfoModel.YrEnded, isActive = artistInfoModel.isActive, HomeCountry = artistInfoModel.HomeCountry, HomeTown = artistInfoModel.HomeTown, PrimaryImage = _image.GetPrimaryImageByEntity(2, artistInfoModel.Id, 1) }; var model = new MainIndexModel { GenreListing = genres, ArtistListing = artists, ArtistGenre = artistGenres, ArtistInfo = artistInfo }; return(View(model)); }