// (C) Copyright 2019 C-xC-c // This file is part of BantFlags. // BantFlags is licensed under the GNU AGPL Version 3.0 or later. // see the LICENSE file or using BantFlags.Data; using BantFlags.Data.Database; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace BantFlags.Controllers { [ApiController] [Route("api")] public class FlagsController : Controller { private DatabaseService Database { get; } public FlagsController(DatabaseService db) { Database = db; } /// /// Retrives flags from the database from the posts sent in post_nrs /// /// The comma seperated list of post numbers from the thread. /// Currently should only be /bant/. Not checked here because we don't need to care what they send. /// The version of the userscript. [HttpPost] [Route("get")] [Consumes("application/x-www-form-urlencoded")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task Get([FromForm]string post_nrs, [FromForm]string board, [FromForm]int? version) { int ver = version ?? 0; if (ver > 1) { // Improved data structuring, see Docs/GetPosts return Json(await Database.GetPosts_V2(post_nrs, board)); } return Json(await Database.GetPosts_V1(post_nrs, board)); } /// /// Posts flags in the database. /// /// The post number to associate the flags to. /// Currently should only be /bant/. /// List of flags to associate with the post. Split by "||" in API V1 and "," in V2. /// The version of the userscript. [HttpPost] [Route("post")] [Consumes("application/x-www-form-urlencoded")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task Post([FromForm]string post_nr, [FromForm]string board, [FromForm]string regions, [FromForm]int? version) { string splitFlag = (version ?? 0) > 1 ? "," : "||"; // comma for v2+, else || for backwards compatibility. Result post = PostModel.Create(post_nr, board, regions, splitFlag, Database.KnownFlags, Database.Boards); if (post.Failed) { return Problem(post.ErrorMessage, statusCode: StatusCodes.Status400BadRequest); } await Database.InsertPost(post.Value); return Ok(post.Value); } /// /// Gets the list of supported flags. /// [HttpGet] [Route("flags")] [ProducesResponseType(StatusCodes.Status200OK)] public IActionResult Flags() => Ok(Database.FlagList); } }