Advertisement
vlad0

Untitled

Aug 6th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MusicStore.Data;
  7. using System.Data.Entity;
  8. using MusicStore.Model;
  9. using MusicStore.Data.Migrations;
  10. using System.Net.Http;
  11. using System.Net.Http.Headers;
  12.  
  13. namespace MusicStore.Client
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             Console.WriteLine("{0}", new String('-', 20));
  20.             Console.WriteLine("GetAll<Artists>()");
  21.             Console.WriteLine("{0}", new String('-', 20));
  22.  
  23.             var allArtists = GetAll<Artist>("artists");
  24.             if (allArtists != null)
  25.             {
  26.                 foreach (var item in allArtists)
  27.                 {
  28.                     Console.WriteLine("{0} {1} {2}", item.ID, item.FullName, item.Country);
  29.                 }
  30.             }
  31.  
  32.             Console.WriteLine("{0}", new String('-', 20));
  33.             Console.WriteLine("GetAll<Songs>()");
  34.             Console.WriteLine("{0}", new String('-', 20));
  35.  
  36.             var allSongs = GetAll<Song>("songs");
  37.             if (allSongs != null)
  38.             {
  39.                 foreach (var item in allSongs)
  40.                 {
  41.                     Console.WriteLine("{0} {1} {2}", item.ID, item.Title, item.Artist.FullName);
  42.                 }
  43.             }
  44.  
  45.             Console.WriteLine("{0}", new String('-', 20));
  46.             Console.WriteLine("GetAll<Albums>()");
  47.             Console.WriteLine("{0}", new String('-', 20));
  48.  
  49.             var allAlbums = GetAll<Album>("albums");
  50.             if (allAlbums != null)
  51.             {
  52.                 foreach (var item in allAlbums)
  53.                 {
  54.                     Console.WriteLine("Album: {0} {1} {2}", item.ID, item.Title, item.Producer);
  55.                     foreach (var artist in item.Artists)
  56.                     {
  57.                         Console.WriteLine("Artist: {0}", artist.FullName);
  58.                     }
  59.                     foreach (var song in item.Songs)
  60.                     {
  61.                         Console.WriteLine("Song: {0}", song.Title);
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             Console.WriteLine("{0}", new String('-', 20));
  67.             Console.WriteLine("GetItem<Artist>()");
  68.             Console.WriteLine("{0}", new String('-', 20));
  69.  
  70.             var artistItem = GetItem<Artist>("artists",1);
  71.             if (artistItem != null)
  72.             {
  73.                 Console.WriteLine("{0} {1}",artistItem.ID, artistItem.FullName);
  74.             }
  75.            
  76.         }
  77.  
  78.         private static IEnumerable<T> GetAll<T>(string repository)
  79.         {
  80.             var client = new HttpClient
  81.             {
  82.                 BaseAddress = new Uri("http://localhost:31109/")
  83.             };
  84.             client.DefaultRequestHeaders.Accept.Add(new
  85.                 MediaTypeWithQualityHeaderValue("application/json"));
  86.             HttpResponseMessage response =
  87.                 client.GetAsync("api/" + repository).Result;
  88.             if (response.IsSuccessStatusCode)
  89.             {
  90.                 var artists = response.Content
  91.                     .ReadAsAsync<IEnumerable<T>>().Result;
  92.                 return artists;
  93.             }
  94.             else
  95.             {
  96.                 Console.WriteLine("{0} ({1})",
  97.                     (int)response.StatusCode, response.ReasonPhrase);
  98.                 return null;
  99.             }
  100.         }
  101.  
  102.         //where T: class, new() allows to return null
  103.         private static T GetItem<T>(string repository, int ID)
  104.             where T : class, new()
  105.         {
  106.             var client = new HttpClient
  107.             {
  108.                 BaseAddress = new Uri("http://localhost:31109/")
  109.             };
  110.             client.DefaultRequestHeaders.Accept.Add(new
  111.                 MediaTypeWithQualityHeaderValue("application/json"));
  112.             HttpResponseMessage response =
  113.                 client.GetAsync("api/" + repository + "/" + ID).Result;
  114.             if (response.IsSuccessStatusCode)
  115.             {
  116.                 var artists = response.Content
  117.                     .ReadAsAsync<T>().Result;
  118.                 return artists;
  119.             }
  120.             else
  121.             {
  122.                 Console.WriteLine("{0} ({1})",
  123.                     (int)response.StatusCode, response.ReasonPhrase);
  124.                 return null;
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement