示例#1
0
        static async void MainAsync(string[] args)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Accept", "application/hal+json");

            HttpResponseMessage response = await client.GetAsync(new Uri(BaseUrl + "/breweries"));

            string stringResponse = await response.Content.ReadAsStringAsync();

            var endpoints = JsonConvert.DeserializeObject <EndpointsClass>(stringResponse);

            var count            = endpoints.Links.Brewery.Count;
            var postBeerEndpoint = string.Empty;

            if (endpoints.Embedded != null && endpoints.Embedded.Brewery.Count > 0)
            {
                postBeerEndpoint = "/" + endpoints.Embedded.Brewery[0].Links.Beers.Href.Split('/')[3];
            }

            int opt;

            do
            {
                Console.WriteLine("Alegeti optiunea: ");
                Console.WriteLine("1 - Afisare beri");
                Console.WriteLine("2 - Afiseaza o bere");
                Console.WriteLine("3 - Adauga o bere");
                Console.WriteLine("4 - Editeaza o bere");
                Console.WriteLine("5 - Sterge o bere");
                Console.WriteLine("0 - Exit");

                opt = int.Parse(Console.ReadLine());
                string url;

                switch (opt)
                {
                //afisare beri
                case 1:
                    Console.Write("Choose the brewery's id: ");    // or brewery's name
                    var breweryId = int.Parse(Console.ReadLine());
                    if (breweryId > count)
                    {
                        Console.WriteLine("Nu exista id-ul berariei!"); break;
                    }

                    if (endpoints.Embedded != null)
                    {
                        url      = BaseUrl + endpoints.Embedded.Brewery.First(e => e.Id == breweryId).Links.Beers.Href;
                        response = await client.GetAsync(new Uri(url));

                        stringResponse = await response.Content.ReadAsStringAsync();

                        var obj  = JsonConvert.DeserializeObject(stringResponse);
                        var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
                        Console.WriteLine(json + "\nStatus code: " + response.StatusCode);
                    }
                    break;

                //afisarea unei beri
                case 2:
                    Console.Write("Choose the brewery's id: ");    //or name
                    breweryId = int.Parse(Console.ReadLine());
                    if (breweryId > count)
                    {
                        Console.WriteLine("Nu exista id-ul berariei!"); break;
                    }

                    Console.Write("Choose the beer's id: ");
                    var beerId = int.Parse(Console.ReadLine());

                    url      = BaseUrl + endpoints.Embedded.Brewery.First(e => e.Id == breweryId).Links.Beers.Href;
                    response = await client.GetAsync(new Uri(url));

                    stringResponse = await response.Content.ReadAsStringAsync();

                    var beers = JsonConvert.DeserializeObject <BeersResponse>(stringResponse);

                    if (beerId > beers.Links.Beers.Count)
                    {
                        Console.WriteLine("Nu exista id-ul berii!"); break;
                    }

                    url      = BaseUrl + beers.Links.Beers[beerId - 1].Href;
                    response = await client.GetAsync(new Uri(url));

                    stringResponse = await response.Content.ReadAsStringAsync();

                    var beer     = JsonConvert.DeserializeObject(stringResponse);
                    var jsonBeer = JsonConvert.SerializeObject(beer, Formatting.Indented);
                    Console.WriteLine(jsonBeer + "\nStatus code: " + response.StatusCode);
                    break;

                //adaugarea unei beri
                case 3:
                    Beer br = new Beer();
                    Console.Write("Dati Id-ul berii: ");
                    br.Id = int.Parse(Console.ReadLine());
                    Console.Write("Dati Id-ul berariei: ");
                    br.BreweryId = int.Parse(Console.ReadLine());
                    Console.Write("Dati numele berii: ");
                    br.Name = Console.ReadLine();
                    Console.Write("Dati numele berariei: ");
                    br.BreweryName = Console.ReadLine();
                    Console.Write("Dati Id-ul stilului: ");
                    br.StyleId = int.Parse(Console.ReadLine());
                    Console.Write("Dati numele stilului: ");
                    br.StyleName = Console.ReadLine();

                    var jsonBeerFormat = JsonConvert.SerializeObject(br, Formatting.Indented);
                    var httpContent    = new StringContent(jsonBeerFormat, Encoding.UTF8, "application/json");
                    url = BaseUrl + postBeerEndpoint;
                    var postResponse = await client.PostAsync(url, httpContent);

                    var responseString = await postResponse.Content.ReadAsStringAsync();

                    Console.WriteLine("Status code: " + postResponse.StatusCode);
                    if (postResponse.StatusCode == HttpStatusCode.Created)
                    {
                        Console.WriteLine("Berea a fost adaugata: \n" + responseString);
                    }
                    break;

                //editarea unei beri
                case 4:
                    Console.Write("Choose the brewery's id: ");
                    breweryId = int.Parse(Console.ReadLine());
                    if (breweryId > count)
                    {
                        Console.WriteLine("Nu exista id-ul berariei!"); break;
                    }

                    Console.Write("Choose the beer's id: ");
                    beerId = int.Parse(Console.ReadLine());

                    Console.Write("Alege alt nume pentru bere:");
                    var newName = Console.ReadLine();

                    url      = BaseUrl + endpoints.Embedded.Brewery.First(e => e.Id == breweryId).Links.Beers.Href;
                    response = await client.GetAsync(new Uri(url));

                    stringResponse = await response.Content.ReadAsStringAsync();

                    beers = JsonConvert.DeserializeObject <BeersResponse>(stringResponse);

                    if (beerId > beers.Links.Beers.Count)
                    {
                        Console.WriteLine("Nu exista id-ul berii!"); break;
                    }

                    url = BaseUrl + beers.Links.Beers[beerId - 1].Href;

                    jsonBeerFormat = JsonConvert.SerializeObject(new Beer {
                        Name = newName
                    }, Formatting.Indented);
                    httpContent = new StringContent(jsonBeerFormat, Encoding.UTF8, "application/json");

                    var putResponse = await client.PutAsync(url, httpContent);

                    responseString = await putResponse.Content.ReadAsStringAsync();

                    Console.WriteLine("Status code: " + putResponse.StatusCode);
                    if (putResponse.StatusCode == HttpStatusCode.OK)
                    {
                        Console.WriteLine("Berea a fost edidata: \n" + responseString);
                    }
                    break;

                //stergerea unei beri
                case 5:
                    Console.Write("Choose the brewery's id: ");    //or name
                    breweryId = int.Parse(Console.ReadLine());
                    if (breweryId > count)
                    {
                        Console.WriteLine("Nu exista id-ul berariei!"); break;
                    }

                    Console.Write("Choose the beer's id: ");
                    beerId = int.Parse(Console.ReadLine());

                    url      = BaseUrl + endpoints.Embedded.Brewery.First(e => e.Id == breweryId).Links.Beers.Href;
                    response = await client.GetAsync(new Uri(url));

                    stringResponse = await response.Content.ReadAsStringAsync();

                    beers = JsonConvert.DeserializeObject <BeersResponse>(stringResponse);

                    if (beerId > beers.Links.Beers.Count)
                    {
                        Console.WriteLine("Nu exista id-ul berii!"); break;
                    }

                    url = BaseUrl + beers.Links.Beers[beerId - 1].Href;

                    response = await client.DeleteAsync(new Uri(url));

                    stringResponse = await response.Content.ReadAsStringAsync();

                    var objDel  = JsonConvert.DeserializeObject(stringResponse);
                    var jsonObj = JsonConvert.SerializeObject(objDel, Formatting.Indented);
                    Console.WriteLine(jsonObj + "\nStatus code: " + response.StatusCode);

                    break;

                default:
                    Console.WriteLine("Comanda gresita!");
                    break;
                }
            } while (opt != 0);
        }
