mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using RadicadeServer.DB;
|
|
using RadicadeServer.Extensions;
|
|
using RadicadeServer.RecNet.Enums.Inventions;
|
|
using RadicadeServer.RecNet.Inventions;
|
|
|
|
namespace RadicadeServer.API.Inventions.V2
|
|
{
|
|
[Route("api/inventions/v2")]
|
|
public class InventionsV2(RecDBContext db) : Controller
|
|
{
|
|
[HttpGet("search")]
|
|
public async Task<List<Invention>> SearchInventions([FromQuery] int? take, [FromQuery] string? value)
|
|
{
|
|
if (take == null) take = 50;
|
|
|
|
List<InventionDB>? foundInventions = null;
|
|
|
|
if (value != null)
|
|
{
|
|
foundInventions = await db.Inventions.Where(x => !x.HideFromPlayer && x.IsPublished &&
|
|
(x.Name.Contains(value) || x.Description.Contains(value) || x.Tags.Contains(value))
|
|
).OrderByDescending(x => x.ModifiedAt).Take((int)take).ToListAsync();
|
|
}
|
|
else
|
|
{
|
|
foundInventions = await db.Inventions.Where(x => !x.HideFromPlayer && x.IsPublished)
|
|
.OrderByDescending(x => x.ModifiedAt).Take((int)take).ToListAsync();
|
|
}
|
|
|
|
var converted = new List<Invention>();
|
|
foreach (var i in foundInventions)
|
|
{
|
|
var invention = i.ToInvention();
|
|
converted.Add(invention);
|
|
}
|
|
|
|
return converted;
|
|
}
|
|
|
|
[HttpGet("batch"), Authorize]
|
|
public async Task<List<Invention>?> GetBatchInventions_V2([FromQuery] List<long> id)
|
|
{
|
|
var userId = User.GetUserId();
|
|
|
|
var inventions = new List<Invention>();
|
|
|
|
foreach (var i in id)
|
|
{
|
|
var invention = await db.Inventions.FirstOrDefaultAsync(x => x.InventionId == i);
|
|
if (invention == null) continue;
|
|
inventions.Add(invention.ToInvention());
|
|
}
|
|
|
|
return inventions;
|
|
}
|
|
|
|
[HttpGet("mine"), Authorize]
|
|
public async Task<List<Invention>> GetOwnedInventions()
|
|
{
|
|
var userId = User.GetUserId();
|
|
|
|
var unlocks = await db.Unlocks.FirstOrDefaultAsync(x => x.AccountId == userId);
|
|
if (unlocks == null) return [];
|
|
|
|
var ownedInventions = unlocks.Inventions.FromJson<List<long>>() ?? new List<long>();
|
|
|
|
var allInventions = new List<Invention>();
|
|
foreach (var invention in ownedInventions)
|
|
{
|
|
var foundInvention = await db.Inventions.FirstOrDefaultAsync(x => x.InventionId == invention);
|
|
if (foundInvention != null)
|
|
allInventions.Add(foundInvention.ToInvention());
|
|
}
|
|
|
|
return allInventions;
|
|
}
|
|
}
|
|
}
|