Skip to content

refactor partialitem casting #27

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions src/Wfdf.Api/Controller/DropSourceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Mvc;
using Wfdf.Core.Models;
using Wfdf.Core.Service;

namespace Wfdf.Api.Controllers;

[Route("api/[controller]")]
[ApiController]
public class DropSourceController : ControllerBase
{
private readonly ILogger<DropSourceController> _logger;
private readonly ItemService _itemService;

public DropSourceController(ILogger<DropSourceController> logger, ItemService itemService)
{
_logger = logger;
_itemService = itemService;
}

[HttpGet]
public async Task<IEnumerable<PartialItem>> GetByLocationName(string locationName)
{
return await _itemService.FindItemsWithDropSourceAsync(locationName);
}
}
2 changes: 1 addition & 1 deletion src/Wfdf.Core/Models/DropSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public record DropSource
public string location { get; init; } = string.Empty;
public string rarity { get; init; } = string.Empty;
public string type { get; init; } = string.Empty;
}
}
3 changes: 3 additions & 0 deletions src/Wfdf.Core/Models/PartialItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

using MongoDB.Bson.Serialization.Attributes;

namespace Wfdf.Core.Models;

// This class is to be used where an item's full data is not needed (i.e item searches)
[BsonIgnoreExtraElements]
public record PartialItem
{
public string uniqueName { get; init; } = string.Empty;
Expand Down
11 changes: 8 additions & 3 deletions src/Wfdf.Core/Service/ItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public async Task<BulkWriteResult> UpsertItemsAsync(List<Item> items)
public async Task<IEnumerable<PartialItem>> SearchForItemAsync(string name)
{
var filter = Builders<Item>.Filter.Regex("name", new BsonRegularExpression(name, "i"));
var matches = await _items.Find(filter).ToListAsync();
return matches.Select(i => (PartialItem)i);
var matches = await _items.Find(filter).As<PartialItem>().ToListAsync();
return matches;
}

public async Task<Item> GetItemAsync(string uniqueName)
Expand All @@ -72,13 +72,18 @@ public async Task<IEnumerable<PartialItem>> GetRandomItemsAsync(int count)
var items = await _items.AsQueryable()
.Where(i => !categoryBlacklist.Contains(i.category))
.Sample(count)
.Select(i => (PartialItem)i)
.ToListAsync();
return items.Select(i => (PartialItem)i);
return items;
}

public async Task<IEnumerable<Item>> FindItemsWithComponentAsync(string componentUniqueName)
=> await _items.Find(Builders<Item>.Filter.ElemMatch(i => i.components, c => c.uniqueName.Equals(componentUniqueName)))
.ToListAsync();


public async Task<IEnumerable<PartialItem>> FindItemsWithDropSourceAsync(string location)
=> await _items.Find(Builders<Item>.Filter.ElemMatch(i => i.drops, d => d.location.Equals(location)))
.As<PartialItem>()
.ToListAsync();
}