diff --git a/gaseous-server/Classes/Metadata/Games.cs b/gaseous-server/Classes/Metadata/Games.cs index ba235de..9e50b98 100644 --- a/gaseous-server/Classes/Metadata/Games.cs +++ b/gaseous-server/Classes/Metadata/Games.cs @@ -24,7 +24,22 @@ namespace gaseous_server.Classes.Metadata { if (Id == 0) { - return null; + Game returnValue = new Game(); + if ((Storage.GetCacheStatus("game", 0) == Storage.CacheStatus.NotPresent) || (forceRefresh == true)) + { + returnValue = new Game + { + Id = 0, + Name = "Unknown" + }; + Storage.NewCacheValue(returnValue); + + return returnValue; + } + else + { + return Storage.GetCacheValue(returnValue, "id", 0); + } } else { diff --git a/gaseous-server/Classes/Metadata/Platforms.cs b/gaseous-server/Classes/Metadata/Platforms.cs index 93ad177..4182a93 100644 --- a/gaseous-server/Classes/Metadata/Platforms.cs +++ b/gaseous-server/Classes/Metadata/Platforms.cs @@ -26,7 +26,22 @@ namespace gaseous_server.Classes.Metadata { if (Id == 0) { - return null; + Platform returnValue = new Platform(); + if (Storage.GetCacheStatus("platform", 0) == Storage.CacheStatus.NotPresent) + { + returnValue = new Platform + { + Id = 0, + Name = "Unknown" + }; + Storage.NewCacheValue(returnValue); + + return returnValue; + } + else + { + return Storage.GetCacheValue(returnValue, "id", 0); + } } else { diff --git a/gaseous-server/Controllers/PlatformsController.cs b/gaseous-server/Controllers/PlatformsController.cs new file mode 100644 index 0000000..650ea7d --- /dev/null +++ b/gaseous-server/Controllers/PlatformsController.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using gaseous_server.Classes.Metadata; +using gaseous_tools; +using IGDB.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; + +namespace gaseous_server.Controllers +{ + [Route("api/v1/[controller]")] + [ApiController] + public class PlatformsController : Controller + { + [HttpGet] + [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] + public ActionResult Platform() + { + Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString); + + string sql = "SELECT * FROM gaseous.platform WHERE id IN (SELECT DISTINCT platformid FROM games_roms) ORDER BY `name` ASC;"; + + List RetVal = new List(); + + DataTable dbResponse = db.ExecuteCMD(sql); + foreach (DataRow dr in dbResponse.Rows) + { + RetVal.Add(Classes.Metadata.Platforms.GetPlatform((long)dr["id"])); + } + + return Ok(RetVal); + } + + [HttpGet] + [Route("{PlatformId}")] + [ProducesResponseType(typeof(Platform), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public ActionResult Platform(long PlatformId) + { + try + { + IGDB.Models.Platform platformObject = Classes.Metadata.Platforms.GetPlatform(PlatformId); + + if (platformObject != null) + { + return Ok(platformObject); + } + else + { + return NotFound(); + } + } + catch + { + return NotFound(); + } + } + + [HttpGet] + [Route("{PlatformId}/platformlogo")] + [ProducesResponseType(typeof(PlatformLogo), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public ActionResult PlatformLogo(long PlatformId) + { + try + { + IGDB.Models.Platform platformObject = Classes.Metadata.Platforms.GetPlatform(PlatformId); + if (platformObject != null) + { + IGDB.Models.PlatformLogo logoObject = PlatformLogos.GetPlatformLogo(platformObject.PlatformLogo.Id, Config.LibraryConfiguration.LibraryMetadataDirectory_Platform(platformObject)); + if (logoObject != null) + { + return Ok(logoObject); + } + else + { + return NotFound(); + } + } + else + { + return NotFound(); + } + } + catch + { + return NotFound(); + } + } + + [HttpGet] + [Route("{PlatformId}/platformlogo/image")] + [ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public ActionResult PlatformLogoImage(long PlatformId) + { + try + { + IGDB.Models.Platform platformObject = Classes.Metadata.Platforms.GetPlatform(PlatformId); + + string logoFilePath = Path.Combine(Config.LibraryConfiguration.LibraryMetadataDirectory_Platform(platformObject), "Logo_Medium.png"); + if (System.IO.File.Exists(logoFilePath)) + { + string filename = "Logo.png"; + string filepath = logoFilePath; + byte[] filedata = System.IO.File.ReadAllBytes(filepath); + string contentType = "image/png"; + + var cd = new System.Net.Mime.ContentDisposition + { + FileName = filename, + Inline = true, + }; + + Response.Headers.Add("Content-Disposition", cd.ToString()); + + return File(filedata, contentType); + } + else + { + return NotFound(); + } + } + catch + { + return NotFound(); + } + } + } +} + diff --git a/gaseous-server/Program.cs b/gaseous-server/Program.cs index 00eea39..1cb716e 100644 --- a/gaseous-server/Program.cs +++ b/gaseous-server/Program.cs @@ -59,6 +59,10 @@ app.MapControllers(); // setup library directories Config.LibraryConfiguration.InitLibrary(); +// insert unknown platform and game if not present +gaseous_server.Classes.Metadata.Games.GetGame(0, false, true); +gaseous_server.Classes.Metadata.Platforms.GetPlatform(0); + // organise library //gaseous_server.Classes.ImportGame.OrganiseLibrary();