Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup building by name or acronym #34

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

/// <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 All @@ -57,7 +63,7 @@
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<ActionResult<Building?>> GetById(string id)

Check warning on line 66 in Controllers/BuildingController.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
// Simple validation to check if the ID is not null
if (string.IsNullOrEmpty(id))
Expand All @@ -78,7 +84,7 @@
[HttpPost]
public async Task<ActionResult> Post(Building building)
{
await _buildings.InsertOneAsync(building);

Check warning on line 87 in Controllers/BuildingController.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
return CreatedAtAction(nameof(GetById), new { id = building.Id }, building);

}
Expand All @@ -92,7 +98,7 @@
public async Task<ActionResult> Update(Building building)
{
var filter = Builders<Building>.Filter.Eq(x => x.Id, building.Id);
await _buildings.ReplaceOneAsync(filter, building);

Check warning on line 101 in Controllers/BuildingController.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
return Ok();
}

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
Loading