mysimulation/server/FSO.Server.Api.Core/Controllers/BulletinInfoController.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

134 lines
5.1 KiB
C#
Executable file

using FSO.Server.Api.Core.Utils;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FSO.Server.Database.DA.Bulletin;
namespace FSO.Server.Api.Core.Controllers
{
[EnableCors]
[ApiController]
public class BulletinInfoController : ControllerBase
{
[HttpGet("nhoodId")]
[Route("userapi/neighborhood/{nhoodId}/bulletins")]
public IActionResult GetByNhoodAndAfter(uint nhoodId, [FromQuery(Name = "after")]uint after)
{
var api = Api.INSTANCE;
using (var da = api.DAFactory.Get())
{
if (after == null) after = 0;
var bulletins = da.BulletinPosts.GetByNhoodId(nhoodId, after);
if (bulletins == null) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONBulletinError("Bulletins not found"));
List<JSONBulletin> bulletinJson = new List<JSONBulletin>();
foreach (var bulletin in bulletins)
{
bulletinJson.Add(new JSONBulletin
{
bulletin_id = bulletin.bulletin_id,
neighborhood_id = bulletin.neighborhood_id,
avatar_id = bulletin.avatar_id,
title = bulletin.title,
body = bulletin.body,
date = bulletin.date,
flags = bulletin.flags,
lot_id = bulletin.lot_id,
type = bulletin.type
});
}
var bulletinsJson = new JSONBulletins();
bulletinsJson.bulletins = bulletinJson;
return ApiResponse.Json(HttpStatusCode.OK, bulletinsJson);
}
}
[HttpGet]
[Route("userapi/neighborhood/{nhoodid}/bulletins/{bulletinId}")]
public IActionResult GetByID(uint bulletinId)
{
var api = Api.INSTANCE;
using (var da = api.DAFactory.Get())
{
var bulletin = da.BulletinPosts.Get(bulletinId);
if (bulletin == null) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONBulletinError("Bulletin not found"));
var bulletinJson = new JSONBulletin();
bulletinJson = new JSONBulletin
{
bulletin_id = bulletin.bulletin_id,
neighborhood_id = bulletin.neighborhood_id,
avatar_id = bulletin.avatar_id,
title = bulletin.title,
body = bulletin.body,
date = bulletin.date,
flags = bulletin.flags,
lot_id = bulletin.lot_id,
type = bulletin.type
};
return ApiResponse.Json(HttpStatusCode.OK, bulletinJson);
}
}
[HttpGet]
[Route("userapi/neighborhood/{nhoodId}/bulletins/type/{bulletinType}")]
public IActionResult GetByNhoodAndType(uint nhoodId,DbBulletinType bulletinType)
{
var api = Api.INSTANCE;
using (var da = api.DAFactory.Get())
{
var bulletins = da.BulletinPosts.GetByNhoodId(nhoodId, 0).Where(x => x.type == bulletinType);
if (bulletins == null) return ApiResponse.Json(HttpStatusCode.NotFound, new JSONBulletinError("Bulletins not found"));
List<JSONBulletin> bulletinJson = new List<JSONBulletin>();
foreach (var bulletin in bulletins)
{
bulletinJson.Add(new JSONBulletin
{
bulletin_id = bulletin.bulletin_id,
neighborhood_id = bulletin.neighborhood_id,
avatar_id = bulletin.avatar_id,
title = bulletin.title,
body = bulletin.body,
date = bulletin.date,
flags = bulletin.flags,
lot_id = bulletin.lot_id,
type = bulletin.type
});
}
var bulletinsJson = new JSONBulletins();
bulletinsJson.bulletins = bulletinJson;
return ApiResponse.Json(HttpStatusCode.OK, bulletinsJson);
}
}
}
public class JSONBulletinError
{
public string error;
public JSONBulletinError(string errorString)
{
error = errorString;
}
}
public class JSONBulletins
{
public List<JSONBulletin> bulletins { get; set; }
}
public class JSONBulletin
{
public uint bulletin_id { get; set; }
public int neighborhood_id { get; set; }
public uint? avatar_id { get; set; }
public string title { get; set; }
public string body { get; set; }
public uint date { get; set; }
public uint flags { get; set; }
public int? lot_id { get; set; }
public DbBulletinType type { get; set; }
}
}