From a1f6b41accb47e4c1e1e0474148afa94732377da Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Apr 2022 21:52:26 +0800 Subject: ... --- BackEnd/Timeline/Controllers/Resource.Designer.cs | 9 + BackEnd/Timeline/Controllers/Resource.resx | 3 + BackEnd/Timeline/Controllers/TimelineController.cs | 16 +- BackEnd/Timeline/ErrorCodes.cs | 1 + .../Timeline/Models/Validation/NameValidator.cs | 10 +- .../Models/Validation/Resource.Designer.cs | 312 +++++++++++---------- BackEnd/Timeline/Models/Validation/Resource.resx | 3 + .../Models/Validation/TimelineNameValidator.cs | 7 +- .../Timeline/Services/Timeline/TimelineService.cs | 32 ++- 9 files changed, 228 insertions(+), 165 deletions(-) (limited to 'BackEnd') diff --git a/BackEnd/Timeline/Controllers/Resource.Designer.cs b/BackEnd/Timeline/Controllers/Resource.Designer.cs index eeb2f0fa..4c9da98a 100644 --- a/BackEnd/Timeline/Controllers/Resource.Designer.cs +++ b/BackEnd/Timeline/Controllers/Resource.Designer.cs @@ -111,6 +111,15 @@ namespace Timeline.Controllers { } } + /// + /// Looks up a localized string similar to Multiple timeline with the name exists. Please use new api to get timeline.. + /// + internal static string MessageMultipleTimeline { + get { + return ResourceManager.GetString("MessageMultipleTimeline", resourceCulture); + } + } + /// /// Looks up a localized string similar to The old password is wrong.. /// diff --git a/BackEnd/Timeline/Controllers/Resource.resx b/BackEnd/Timeline/Controllers/Resource.resx index b70e5230..004bd7cc 100644 --- a/BackEnd/Timeline/Controllers/Resource.resx +++ b/BackEnd/Timeline/Controllers/Resource.resx @@ -159,4 +159,7 @@ A user with given username already exists. + + Multiple timeline with the name exists. Please use new api to get timeline. + \ No newline at end of file diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index a6749706..c10a8793 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -117,13 +117,21 @@ namespace Timeline.Controllers /// The timeline info. [HttpGet("{timeline}")] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> TimelineGet([FromRoute][GeneralTimelineName] string timeline) { - var timelineId = await _service.GetTimelineIdByNameAsync(timeline); - var t = await _service.GetTimelineAsync(timelineId); - var result = await Map(t); - return result; + try + { + var timelineId = await _service.GetTimelineIdByNameAsync(timeline); + var t = await _service.GetTimelineAsync(timelineId); + var result = await Map(t); + return result; + } + catch (MultipleTimelineException) + { + return BadRequestWithCommonResponse(ErrorCodes.TimelineController.MultipleTimelineWithSameName, Resource.MessageMultipleTimeline); + } } /// diff --git a/BackEnd/Timeline/ErrorCodes.cs b/BackEnd/Timeline/ErrorCodes.cs index 9201979f..712ac374 100644 --- a/BackEnd/Timeline/ErrorCodes.cs +++ b/BackEnd/Timeline/ErrorCodes.cs @@ -79,6 +79,7 @@ public static class TimelineController { public const int QueryRelateNotExist = 1_104_04_01; + public const int MultipleTimelineWithSameName = 1_104_04_02; } public static class HighlightTimelineController diff --git a/BackEnd/Timeline/Models/Validation/NameValidator.cs b/BackEnd/Timeline/Models/Validation/NameValidator.cs index 2220de6f..c7c18738 100644 --- a/BackEnd/Timeline/Models/Validation/NameValidator.cs +++ b/BackEnd/Timeline/Models/Validation/NameValidator.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; namespace Timeline.Models.Validation @@ -7,10 +8,17 @@ namespace Timeline.Models.Validation { private static Regex UniqueIdRegex { get; } = new Regex(@"^[a-zA-Z0-9]{32}$"); + public List DisallowedNames { get; set; } = new List(); + public const int MaxLength = 26; protected override (bool, string) DoValidate(string value) { + if (DisallowedNames.Contains(value)) + { + return (false, Resource.NameDisallowed); + } + if (value.Length == 0) { return (false, Resource.NameCantBeEmpty); diff --git a/BackEnd/Timeline/Models/Validation/Resource.Designer.cs b/BackEnd/Timeline/Models/Validation/Resource.Designer.cs index 47ad4248..4f52e047 100644 --- a/BackEnd/Timeline/Models/Validation/Resource.Designer.cs +++ b/BackEnd/Timeline/Models/Validation/Resource.Designer.cs @@ -1,153 +1,159 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Timeline.Models.Validation { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resource { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resource() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Models.Validation.Resource", typeof(Resource).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to it can't be null. - /// - internal static string CantBeNull { - get { - return ResourceManager.GetString("CantBeNull", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to name can't be empty. - /// - internal static string NameCantBeEmpty { - get { - return ResourceManager.GetString("NameCantBeEmpty", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to name can't be longer than {0}. - /// - internal static string NameCantBeLongerThan { - get { - return ResourceManager.GetString("NameCantBeLongerThan", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to name can't has the same pattern of unique id. - /// - internal static string NameCantBeUniqueIdPattern { - get { - return ResourceManager.GetString("NameCantBeUniqueIdPattern", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to name can only consists of alphabet, number, '_' and '-' . - /// - internal static string NameInvalidChar { - get { - return ResourceManager.GetString("NameInvalidChar", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to nickname can't be longer than 25. - /// - internal static string NicknameTooLong { - get { - return ResourceManager.GetString("NicknameTooLong", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to it is not of type {0}. - /// - internal static string NotOfType { - get { - return ResourceManager.GetString("NotOfType", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Failed to create validator instance.. - /// - internal static string ValidateWithAttributeExceptionCreateFail { - get { - return ResourceManager.GetString("ValidateWithAttributeExceptionCreateFail", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Given type is not a IValidator instance.. - /// - internal static string ValidateWithAttributeExceptionNotValidator { - get { - return ResourceManager.GetString("ValidateWithAttributeExceptionNotValidator", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to validation passed. - /// - internal static string ValidationPassed { - get { - return ResourceManager.GetString("ValidationPassed", resourceCulture); - } - } - } -} +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Timeline.Models.Validation { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// This class was generated by MSBuild using the GenerateResource task. + /// To add or remove a member, edit your .resx file then rerun MSBuild. + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Build.Tasks.StronglyTypedResourceBuilder", "15.1.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resource { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resource() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Models.Validation.Resource", typeof(Resource).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to it can't be null. + /// + internal static string CantBeNull { + get { + return ResourceManager.GetString("CantBeNull", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can't be empty. + /// + internal static string NameCantBeEmpty { + get { + return ResourceManager.GetString("NameCantBeEmpty", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can't be longer than {0}. + /// + internal static string NameCantBeLongerThan { + get { + return ResourceManager.GetString("NameCantBeLongerThan", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can't has the same pattern of unique id. + /// + internal static string NameCantBeUniqueIdPattern { + get { + return ResourceManager.GetString("NameCantBeUniqueIdPattern", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to this special value is not allowed. + /// + internal static string NameDisallowed { + get { + return ResourceManager.GetString("NameDisallowed", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can only consists of alphabet, number, '_' and '-' . + /// + internal static string NameInvalidChar { + get { + return ResourceManager.GetString("NameInvalidChar", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to nickname can't be longer than 25. + /// + internal static string NicknameTooLong { + get { + return ResourceManager.GetString("NicknameTooLong", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to it is not of type {0}. + /// + internal static string NotOfType { + get { + return ResourceManager.GetString("NotOfType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Failed to create validator instance.. + /// + internal static string ValidateWithAttributeExceptionCreateFail { + get { + return ResourceManager.GetString("ValidateWithAttributeExceptionCreateFail", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Given type is not a IValidator instance.. + /// + internal static string ValidateWithAttributeExceptionNotValidator { + get { + return ResourceManager.GetString("ValidateWithAttributeExceptionNotValidator", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to validation passed. + /// + internal static string ValidationPassed { + get { + return ResourceManager.GetString("ValidationPassed", resourceCulture); + } + } + } +} diff --git a/BackEnd/Timeline/Models/Validation/Resource.resx b/BackEnd/Timeline/Models/Validation/Resource.resx index 68ba3810..9bec20a5 100644 --- a/BackEnd/Timeline/Models/Validation/Resource.resx +++ b/BackEnd/Timeline/Models/Validation/Resource.resx @@ -120,6 +120,9 @@ it can't be null + + this special value is not allowed + name can't be empty diff --git a/BackEnd/Timeline/Models/Validation/TimelineNameValidator.cs b/BackEnd/Timeline/Models/Validation/TimelineNameValidator.cs index f1ab54e8..7cd49fd2 100644 --- a/BackEnd/Timeline/Models/Validation/TimelineNameValidator.cs +++ b/BackEnd/Timeline/Models/Validation/TimelineNameValidator.cs @@ -1,5 +1,6 @@ using System; - +using System.Collections.Generic; + namespace Timeline.Models.Validation { public class TimelineNameValidator : NameValidator @@ -12,8 +13,8 @@ namespace Timeline.Models.Validation { public TimelineNameAttribute() : base(typeof(TimelineNameValidator)) - { - + { + } } } diff --git a/BackEnd/Timeline/Services/Timeline/TimelineService.cs b/BackEnd/Timeline/Services/Timeline/TimelineService.cs index bd838c7d..cdea39fa 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelineService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelineService.cs @@ -82,6 +82,15 @@ namespace Timeline.Services.Timeline { ["id"] = id }); + } + + protected static EntityNotExistException CreateTimelineNotExistException(long ownerId, string timelineName) + { + return new EntityNotExistException(EntityTypes.Timeline, new Dictionary + { + [nameof(ownerId)] = ownerId, + [nameof(timelineName)] = timelineName + }); } protected void CheckGeneralTimelineName(string timelineName, string? paramName) @@ -378,6 +387,10 @@ namespace Timeline.Services.Timeline throw new ArgumentNullException(nameof(timelineName)); CheckTimelineName(timelineName, nameof(timelineName)); + if (timelineName == "self") + { + throw new ArgumentException("Timeline name can't be 'self'."); + } var conflict = await _database.Timelines.AnyAsync(t => t.OwnerId == ownerId && t.Name == timelineName); @@ -405,14 +418,25 @@ namespace Timeline.Services.Timeline _logger.LogWarning(Resource.LogTimelineDelete, id); } - public Task GetTimelineIdAsync(long ownerId, string timelineName) + public async Task GetTimelineIdAsync(long ownerId, string timelineName) { - throw new NotImplementedException(); + if (timelineName is null) + throw new ArgumentNullException(nameof(timelineName)); + CheckTimelineName(timelineName, nameof(timelineName)); + + string? tn = timelineName == "self" ? null : timelineName; + + var entity = await _database.Timelines.Where(t => t.OwnerId == ownerId && t.Name == tn).SingleOrDefaultAsync(); + if (entity is null) + throw CreateTimelineNotExistException(ownerId, timelineName); + + return entity.Id; } - public Task GetTimelineIdAsync(string ownerUsername, string timelineName) + public async Task GetTimelineIdAsync(string ownerUsername, string timelineName) { - throw new NotImplementedException(); + var ownerId = await _userService.GetUserIdByUsernameAsync(ownerUsername); + return await GetTimelineIdAsync(ownerId, timelineName); } } } -- cgit v1.2.3