From eda3d3278a13bfa004da66fb65a8c91913108299 Mon Sep 17 00:00:00 2001 From: Jose E Rodriguez-Marrero Date: Sat, 29 Jun 2024 01:18:35 -0700 Subject: [PATCH] Add the ability to query comments by stock and order --- api/Controllers/CommentController.cs | 5 +++-- api/Helpers/CommentQueryObject.cs | 8 ++++++++ api/Interfaces/ICommentRepo.cs | 3 ++- api/Repositories/CommentRepo.cs | 17 +++++++++++++++-- 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 api/Helpers/CommentQueryObject.cs diff --git a/api/Controllers/CommentController.cs b/api/Controllers/CommentController.cs index fec646e..60b75e3 100644 --- a/api/Controllers/CommentController.cs +++ b/api/Controllers/CommentController.cs @@ -1,5 +1,6 @@ using api.DTOs.Comment; using api.Extensions; +using api.Helpers; using api.Interfaces; using api.Mappers; using api.Models; @@ -27,9 +28,9 @@ public CommentController(ICommentRepo commentRepo, IStockRepo stockRepo, UserMan } [HttpGet] - public async Task GetAllCommentsAsync() + public async Task GetAllCommentsAsync(CommentQueryObject queryObject) { - var comments = await _commentRepo.GetAllAsync(); + var comments = await _commentRepo.GetAllAsync(queryObject); if (comments is null) { diff --git a/api/Helpers/CommentQueryObject.cs b/api/Helpers/CommentQueryObject.cs new file mode 100644 index 0000000..9694ac2 --- /dev/null +++ b/api/Helpers/CommentQueryObject.cs @@ -0,0 +1,8 @@ +namespace api.Helpers +{ + public class CommentQueryObject + { + public string? Symbol { get; set; } + public bool IsDescending { get; set; } = true; + } +} \ No newline at end of file diff --git a/api/Interfaces/ICommentRepo.cs b/api/Interfaces/ICommentRepo.cs index 9665ae8..345bbfe 100644 --- a/api/Interfaces/ICommentRepo.cs +++ b/api/Interfaces/ICommentRepo.cs @@ -1,11 +1,12 @@ using api.DTOs.Comment; +using api.Helpers; using api.Models; namespace api.Interfaces { public interface ICommentRepo { - Task> GetAllAsync(); + Task> GetAllAsync(CommentQueryObject queryObject); Task GetByIdAsync(int id); Task CreateAsync(Comment comment); Task UpdateAsync(int id, UpdateCommentRequestDto commentDto); diff --git a/api/Repositories/CommentRepo.cs b/api/Repositories/CommentRepo.cs index e5294d0..c6f0e44 100644 --- a/api/Repositories/CommentRepo.cs +++ b/api/Repositories/CommentRepo.cs @@ -1,5 +1,6 @@ using api.Data; using api.DTOs.Comment; +using api.Helpers; using api.Interfaces; using api.Mappers; using api.Models; @@ -15,9 +16,21 @@ public CommentRepo(AppDbContext context) { _context = context; } - public async Task> GetAllAsync() + public async Task> GetAllAsync(CommentQueryObject queryObject) { - return await _context.Comments.Include(a => a.AppUser).ToListAsync(); + var comments = _context.Comments.Include(a => a.AppUser).AsQueryable(); + + if (!string.IsNullOrWhiteSpace(queryObject.Symbol)) + { + comments = comments.Where(c => c.Stock.Symbol == queryObject.Symbol); + }; + + if (queryObject.IsDescending) + { + comments = comments.OrderByDescending(c => c.CreatedOn); + } + + return await comments.ToListAsync(); } public async Task GetByIdAsync(int id)