Rename FlagModel to PostModel. Introduct Result<T> class to handle creation of PostModel objects instead of using a tuple.

dotnetflags
C-xC-c 4 years ago
parent c66bc732af
commit fe771d8bbe

@ -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<PostModel> 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);
}

@ -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())
{

@ -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;
}
/// <summary>
/// A wrapper around post validation so it's all in one place.
/// </summary>
public static (bool, FlagModel, string) Create(string post_nr, string board, string regions, string splitFlag, HashSet<string> 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);
}
}
}

@ -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<PostModel> Create(string post_nr, string board, string regions, string splitFlag, HashSet<string> knownFlags)
{
string[] empty = new string[] { "empty, or there were errors. Re-set your flags." };
if (!int.TryParse(post_nr, out int postNumber))
return Result<PostModel>.Fail("Invalid post number.");
if (board != "bant")
return Result<PostModel>.Fail("Invalid board parameter.");
if (regions == null)
return Result<PostModel>.Pass(new PostModel(postNumber, board, empty));
var flags = regions.Split(splitFlag);
if (flags.Count() > 30)
return Result<PostModel>.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<PostModel>.Pass(new PostModel(postNumber, board, empty));
}
return Result<PostModel>.Pass(new PostModel(postNumber, board, flags));
}
}
}

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BantFlags.Data
{
public struct Result<T>
{
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<T> Pass(T value) => new Result<T>(false, default, value);
public static Result<T> Fail(string error) => new Result<T>(true, error, default);
}
}

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
namespace BantFlags.Data
{

Loading…
Cancel
Save