Esempio n. 1
0
        static async Task <AnswerResponse> PostAnswer(string token, Answer answer)
        {
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            HttpContent content = new StringContent(JsonConvert.SerializeObject(answer), Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.PostAsync(apiBaseAddress + "api/answer/" + token, content);

            if (response.IsSuccessStatusCode)
            {
                string         result         = response.Content.ReadAsStringAsync().Result;
                AnswerResponse answerResponse = JsonConvert.DeserializeObject <AnswerResponse>(result);
                return(answerResponse);
            }
            return(null);
        }
Esempio n. 2
0
        static async Task RunAsync()
        {
            client.BaseAddress = apiBaseAddress;
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            try
            {
                //Get api token
                string token = await GetToken();

                Console.WriteLine("Token = " + token);

                //Get list of subscribers
                List <Subscriber> subscribers = await GetSubscribers(token);

                //Get list of categories
                List <string> categories = await GetCategories(token);

                Console.WriteLine("*********Categories*****************");

                foreach (string category in categories)
                {
                    Console.WriteLine(category);
                }


                Console.WriteLine("************************************");

                //Get the list of all magazines of all categories
                List <Magazine> magazines = await GetAllMagazines(token, categories);

                Console.WriteLine("*********************************************************************************");
                Console.WriteLine("List of subscribers that are subscirbed to at least one magazine in each category");
                Console.WriteLine("*********************************************************************************");

                Answer answer = new Answer();
                answer.Subscribers = new List <string>();

                //Loop through subcriber list to check the list of subscribers that are subscribed to at least one mangazine in each category
                foreach (Subscriber subscriber in subscribers)
                {
                    bool isFound = (from m in magazines
                                    join c in categories on m.Category equals c
                                    join sm in subscriber.MagazineIds on m.Id equals sm
                                    group sm by m.Category into g
                                    select g).ToList().Count == 3;

                    if (isFound)
                    {
                        answer.Subscribers.Add(subscriber.Id);
                        Console.WriteLine("Subcriber :  " + subscriber.Id);
                    }
                }

                Console.WriteLine("*********************************************************************************");

                AnswerResponse answerResponse = await PostAnswer(token, answer);

                Console.WriteLine("*********************************************************************************");
                Console.WriteLine("                     Answer Response");
                Console.WriteLine("*********************************************************************************");
                Console.WriteLine("Total Time     : " + answerResponse.Data.TotalTime);
                Console.WriteLine("Answer Correct : " + answerResponse.Data.AnswerCorrect);
                Console.WriteLine("Should Be      : ");
                if (answerResponse.Data.ShouldBe != null)
                {
                    foreach (string item in answerResponse.Data.ShouldBe)
                    {
                        Console.WriteLine(item);
                    }
                }
                Console.WriteLine("*********************************************************************************");

                Console.WriteLine(">Press any key to exit.");
                Console.ReadLine();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }