Beispiel #1
0
        private static async Task GetExchangeById()
        {
            Console.Clear();
            Console.Write("Exchange ID to lookup:");
            string userInput = Console.ReadLine();

            Console.Clear();

            var response = await _httpClient.GetAsync($"https://localhost:44331/api/Exchange/{userInput}");

            if (response is null)
            {
                Console.WriteLine($"No exchange found with ID: {userInput}");
                return;
            }

            ExchangeDetail exchange = response.Content.ReadAsAsync <ExchangeDetail>().Result;

            Console.WriteLine($"" +
                              $"Exchange ID: {exchange.Id}\n" +
                              $"Book ISBN: {exchange.BookId}\n" +
                              $"Book Title: {exchange.BookTitle}\n" +
                              $"Posting User: {exchange.PostingUser}\n" +
                              $"Users Rating: {exchange.PostersRating}\n" +
                              $"Exchange Posted Date: {exchange.Posted}\n");
            if (exchange.Comments.Count > 0)
            {
                Console.WriteLine("\n\n" +
                                  "_____________________\n" +
                                  "Comments: \n" +
                                  "_____________________");
                foreach (var comment in exchange.Comments)
                {
                    string indentation = "     ";
                    Console.WriteLine($"{indentation}Comment By:{comment.CommentersName}\n" +
                                      $"{indentation}Comment ID:{comment.Id}\n" +
                                      $"{indentation}{comment.Text}\n\n");
                    if (comment.Replies.Count > 0)
                    {
                        Console.WriteLine(indentation +
                                          $"Replies to {comment.CommentersName}:");
                        PrintReplies(comment.Replies, indentation);
                    }
                    Console.WriteLine("_____________________");
                }
            }
        }
 public ExchangeDetail GetExchangeById(int id)
 {
     using (var ctx = new ApplicationDbContext())
     {
         var entity =
             ctx
             .Exchanges
             .Single(e => e.Id == id);
         var detailedExchange =
             new ExchangeDetail
         {
             Id        = entity.Id,
             BookId    = entity.BookId,
             BookTitle = entity.Book.BookTitle,
             //using ternaries incase of null senderUser
             PostingUser   = entity.SenderUser != null ? entity.SenderUser.FullName : null,
             PostersRating = entity.SenderUser != null ? entity.SenderUser.ExchangeRating : null,
             Posted        = entity.Posted,
             SentDate      = entity.SentDate
         };
         foreach (Comment comment in entity.Comments)
         {
             //If comment is Reply it will attach to its comment so can be skipped
             if (comment is Reply)
             {
                 continue;
             }
             //Add comments and their replies
             var commentDetail = new CommentDetail {
                 Id = comment.Id, Text = comment.Text,
                 //Using ternary incase of null commenter
                 CommentersName = comment.Commenter != null ? comment.Commenter.FullName : "Unknown",
                 //Using recursive Method to populate replies
                 Replies = AddReplies(comment.Replies)
             };
             detailedExchange.Comments.Add(commentDetail);
         }
         return(detailedExchange);
     }
 }