Skip to content

Commit

Permalink
Add the ability to query comments by stock and order
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorNull committed Jun 29, 2024
1 parent a155978 commit eda3d32
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions api/Controllers/CommentController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.DTOs.Comment;
using api.Extensions;
using api.Helpers;
using api.Interfaces;
using api.Mappers;
using api.Models;
Expand Down Expand Up @@ -27,9 +28,9 @@ public CommentController(ICommentRepo commentRepo, IStockRepo stockRepo, UserMan
}

[HttpGet]
public async Task<IActionResult> GetAllCommentsAsync()
public async Task<IActionResult> GetAllCommentsAsync(CommentQueryObject queryObject)
{
var comments = await _commentRepo.GetAllAsync();
var comments = await _commentRepo.GetAllAsync(queryObject);

if (comments is null)
{
Expand Down
8 changes: 8 additions & 0 deletions api/Helpers/CommentQueryObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace api.Helpers
{
public class CommentQueryObject
{
public string? Symbol { get; set; }
public bool IsDescending { get; set; } = true;
}
}
3 changes: 2 additions & 1 deletion api/Interfaces/ICommentRepo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using api.DTOs.Comment;
using api.Helpers;
using api.Models;

namespace api.Interfaces
{
public interface ICommentRepo
{
Task<List<Comment>> GetAllAsync();
Task<List<Comment>> GetAllAsync(CommentQueryObject queryObject);
Task<Comment?> GetByIdAsync(int id);
Task<Comment> CreateAsync(Comment comment);
Task<Comment?> UpdateAsync(int id, UpdateCommentRequestDto commentDto);
Expand Down
17 changes: 15 additions & 2 deletions api/Repositories/CommentRepo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.Data;
using api.DTOs.Comment;
using api.Helpers;
using api.Interfaces;
using api.Mappers;
using api.Models;
Expand All @@ -15,9 +16,21 @@ public CommentRepo(AppDbContext context)
{
_context = context;
}
public async Task<List<Comment>> GetAllAsync()
public async Task<List<Comment>> 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);

Check warning on line 25 in api/Repositories/CommentRepo.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
};

if (queryObject.IsDescending)
{
comments = comments.OrderByDescending(c => c.CreatedOn);
}

return await comments.ToListAsync();
}

public async Task<Comment?> GetByIdAsync(int id)
Expand Down

0 comments on commit eda3d32

Please sign in to comment.