From 104c453b93b22f083b9cef53aa579070c037b777 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 31 Jan 2021 16:07:07 +0800 Subject: ... --- BackEnd/Timeline/Controllers/TimelineController.cs | 12 ++--- .../Models/Validation/StringSetValidator.cs | 40 ++++++++++++++++ BackEnd/Timeline/Program.cs | 54 +++++++++++----------- 3 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 BackEnd/Timeline/Models/Validation/StringSetValidator.cs (limited to 'BackEnd/Timeline') diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index 06ab8004..8479ca83 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Timeline.Filters; using Timeline.Models; @@ -54,7 +53,7 @@ namespace Timeline.Controllers [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] - public async Task>> TimelineList([FromQuery][Username] string? relate, [FromQuery][RegularExpression("(own)|(join)")] string? relateType, [FromQuery] string? visibility) + public async Task>> TimelineList([FromQuery][Username] string? relate, [FromQuery][ValidationSet("own", "join", "default")] string? relateType, [FromQuery] string? visibility) { List? visibilityFilter = null; if (visibility != null) @@ -92,12 +91,9 @@ namespace Timeline.Controllers { var relatedUserId = await _userService.GetUserIdByUsername(relate); - relationship = new TimelineUserRelationship(relateType switch - { - "own" => TimelineUserRelationshipType.Own, - "join" => TimelineUserRelationshipType.Join, - _ => TimelineUserRelationshipType.Default - }, relatedUserId); + var relationType = relateType is null ? TimelineUserRelationshipType.Default : Enum.Parse(relateType, true); + + relationship = new TimelineUserRelationship(relationType, relatedUserId); } catch (UserNotExistException) { diff --git a/BackEnd/Timeline/Models/Validation/StringSetValidator.cs b/BackEnd/Timeline/Models/Validation/StringSetValidator.cs new file mode 100644 index 00000000..363ece10 --- /dev/null +++ b/BackEnd/Timeline/Models/Validation/StringSetValidator.cs @@ -0,0 +1,40 @@ +using System; +using System.Linq; + +namespace Timeline.Models.Validation +{ + public class StringSetValidator : Validator + { + public StringSetValidator(params string[] set) + { + Set = set; + } + +#pragma warning disable CA1819 // Properties should not return arrays + public string[] Set { get; set; } +#pragma warning restore CA1819 // Properties should not return arrays + + protected override (bool, string) DoValidate(string value) + { + var contains = Set.Contains(value, StringComparer.OrdinalIgnoreCase); + if (!contains) + { + return (false, "Not a valid value."); + } + else + { + return (true, GetSuccessMessage()); + } + } + } + + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, + AllowMultiple = false)] + public class ValidationSetAttribute : ValidateWithAttribute + { + public ValidationSetAttribute(params string[] set) : base(new StringSetValidator(set)) + { + + } + } +} diff --git a/BackEnd/Timeline/Program.cs b/BackEnd/Timeline/Program.cs index 1f0f72b9..75bf6154 100644 --- a/BackEnd/Timeline/Program.cs +++ b/BackEnd/Timeline/Program.cs @@ -11,38 +11,38 @@ using Timeline.Services; namespace Timeline { - public static class Program - { - public static void Main(string[] args) + public static class Program { - var host = CreateWebHostBuilder(args).Build(); + public static void Main(string[] args) + { + var host = CreateWebHostBuilder(args).Build(); - var env = host.Services.GetRequiredService(); + var env = host.Services.GetRequiredService(); - var databaseBackupService = host.Services.GetRequiredService(); - databaseBackupService.BackupNow(); + var databaseBackupService = host.Services.GetRequiredService(); + databaseBackupService.BackupNow(); - if (env.IsProduction()) - { - using (var scope = host.Services.CreateScope()) - { - var databaseContext = scope.ServiceProvider.GetRequiredService(); - databaseContext.Database.Migrate(); + if (env.IsProduction()) + { + using (var scope = host.Services.CreateScope()) + { + var databaseContext = scope.ServiceProvider.GetRequiredService(); + databaseContext.Database.Migrate(); + } + } + + host.Run(); } - } - host.Run(); + public static IHostBuilder CreateWebHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration(config => + { + config.AddEnvironmentVariables("Timeline_"); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } - - public static IHostBuilder CreateWebHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureAppConfiguration(config => - { - config.AddEnvironmentVariables("Timeline_"); - }) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } } -- cgit v1.2.3