diff --git a/BantFlags/Controllers/FlagsController.cs b/BantFlags/Controllers/FlagsController.cs index 19c24d8..cc4c0b5 100644 --- a/BantFlags/Controllers/FlagsController.cs +++ b/BantFlags/Controllers/FlagsController.cs @@ -59,14 +59,14 @@ namespace BantFlags.Controllers { string splitFlag = (version ?? 0) > 1 ? "," : "||"; // comma for v2+, else || for backwards compatibility. - (bool isValid, FlagModel post, string errorMessage) = FlagModel.Create(post_nr, board, regions, splitFlag, Database.KnownFlags); + Result post = PostModel.Create(post_nr, board, regions, splitFlag, Database.KnownFlags); - if (!isValid) + if (post.Failed) { - return Problem(errorMessage, statusCode: StatusCodes.Status400BadRequest); + return Problem(post.ErrorMessage, statusCode: StatusCodes.Status400BadRequest); } - await Database.InsertPost(post); + //await Database.InsertPost(post.Value); return Ok(post); } diff --git a/BantFlags/Data/Database/DatabaseService.cs b/BantFlags/Data/Database/DatabaseService.cs index 4f81503..dc4eaee 100644 --- a/BantFlags/Data/Database/DatabaseService.cs +++ b/BantFlags/Data/Database/DatabaseService.cs @@ -53,7 +53,7 @@ namespace BantFlags.Data.Database .ExecuteNonQueryAsync(reuse: true)); } - public async Task InsertPost(FlagModel post) + public async Task InsertPost(PostModel post) { using (var rentedConnection = await ConnectionPool.RentConnectionAsync()) { diff --git a/BantFlags/Data/FlagModel.cs b/BantFlags/Data/FlagModel.cs deleted file mode 100644 index a57ca95..0000000 --- a/BantFlags/Data/FlagModel.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace BantFlags.Data -{ - public class FlagModel - { - public int PostNumber { get; private set; } - - public string Board { get; private set; } - - public string[] Flags { get; private set; } - - private FlagModel(int post_nr, string board, string[] flags) - { - PostNumber = post_nr; - Board = board; - Flags = flags; - } - - /// - /// A wrapper around post validation so it's all in one place. - /// - public static (bool, FlagModel, string) Create(string post_nr, string board, string regions, string splitFlag, HashSet knownFlags) - { - if (!int.TryParse(post_nr, out int postNumber)) - return (false, default, "Invalid post number."); - - if (board != "bant") - return (false, default, "Invalid board parameter."); - - if (regions == null) - regions = "somebrokenflagstringsothatwegettheemptyflagwhenweshould"; - - var flags = regions.Split(splitFlag); - - if (flags.Count() > 30) - return (false, default, "Too many flags."); - - foreach (string flag in flags) - { - if (!knownFlags.Contains(flag)) // Not ideal but it's better than doing it in the controller or passing the database here. - { - flags = new string[] { "empty, or there were errors. Re-set your flags." }; - break; - } - } - - return (true, new FlagModel(postNumber, board, flags), default); - } - } -} \ No newline at end of file diff --git a/BantFlags/Data/PostModel.cs b/BantFlags/Data/PostModel.cs new file mode 100644 index 0000000..6484347 --- /dev/null +++ b/BantFlags/Data/PostModel.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace BantFlags.Data +{ + public class PostModel + { + public int PostNumber { get; private set; } + + public string Board { get; private set; } + + public string[] Flags { get; private set; } + + private PostModel(int post_nr, string board, string[] flags) + { + PostNumber = post_nr; + Board = board; + Flags = flags; + } + + public static Result Create(string post_nr, string board, string regions, string splitFlag, HashSet knownFlags) + { + string[] empty = new string[] { "empty, or there were errors. Re-set your flags." }; + + if (!int.TryParse(post_nr, out int postNumber)) + return Result.Fail("Invalid post number."); + + if (board != "bant") + return Result.Fail("Invalid board parameter."); + + if (regions == null) + return Result.Pass(new PostModel(postNumber, board, empty)); + + var flags = regions.Split(splitFlag); + + if (flags.Count() > 30) + return Result.Fail("Too many flags."); + + foreach (string flag in flags) + { + if (!knownFlags.Contains(flag)) // Not ideal but it's better than doing it in the controller or passing the database here. + return Result.Pass(new PostModel(postNumber, board, empty)); + } + + return Result.Pass(new PostModel(postNumber, board, flags)); + } + } +} \ No newline at end of file diff --git a/BantFlags/Data/ResultT.cs b/BantFlags/Data/ResultT.cs new file mode 100644 index 0000000..2777d8d --- /dev/null +++ b/BantFlags/Data/ResultT.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BantFlags.Data +{ + public struct Result + { + public bool Failed { get; private set; } + + public string ErrorMessage { get; private set; } + + private T _Value { get; set; } + + public T Value => Failed ? throw new Exception() : _Value; + + public Result(bool failed, string error, T value) + { + Failed = failed; + ErrorMessage = error; + _Value = value; + } + + public static Result Pass(T value) => new Result(false, default, value); + + public static Result Fail(string error) => new Result(true, error, default); + } +} \ No newline at end of file diff --git a/BantFlags/Data/Staging.cs b/BantFlags/Data/Staging.cs index 0cb214c..dbdc0e6 100644 --- a/BantFlags/Data/Staging.cs +++ b/BantFlags/Data/Staging.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace BantFlags.Data {