diff options
author | crupest <crupest@outlook.com> | 2021-01-31 16:07:07 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-01-31 16:07:07 +0800 |
commit | 104c453b93b22f083b9cef53aa579070c037b777 (patch) | |
tree | 636e8869c8cecdbc89f7b037b1b271861c98dcb6 /BackEnd/Timeline | |
parent | 646b904dec11f59fe8c3a178b9dea735cdf31175 (diff) | |
download | timeline-104c453b93b22f083b9cef53aa579070c037b777.tar.gz timeline-104c453b93b22f083b9cef53aa579070c037b777.tar.bz2 timeline-104c453b93b22f083b9cef53aa579070c037b777.zip |
...
Diffstat (limited to 'BackEnd/Timeline')
-rw-r--r-- | BackEnd/Timeline/Controllers/TimelineController.cs | 12 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Validation/StringSetValidator.cs | 40 | ||||
-rw-r--r-- | BackEnd/Timeline/Program.cs | 54 |
3 files changed, 71 insertions, 35 deletions
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<ActionResult<List<HttpTimeline>>> TimelineList([FromQuery][Username] string? relate, [FromQuery][RegularExpression("(own)|(join)")] string? relateType, [FromQuery] string? visibility)
+ public async Task<ActionResult<List<HttpTimeline>>> TimelineList([FromQuery][Username] string? relate, [FromQuery][ValidationSet("own", "join", "default")] string? relateType, [FromQuery] string? visibility)
{
List<TimelineVisibility>? 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<TimelineUserRelationshipType>(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<string>
+ {
+ 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<IWebHostEnvironment>();
+ var env = host.Services.GetRequiredService<IWebHostEnvironment>();
- var databaseBackupService = host.Services.GetRequiredService<IDatabaseBackupService>();
- databaseBackupService.BackupNow();
+ var databaseBackupService = host.Services.GetRequiredService<IDatabaseBackupService>();
+ databaseBackupService.BackupNow();
- if (env.IsProduction())
- {
- using (var scope = host.Services.CreateScope())
- {
- var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
- databaseContext.Database.Migrate();
+ if (env.IsProduction())
+ {
+ using (var scope = host.Services.CreateScope())
+ {
+ var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
+ 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<Startup>();
+ });
}
-
- public static IHostBuilder CreateWebHostBuilder(string[] args) =>
- Host.CreateDefaultBuilder(args)
- .ConfigureAppConfiguration(config =>
- {
- config.AddEnvironmentVariables("Timeline_");
- })
- .ConfigureWebHostDefaults(webBuilder =>
- {
- webBuilder.UseStartup<Startup>();
- });
- }
}
|