Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MusicStore.Data;
- using System.Data.Entity;
- using MusicStore.Model;
- using MusicStore.Data.Migrations;
- using System.Net.Http;
- using System.Net.Http.Headers;
- namespace MusicStore.Client
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("{0}", new String('-', 20));
- Console.WriteLine("GetAll<Artists>()");
- Console.WriteLine("{0}", new String('-', 20));
- var allArtists = GetAll<Artist>("artists");
- if (allArtists != null)
- {
- foreach (var item in allArtists)
- {
- Console.WriteLine("{0} {1} {2}", item.ID, item.FullName, item.Country);
- }
- }
- Console.WriteLine("{0}", new String('-', 20));
- Console.WriteLine("GetAll<Songs>()");
- Console.WriteLine("{0}", new String('-', 20));
- var allSongs = GetAll<Song>("songs");
- if (allSongs != null)
- {
- foreach (var item in allSongs)
- {
- Console.WriteLine("{0} {1} {2}", item.ID, item.Title, item.Artist.FullName);
- }
- }
- Console.WriteLine("{0}", new String('-', 20));
- Console.WriteLine("GetAll<Albums>()");
- Console.WriteLine("{0}", new String('-', 20));
- var allAlbums = GetAll<Album>("albums");
- if (allAlbums != null)
- {
- foreach (var item in allAlbums)
- {
- Console.WriteLine("Album: {0} {1} {2}", item.ID, item.Title, item.Producer);
- foreach (var artist in item.Artists)
- {
- Console.WriteLine("Artist: {0}", artist.FullName);
- }
- foreach (var song in item.Songs)
- {
- Console.WriteLine("Song: {0}", song.Title);
- }
- }
- }
- Console.WriteLine("{0}", new String('-', 20));
- Console.WriteLine("GetItem<Artist>()");
- Console.WriteLine("{0}", new String('-', 20));
- var artistItem = GetItem<Artist>("artists",1);
- if (artistItem != null)
- {
- Console.WriteLine("{0} {1}",artistItem.ID, artistItem.FullName);
- }
- }
- private static IEnumerable<T> GetAll<T>(string repository)
- {
- var client = new HttpClient
- {
- BaseAddress = new Uri("http://localhost:31109/")
- };
- client.DefaultRequestHeaders.Accept.Add(new
- MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response =
- client.GetAsync("api/" + repository).Result;
- if (response.IsSuccessStatusCode)
- {
- var artists = response.Content
- .ReadAsAsync<IEnumerable<T>>().Result;
- return artists;
- }
- else
- {
- Console.WriteLine("{0} ({1})",
- (int)response.StatusCode, response.ReasonPhrase);
- return null;
- }
- }
- //where T: class, new() allows to return null
- private static T GetItem<T>(string repository, int ID)
- where T : class, new()
- {
- var client = new HttpClient
- {
- BaseAddress = new Uri("http://localhost:31109/")
- };
- client.DefaultRequestHeaders.Accept.Add(new
- MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response =
- client.GetAsync("api/" + repository + "/" + ID).Result;
- if (response.IsSuccessStatusCode)
- {
- var artists = response.Content
- .ReadAsAsync<T>().Result;
- return artists;
- }
- else
- {
- Console.WriteLine("{0} ({1})",
- (int)response.StatusCode, response.ReasonPhrase);
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement