Server Controller.

Added a controller for server data.
This commit is contained in:
JacobTech 2023-07-11 13:30:48 -04:00
parent 633fd3616e
commit 5ddae1b173
5 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,19 @@
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
public static class Server
{
public static TableColumn<long> ID { get; } = new("id", true) { DefaultValue = 0 };
public static TableColumn<PictureType> PictureType { get; } = new("picture_type") {DefaultValue = Enums.PictureType.png };
public static TableColumn<byte[]> Picture { get; } = new("picture") { DefaultValue = Array.Empty<byte>() };
public static TableColumn<string> Name { get; } = new("name") { DefaultValue = "Luski Server" };
public static TableColumn<string> Description { get; } = new("description") { DefaultValue = "description" };
public static TableColumn<long> Owner { get; } = new("owner") { DefaultValue = 0 };
}
[TableRow(typeof(Server))]
public partial class ServerRow
{}

View File

@ -6,6 +6,7 @@ namespace LuskiServer.Classes;
public static class Tables public static class Tables
{ {
public static Table<UserRow> Users { get; } = new("users", null!); public static Table<UserRow> Users { get; } = new("users", null!);
public static Table<ServerRow> Server { get; } = new("server", null!);
public static Table<RoleRow> Roles { get; } = new("roles", null!); public static Table<RoleRow> Roles { get; } = new("roles", null!);
public static Table<LogRow> Logs { get; } = new("logs", null!); public static Table<LogRow> Logs { get; } = new("logs", null!);
public static Table<FileRow> Files { get; } = new("files", null!); public static Table<FileRow> Files { get; } = new("files", null!);

View File

@ -0,0 +1,10 @@
using LuskiServer.Interfaces;
namespace LuskiServer.Classes.v1.OutGoing;
public class ServerInfoJson : IWebResponse
{
public string name { get; set; }
public string description { get; set; }
public long owner { get; set; }
}

View File

@ -0,0 +1,70 @@
using System.Net.Mime;
using Asp.Versioning;
using LuskiServer.Classes;
using LuskiServer.Classes.TableDef;
using LuskiServer.Classes.v1.OutGoing;
using LuskiServer.Enums;
using Microsoft.AspNetCore.Mvc;
namespace LuskiServer.Controllers.v1;
[ApiVersion(1)]
[ApiController]
public class SocketServerController : ControllerBase
{
/// <summary>
/// Server Info.
/// </summary>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(ServerInfoJson), StatusCodes.Status200OK)]
[Produces(MediaTypeNames.Application.Json)]
[Route(Luski.Info.Routes.Default.Base)]
public IActionResult Get()
{
var sr = Tables.Server.ReadRow(Server.ID.CreateParameter(0));
return this.ResponseToResult(new ServerInfoJson()
{
name = sr.Name,
description = sr.Description,
owner = sr.Owner
});
}
/// <summary>
/// Returns the icon for the server.
/// </summary>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(HTTPResponse), StatusCodes.Status403Forbidden)]
[Produces(MediaTypeNames.Image.Jpeg, MediaTypeNames.Image.Gif, MediaTypeNames.Image.Tiff, "image/png")]
[Route(Luski.Info.Routes.Default.Base + "/Icon")]
public IActionResult Icon()
{
if (Tables.Server.TryRead(Server.Picture, out byte[]? image, Server.ID.CreateParameter(0)))
{
return Tables.Server.Read(Server.PictureType, Server.ID.CreateParameter(0)) switch
{
PictureType.png => File(image, "image/png"),
PictureType.jpeg => File(image, "image/jpeg"),
PictureType.bmp => File(image, "image/bmp"),
PictureType.gif => File(image, "image/gif"),
PictureType.ico => File(image, "image/vnd.microsoft.icon"),
PictureType.svg => File(image, "image/svg+xml"),
PictureType.tif => File(image, "image/tiff"),
PictureType.webp => File(image, "image/webp"),
//should never happen
_ => File(image, "image/png"),
};
}
else
{
return StatusCode(403, new HTTPResponse()
{
error = ErrorCode.Forbidden,
error_message = "the user you have given does not exist"
});
}
}
}

View File

@ -46,6 +46,11 @@ foreach (PropertyInfo prop in typeof(Tables).GetProperties())
Luski.Database.RegisterTables(); Luski.Database.RegisterTables();
if (!Tables.Server.TryRead(Server.ID, out _, Server.ID.CreateParameter(0)))
{
Tables.Server.Insert();
}
if (!Luski.Database.VersionsTable.TryRead(Luski.Database.VersionsTable.ID, out _, if (!Luski.Database.VersionsTable.TryRead(Luski.Database.VersionsTable.ID, out _,
Luski.Database.VersionsTable.ID.CreateParameter(0))) Luski.Database.VersionsTable.ID.CreateParameter(0)))
{ {