예제 #1
0
        public void GetRequestsandTop5()
        {
            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        List <Request> requestListToAdd = new List <Request>();

                        HttpResponseMessage requestResponse = await HttpClientServer.GetAsync($"api/Requests/{LastRequestId}");

                        if (requestResponse.IsSuccessStatusCode)
                        {
                            string jsonRequestResponse = await requestResponse.Content.ReadAsStringAsync();
                            requestListToAdd           = JsonConvert.DeserializeObject <List <Request> >(jsonRequestResponse);
                            LastRequestId = LastRequestId + requestListToAdd.Count;
                        }
                        else
                        {
                            ServerStatus = "Server fail to refresh!";
                        }

                        for (int i = 0; i < requestListToAdd.Count; i++)
                        {
                            RequestRich requestRich = new RequestRich(requestListToAdd[i]);
                            Song requestedSong      = SongList.Find(x => x.Id == requestListToAdd[i].SongId);
                            requestRich.Title       = requestedSong.Title;
                            requestRich.LeadAuthor  = requestedSong.LeadAuthor;
                            RichRequestObservable.Add(requestRich);
                        }

                        requestListToAdd.Clear();

                        List <VotedSong> votesSongList = new List <VotedSong>();

                        HttpResponseMessage votesResponse = await HttpClientServer.GetAsync("api/songs/votes");

                        if (votesResponse.IsSuccessStatusCode)
                        {
                            string jsonVotes = await votesResponse.Content.ReadAsStringAsync();
                            votesSongList    = JsonConvert.DeserializeObject <List <VotedSong> >(jsonVotes);
                        }
                        else
                        {
                            ServerStatus = "Server fail to refresh!";
                        }

                        Top5Text = "Top songs:";

                        for (int i = 0; i < 5; i++)
                        {
                            int topSongId    = votesSongList[i].SongId;
                            Song top5Song    = SongList.Find(x => x.Id == topSongId);
                            string songToAdd = "Votes: " + votesSongList[i].Votes + " " + top5Song.Title + " by " + top5Song.LeadAuthor;
                            Top5Text         = Top5Text + "\n\n" + songToAdd;
                        }

                        votesSongList.Clear();

                        List <VotedSong> moreVotesSongList = new List <VotedSong>();

                        HttpResponseMessage moreVotesResponse = await HttpClientServer.GetAsync("api/songs/morevotes");

                        if (moreVotesResponse.IsSuccessStatusCode)
                        {
                            string jsonMoreVotes = await moreVotesResponse.Content.ReadAsStringAsync();
                            moreVotesSongList    = JsonConvert.DeserializeObject <List <VotedSong> >(jsonMoreVotes);
                        }
                        else
                        {
                            ServerStatus = "Server fail to refresh!";
                        }

                        Top10Text = "Top songs:";

                        for (int i = 0; i < 10; i++)
                        {
                            int top10SongId    = moreVotesSongList[i].SongId;
                            Song top10Song     = SongList.Find(x => x.Id == top10SongId);
                            string song10ToAdd = "Votes: " + moreVotesSongList[i].Votes + " " + top10Song.Title + " by " + top10Song.LeadAuthor;
                            Top10Text          = Top10Text + "\n\n" + song10ToAdd;
                        }

                        moreVotesSongList.Clear();

                        HttpResponseMessage chatResponse = await HttpClientServer.GetAsync($"api/Chats/{LastChatId}");

                        if (chatResponse.IsSuccessStatusCode)
                        {
                            string jsonChatResponse = await chatResponse.Content.ReadAsStringAsync();
                            List <string> chatGot   = JsonConvert.DeserializeObject <List <string> >(jsonChatResponse);
                            if (chatGot.Count == 0)
                            {
                            }
                            else
                            {
                                for (int i = 0; i < chatGot.Count; i++)
                                {
                                    ChatText = ChatText + "\n\n" + chatGot[i];
                                }

                                LastChatId = LastChatId + chatGot.Count;
                            }
                        }
                        else
                        {
                            ServerStatus = "Server fail to refresh!";
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Error while loading data!");
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(5000));
                }
            });
        }