private GetCommentsModel GenerateGetCommentsModel(int postId) { var commentaryService = new CommentaryService(); var comments = commentaryService.SearchCommentsByIdPost(new SearchCommentsByIdPostRequest() { IdPost = postId }).Comments; var commentsResult = new GetCommentsModel(); commentsResult.Comments = new List <CommentaryModel>(); foreach (var commentary in comments) { if (!commentary.IdUpperComment.HasValue) { var commentaryToAdd = TheModelFactory.CreateCommentaryModel(commentary); commentaryToAdd.Answers = new List <AnswerModel>(); foreach (var answer in comments) { if (answer.IdUpperComment.HasValue) { if (answer.IdUpperComment == commentary.Id) { var answerToAdd = TheModelFactory.CreateAnswerModel(answer); commentaryToAdd.Answers.Add(answerToAdd); } } } commentsResult.Comments.Add(commentaryToAdd); } } return(commentsResult); }
//GetComments returns all the comments on a plant which matches the given plantID, with Palnt name and comments. public GetCommentsModel GetComments(int plantID) { Plants plants = ctx.Plants.Single(e => e.PlantID == plantID); GetCommentsModel query = new GetCommentsModel { PlantID = plants.PlantID, PlantName = plants.Name, Comments = ctx.Comments.Where(e => e.PlantID == plants.PlantID).Select(g => new CommentModel { Title = g.Title, Comment = g.Comment, Username = ctx.Users.FirstOrDefault(e => e.Id.ToString() == g.UserID.ToString()).UserName, CreatedDate = g.CreatedDate }).ToList(), Likes = ctx.Likes.Where(e => e.PlantID == plants.PlantID).Select(g => g.IsLiked == true).Count() }; return(query); }