示例#2
0
        static void Main(string[] args)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Accept", "application/hal+json");

            var response = client.GetAsync("http://datc-rest.azurewebsites.net/breweries").Result;

            var data = response.Content.ReadAsStringAsync().Result;
            var obj  = JsonConvert.DeserializeObject <Endpoints>(data);

            var numberOfBreweries = obj.Links.Brewery.Count;
            var postBeer          = string.Empty;

            if (obj.Embedded.Brewery.Count > 0 && obj.Embedded != null)
            {
                postBeer = "/" + obj.Embedded.Brewery[0].Links.Beers.Href.Split('/')[3];
            }


            int optiune = 0;

            do
            {
                Console.WriteLine("1.Afiseaza bere");
                Console.WriteLine("2.Adauga bere");
                Console.WriteLine("0.Iesire");
                Console.WriteLine("Alegeti optiunea:");
                optiune = Int32.Parse(Console.ReadLine());
                string url;

                switch (optiune)
                {
                case 1:
                    Console.Write("Alegeti berarie[id]:");
                    int breweryId = 0;
                    int beerId    = 0;
                    try
                    {
                        breweryId = int.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error at brewery id");
                        Console.ReadLine();
                        break;
                    }

                    if (breweryId > numberOfBreweries)
                    {
                        Console.WriteLine("Nu exista id-ul berariei!");
                        Console.ReadLine();
                        break;
                    }

                    Console.Write("Alegeti berea[id]:");
                    try
                    {
                        beerId = int.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Nu exista id-ul berii!");
                        Console.ReadLine();
                        break;
                    }


                    url = Program.Url + obj.Embedded.Brewery.First(x => x.Id == breweryId).Links.Beers.Href;

                    response = client.GetAsync(new Uri(url)).Result;
                    data     = response.Content.ReadAsStringAsync().Result;
                    var beri = JsonConvert.DeserializeObject <BeersResponse>(data);

                    if (beerId > beri.Links.Beers.Count)
                    {
                        Console.WriteLine("Nu exista id-ul berii");
                        Console.ReadLine();
                        break;
                    }

                    url      = Program.Url + beri.Links.Beers[beerId - 1].Href;
                    response = client.GetAsync(new Uri(url)).Result;
                    data     = response.Content.ReadAsStringAsync().Result;
                    var beer     = JsonConvert.DeserializeObject(data);
                    var jsonBeer = JsonConvert.SerializeObject(beer, Formatting.Indented);
                    Console.WriteLine(jsonBeer);
                    Console.ReadLine();

                    break;

                case 2:
                    Beer bere = new Beer();
                    Console.Write("Id-ul berii:");
                    bere.Id = int.Parse(Console.ReadLine());
                    Console.Write("Id-ul berariei:");
                    bere.BreweryId = int.Parse(Console.ReadLine());
                    Console.Write("Numele berii:");
                    bere.Name = Console.ReadLine();
                    Console.Write("Numele berariei:");
                    bere.BreweryName = Console.ReadLine();
                    Console.Write("Stil id:");
                    bere.StyleId = int.Parse(Console.ReadLine());
                    Console.Write("Nume stil:");
                    bere.StyleName = Console.ReadLine();

                    var jsonBereFormat = JsonConvert.SerializeObject(bere, Formatting.Indented);
                    var httpContent    = new StringContent(jsonBereFormat, Encoding.UTF8, "application/json");
                    url = Program.Url + postBeer;
                    var postResponse   = client.PostAsync(url, httpContent).Result;
                    var responseString = postResponse.Content.ReadAsStringAsync();

                    if (postResponse.StatusCode == System.Net.HttpStatusCode.Created)
                    {
                        Console.WriteLine("Berea a fost adaugata");
                    }
                    Console.ReadLine();
                    break;

                case 0:
                    Console.WriteLine("Iesire");
                    Console.ReadLine();
                    break;

                default:
                    Console.WriteLine("Optiunea aleasa nu exista");
                    Console.ReadLine();
                    break;
                }
                Console.Clear();
            } while (optiune != 0);
        }