Skip to content

Commit

Permalink
Merge pull request #1132 from gordon-cs/FilterReportsAPI-Jones
Browse files Browse the repository at this point in the history
Filtering and Pagination on API
  • Loading branch information
Centerville1 authored Jan 30, 2025
2 parents 4a50a83 + ebed1f5 commit 604670e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
17 changes: 14 additions & 3 deletions Gordon360/Controllers/LostAndFoundController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,32 @@ public async Task<ActionResult> UpdateReportStatus(int missingItemId, string sta
/// <summary>
/// Get the list of missing item reports for the currently authenticated user.
/// </summary>
/// <param name="status">The selected status</param>
/// <param name="color">The selected color for filtering reports</param>
/// <param name="category">The selected category for filtering reports</param>
/// <param name="keywords">The selected keywords for filtering by keywords</param>
/// <param name="status">The selected status for filtering reports</param>
/// <param name="user">Query parameter, default is null and route will get all missing items, or if user is set
/// route will get missing items for the authenticated user</param>
/// <param name="lastId">The ID of the last fetched report to start from for pagination</param>
/// <param name="pageSize">The size of the page to fetch for pagination</param>
/// <returns>ObjectResult - an http status code, with an array of MissingItem objects in the body </returns>
[HttpGet]
[Route("missingitems")]
public ActionResult<IEnumerable<MissingItemReportViewModel>> GetMissingItems(string? status, string? user = null)
public ActionResult<IEnumerable<MissingItemReportViewModel>> GetMissingItems(string? user = null,
int? lastId = null,
int? pageSize = null,
string? status = null,
string? color = null,
string? category = null,
string? keywords = null)
{
IEnumerable<MissingItemReportViewModel> result;
var authenticatedUserUsername = AuthUtils.GetUsername(User);

// If no username specified in the query, get all items
if (user == null)
{
result = lostAndFoundService.GetMissingItemsAll(authenticatedUserUsername, status);
result = lostAndFoundService.GetMissingItemsAll(authenticatedUserUsername, lastId, pageSize, status, color, category, keywords);
}
else
{
Expand Down
18 changes: 14 additions & 4 deletions Gordon360/Documentation/Gordon360.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 39 additions & 3 deletions Gordon360/Services/LostAndFoundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,58 @@ public IEnumerable<MissingItemReportViewModel> GetMissingItems(string requestedU
/// Get all missing item reports
/// Throw unauthorized access exception if the user doesn't have admin permissions
/// </summary>
/// <param name="status">The selected status</param>
/// <param name="color">The selected color for filtering reports</param>
/// <param name="category">The selected category for filtering reports</param>
/// <param name="keywords">The selected keywords for filtering by keywords</param>
/// <param name="status">The selected status for filtering reports</param>
/// <param name="username">The username of the person making the request</param>
/// <param name="lastId">The ID of the last fetched report to start from for pagination</param>
/// <param name="pageSize">The size of the page to fetch for pagination</param>
/// <returns>An enumerable of Missing Item Reports, from the Missing Item Data view</returns>
/// <exception cref="UnauthorizedAccessException">If a user without admin permissions attempts to use</exception>
public IEnumerable<MissingItemReportViewModel> GetMissingItemsAll(string username, string status)
public IEnumerable<MissingItemReportViewModel> GetMissingItemsAll(string username,
int? lastId,
int? pageSize,
string? status,
string? color,
string? category,
string? keywords)
{
if (!hasFullPermissions(username))
{
throw new UnauthorizedAccessException();
}

IQueryable<MissingItemData> missingItems = context.MissingItemData;
IQueryable<MissingItemData> missingItems = context.MissingItemData.OrderBy(item => item.ID);
if (status is not null)
{
missingItems = missingItems.Where(x => x.status == status);
}
if (color is not null)
{
missingItems = missingItems.Where(x => x.colors.Contains(color));
}
if (category is not null)
{
missingItems = missingItems.Where(x => x.category == category);
}
if (keywords is not null)
{
missingItems = missingItems.Where(x => x.firstName.Contains(keywords)
|| x.lastName.Contains(keywords)
|| (x.firstName + " " + x.lastName).Contains(keywords)
|| x.description.Contains(keywords)
|| x.locationLost.Contains(keywords));
}
if (lastId is not null)
{
missingItems = missingItems.Where(item => item.ID > lastId);
}
if (pageSize is not null)
{
int pageLength = pageSize ?? default(int);
missingItems = missingItems.Take(pageLength);
}

// Perform a group join to create a MissingItemReportViewModel with actions taken data for each report
// Only performs a single SQL query to the db, so much more performant than alternative solutions
Expand Down
9 changes: 8 additions & 1 deletion Gordon360/Services/ServiceInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using RecIMActivityViewModel = Gordon360.Models.ViewModels.RecIM.ActivityViewModel;
Expand Down Expand Up @@ -232,7 +233,13 @@ public interface ILostAndFoundService
public int CreateMissingItemReport(MissingItemReportViewModel reportDetails, string username);
public int CreateActionTaken(int id, ActionsTakenViewModel ActionsTaken, string username);
IEnumerable<MissingItemReportViewModel> GetMissingItems(string requestedUsername, string requestorUsername);
IEnumerable<MissingItemReportViewModel> GetMissingItemsAll(string username, string status);
IEnumerable<MissingItemReportViewModel> GetMissingItemsAll(string username,
int? lastId,
int? pageSize,
string? status,
string? color,
string? category,
string? keywords);
Task UpdateMissingItemReportAsync(int id, MissingItemReportViewModel reportDetails, string username);
Task UpdateReportStatusAsync(int id, string status, string username);
MissingItemReportViewModel? GetMissingItem(int id, string username);
Expand Down

0 comments on commit 604670e

Please sign in to comment.