Skip to content

Commit

Permalink
Merge pull request #34 from Purdue-ACM-SIGAPP/lookup-by-acronym
Browse files Browse the repository at this point in the history
Lookup building by name or acronym
  • Loading branch information
AndrewZacharyLiu authored Oct 23, 2024
2 parents e1e5a06 + 027d6e5 commit cc761a9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
38 changes: 22 additions & 16 deletions Controllers/BuildingController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using FuzzySharp;
using Microsoft.AspNetCore.Mvc;
using SimpleWebAppReact.Entities;
using Microsoft.Extensions.Logging;
Expand All @@ -23,32 +24,37 @@ public BuildingController(ILogger<BuildingController> logger, MongoDbService mon
}

/// <summary>
/// gets buildings, with optional query parameters
/// gets buildings using an approximate search
/// </summary>
/// <param name="name"></param>
/// <param name="address"></param>
/// <param name="query"></param>
/// <returns></returns>
[HttpGet]
public async Task<IEnumerable<Building>> Get([FromQuery] string? name = null, [FromQuery] string? address = null)
public async Task<IEnumerable<Building>> Get([FromQuery] string? query = null)
{
// Build the filter using a filter builder
var filterBuilder = Builders<Building>.Filter;
var filter = FilterDefinition<Building>.Empty;

// Apply the name filter if the parameter is provided
if (!string.IsNullOrEmpty(name))
int FuzzScore(Building building)
{
filter &= filterBuilder.Eq(b => b.Name, name);
return Math.Max(
building.Name == null ? 0 : Fuzz.Ratio(query, building.Name),
building.Acronym == null ? 0 : Fuzz.Ratio(query, building.Acronym)
);
}

// Apply the address filter if the parameter is provided
if (!string.IsNullOrEmpty(address))
var filter = FilterDefinition<Building>.Empty;
var buildings = await _buildings.Find(filter).ToListAsync();

if (!string.IsNullOrEmpty(query))
{
filter &= filterBuilder.Eq(b => b.Address, address);
buildings.Sort((b1, b2) =>
{
var s1 = FuzzScore(b1);
var s2 = FuzzScore(b2);

// Sort descending
return -s1.CompareTo(s2);
});
}

// Fetch the buildings from the database using the filter
return await _buildings.Find(filter).ToListAsync();
return buildings;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions Entities/Building.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class Building
[BsonElement("name"), BsonRepresentation(BsonType.String)]
public string? Name { get; set; }

[BsonElement("acronym"), BsonRepresentation(BsonType.String)]
public string? Acronym { get; set; }

[BsonElement("address"), BsonRepresentation(BsonType.String)]
public string? Address { get; set; }
}
1 change: 1 addition & 0 deletions SimpleWebAppReact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.33" />
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
Expand Down

0 comments on commit cc761a9

Please sign in to comment.