mysimulation/server/FSO.Server.Api.Core/Utils/ApiResponse.cs
Tony Bark 22191ce648 Removed NioTSO client and server
- NioTSO client isn't needed because we're using RayLib
- Added FreeSO's API server to handle most backend operations
2024-05-01 02:55:43 -04:00

78 lines
No EOL
2.3 KiB
C#
Executable file

using FSO.Common.Utils;
using FSO.Server.Database.DA.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;
using System.Xml;
namespace FSO.Server.Api.Core.Utils
{
public class ApiResponse
{
public static IActionResult Plain(HttpStatusCode code, string text)
{
return new ContentResult
{
StatusCode = (int)code,
Content = text,
ContentType = "text/plain"
};
}
public static IActionResult Json(HttpStatusCode code, object obj)
{
return new ContentResult
{
StatusCode = (int)code,
Content = Newtonsoft.Json.JsonConvert.SerializeObject(obj),
ContentType = "application/json"
};
}
public static IActionResult PagedList<T>(HttpRequest request, HttpStatusCode code, PagedList<T> list)
{
request.HttpContext.Response.Headers.Add("X-Total-Count", list.Total.ToString());
request.HttpContext.Response.Headers.Add("X-Offset", list.Offset.ToString());
return new ContentResult
{
StatusCode = (int)code,
Content = Newtonsoft.Json.JsonConvert.SerializeObject(list),
ContentType = "application/json"
};
}
public static IActionResult Xml(HttpStatusCode code, IXMLEntity xml)
{
var doc = new XmlDocument();
var firstChild = xml.Serialize(doc);
doc.AppendChild(firstChild);
return new ContentResult
{
StatusCode = (int)code,
Content = doc.OuterXml,
ContentType = "text/xml"
};
}
public static Func<IActionResult> XmlFuture(HttpStatusCode code, IXMLEntity xml)
{
var doc = new XmlDocument();
var firstChild = xml.Serialize(doc);
doc.AppendChild(firstChild);
var serialized = doc.OuterXml;
return () =>
{
return new ContentResult
{
StatusCode = (int)code,
Content = serialized,
ContentType = "text/xml"
};
};
}
}
}