diff options
Diffstat (limited to 'BackEnd')
-rw-r--r-- | BackEnd/Timeline/Controllers/Resource.Designer.cs | 9 | ||||
-rw-r--r-- | BackEnd/Timeline/Controllers/Resource.resx | 3 | ||||
-rw-r--r-- | BackEnd/Timeline/Controllers/TimelineController.cs | 16 | ||||
-rw-r--r-- | BackEnd/Timeline/ErrorCodes.cs | 1 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Validation/NameValidator.cs | 10 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Validation/Resource.Designer.cs | 312 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Validation/Resource.resx | 3 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Validation/TimelineNameValidator.cs | 7 | ||||
-rw-r--r-- | BackEnd/Timeline/Services/Timeline/TimelineService.cs | 32 |
9 files changed, 228 insertions, 165 deletions
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 @@ -112,6 +112,15 @@ namespace Timeline.Controllers { } /// <summary> + /// Looks up a localized string similar to Multiple timeline with the name exists. Please use new api to get timeline.. + /// </summary> + internal static string MessageMultipleTimeline { + get { + return ResourceManager.GetString("MessageMultipleTimeline", resourceCulture); + } + } + + /// <summary> /// Looks up a localized string similar to The old password is wrong.. /// </summary> internal static string MessageOldPasswordWrong { 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 @@ <data name="MessageUsernameConflict" xml:space="preserve">
<value>A user with given username already exists.</value>
</data>
+ <data name="MessageMultipleTimeline" xml:space="preserve">
+ <value>Multiple timeline with the name exists. Please use new api to get timeline.</value>
+ </data>
</root>
\ 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 /// <returns>The timeline info.</returns>
[HttpGet("{timeline}")]
[ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<HttpTimeline>> 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); + }
}
/// <summary>
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<string> DisallowedNames { get; set; } = new List<string>();
+
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 @@ -//------------------------------------------------------------------------------
-// <auto-generated>
-// 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.
-// </auto-generated>
-//------------------------------------------------------------------------------
-
-namespace Timeline.Models.Validation {
- using System;
-
-
- /// <summary>
- /// A strongly-typed resource class, for looking up localized strings, etc.
- /// </summary>
- // 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() {
- }
-
- /// <summary>
- /// Returns the cached ResourceManager instance used by this class.
- /// </summary>
- [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;
- }
- }
-
- /// <summary>
- /// Overrides the current thread's CurrentUICulture property for all
- /// resource lookups using this strongly typed resource class.
- /// </summary>
- [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
- internal static global::System.Globalization.CultureInfo Culture {
- get {
- return resourceCulture;
- }
- set {
- resourceCulture = value;
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to it can't be null.
- /// </summary>
- internal static string CantBeNull {
- get {
- return ResourceManager.GetString("CantBeNull", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to name can't be empty.
- /// </summary>
- internal static string NameCantBeEmpty {
- get {
- return ResourceManager.GetString("NameCantBeEmpty", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to name can't be longer than {0}.
- /// </summary>
- internal static string NameCantBeLongerThan {
- get {
- return ResourceManager.GetString("NameCantBeLongerThan", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to name can't has the same pattern of unique id.
- /// </summary>
- internal static string NameCantBeUniqueIdPattern {
- get {
- return ResourceManager.GetString("NameCantBeUniqueIdPattern", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to name can only consists of alphabet, number, '_' and '-' .
- /// </summary>
- internal static string NameInvalidChar {
- get {
- return ResourceManager.GetString("NameInvalidChar", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to nickname can't be longer than 25.
- /// </summary>
- internal static string NicknameTooLong {
- get {
- return ResourceManager.GetString("NicknameTooLong", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to it is not of type {0}.
- /// </summary>
- internal static string NotOfType {
- get {
- return ResourceManager.GetString("NotOfType", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to Failed to create validator instance..
- /// </summary>
- internal static string ValidateWithAttributeExceptionCreateFail {
- get {
- return ResourceManager.GetString("ValidateWithAttributeExceptionCreateFail", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to Given type is not a IValidator instance..
- /// </summary>
- internal static string ValidateWithAttributeExceptionNotValidator {
- get {
- return ResourceManager.GetString("ValidateWithAttributeExceptionNotValidator", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to validation passed.
- /// </summary>
- internal static string ValidationPassed {
- get {
- return ResourceManager.GetString("ValidationPassed", resourceCulture);
- }
- }
- }
-}
+//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace Timeline.Models.Validation { + using System; + + + /// <summary> + /// 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. + /// </summary> + [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() { + } + + /// <summary> + /// Returns the cached ResourceManager instance used by this class. + /// </summary> + [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; + } + } + + /// <summary> + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// <summary> + /// Looks up a localized string similar to it can't be null. + /// </summary> + internal static string CantBeNull { + get { + return ResourceManager.GetString("CantBeNull", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to name can't be empty. + /// </summary> + internal static string NameCantBeEmpty { + get { + return ResourceManager.GetString("NameCantBeEmpty", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to name can't be longer than {0}. + /// </summary> + internal static string NameCantBeLongerThan { + get { + return ResourceManager.GetString("NameCantBeLongerThan", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to name can't has the same pattern of unique id. + /// </summary> + internal static string NameCantBeUniqueIdPattern { + get { + return ResourceManager.GetString("NameCantBeUniqueIdPattern", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to this special value is not allowed. + /// </summary> + internal static string NameDisallowed { + get { + return ResourceManager.GetString("NameDisallowed", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to name can only consists of alphabet, number, '_' and '-' . + /// </summary> + internal static string NameInvalidChar { + get { + return ResourceManager.GetString("NameInvalidChar", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to nickname can't be longer than 25. + /// </summary> + internal static string NicknameTooLong { + get { + return ResourceManager.GetString("NicknameTooLong", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to it is not of type {0}. + /// </summary> + internal static string NotOfType { + get { + return ResourceManager.GetString("NotOfType", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Failed to create validator instance.. + /// </summary> + internal static string ValidateWithAttributeExceptionCreateFail { + get { + return ResourceManager.GetString("ValidateWithAttributeExceptionCreateFail", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Given type is not a IValidator instance.. + /// </summary> + internal static string ValidateWithAttributeExceptionNotValidator { + get { + return ResourceManager.GetString("ValidateWithAttributeExceptionNotValidator", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to validation passed. + /// </summary> + 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 @@ <data name="CantBeNull" xml:space="preserve">
<value>it can't be null</value>
</data>
+ <data name="NameDisallowed" xml:space="preserve">
+ <value>this special value is not allowed</value>
+ </data>
<data name="NameCantBeEmpty" xml:space="preserve">
<value>name can't be empty</value>
</data>
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<string, object>
+ {
+ [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<long> GetTimelineIdAsync(long ownerId, string timelineName) + public async Task<long> 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<long> GetTimelineIdAsync(string ownerUsername, string timelineName) + public async Task<long> GetTimelineIdAsync(string ownerUsername, string timelineName) { - throw new NotImplementedException(); + var ownerId = await _userService.GetUserIdByUsernameAsync(ownerUsername); + return await GetTimelineIdAsync(ownerId, timelineName); } }
}
|