Get game
This commit is contained in:
@ -1,36 +1,33 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using NostrStreamer.ApiModel;
|
||||
using NostrStreamer.Services;
|
||||
|
||||
namespace NostrStreamer.Controllers;
|
||||
|
||||
[Route("/api/v1/games")]
|
||||
public class GameInfoController : Controller
|
||||
public class GameInfoController(GameDb gameDb) : Controller
|
||||
{
|
||||
private readonly GameDb _gameDb;
|
||||
|
||||
public GameInfoController(GameDb gameDb)
|
||||
{
|
||||
_gameDb = gameDb;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetGames([FromQuery] string q, [FromQuery] int limit = 10)
|
||||
{
|
||||
var data = await _gameDb.SearchGames(q, limit);
|
||||
var data = await gameDb.SearchGames(q, limit);
|
||||
|
||||
var mapped = data?.Select(a => new GameInfo()
|
||||
{
|
||||
Id = $"igdb:{a.Id}",
|
||||
Name = a.Name,
|
||||
Cover = $"https://images.igdb.com/igdb/image/upload/t_cover_big_2x/{a.Cover?.ImageId}.jpg",
|
||||
Genres = a.Genres.Select(b => b.Name).ToList()
|
||||
});
|
||||
var mapped = data?.Select(a => a.ToGameInfo());
|
||||
|
||||
return Json(mapped, new JsonSerializerSettings()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetGame([FromQuery] string id)
|
||||
{
|
||||
var data = await gameDb.GetGame(id);
|
||||
|
||||
return Json(data?.ToGameInfo(), new JsonSerializerSettings()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
});
|
||||
}
|
||||
}
|
@ -1,12 +1,14 @@
|
||||
using Amazon;
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
using Igdb;
|
||||
using MaxMind.GeoIP2;
|
||||
using Newtonsoft.Json;
|
||||
using Nostr.Client.Identifiers;
|
||||
using Nostr.Client.Json;
|
||||
using Nostr.Client.Keys;
|
||||
using Nostr.Client.Messages;
|
||||
using NostrStreamer.ApiModel;
|
||||
using NostrStreamer.Database;
|
||||
|
||||
namespace NostrStreamer;
|
||||
@ -119,6 +121,17 @@ public static class Extensions
|
||||
var ev = NostrJson.Deserialize<NostrEvent>(stream.Event);
|
||||
return ev!.ToIdentifier();
|
||||
}
|
||||
|
||||
public static GameInfo ToGameInfo(this Game a)
|
||||
{
|
||||
return new GameInfo
|
||||
{
|
||||
Id = $"igdb:{a.Id}",
|
||||
Name = a.Name,
|
||||
Cover = $"https://images.igdb.com/igdb/image/upload/t_cover_big_2x/{a.Cover?.ImageId}.jpg",
|
||||
Genres = a.Genres.Select(b => b.Name).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class Variant
|
||||
|
@ -77,6 +77,32 @@ public class GameDb
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<Game?> GetGame(string id)
|
||||
{
|
||||
await RefreshToken();
|
||||
|
||||
var req = new HttpRequestMessage(HttpMethod.Post, "https://api.igdb.com/v4/games.pb");
|
||||
req.Headers.Add("Client-ID", _config.ClientId);
|
||||
req.Headers.Authorization = new("Bearer", _currentToken?.AccessToken);
|
||||
req.Content = new StringContent($"fields id,cover.image_id,genres.name,name; where id = {id};");
|
||||
|
||||
var rsp = await _client.SendAsync(req);
|
||||
if (rsp.IsSuccessStatusCode)
|
||||
{
|
||||
var rspStream = await rsp.Content.ReadAsStreamAsync();
|
||||
|
||||
var ret = GameResult.Parser.ParseFrom(rspStream);
|
||||
return ret.Games.FirstOrDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
var content = await rsp.Content.ReadAsStringAsync();
|
||||
_logger.LogWarning("Failed to fetch games {msg}", content);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
class Token
|
||||
{
|
||||
|
Reference in New Issue
Block a user