From eba8e9698c09b805d8ac2a8f58db93b947ac29e3 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 13 Jun 2020 00:28:35 +0800 Subject: refactor(back): Fix #100 . --- Timeline/Controllers/TimelineController.cs | 3 +- Timeline/Controllers/TokenController.cs | 1 + Timeline/Controllers/UserAvatarController.cs | 1 + Timeline/Controllers/UserController.cs | 5 +- Timeline/Filters/Timeline.cs | 2 +- Timeline/Properties/launchSettings.json | 38 +++--- Timeline/Resources/Services/Exception.Designer.cs | 36 ------ Timeline/Resources/Services/Exception.resx | 12 -- Timeline/Resources/Services/Exceptions.Designer.cs | 135 +++++++++++++++++++++ Timeline/Resources/Services/Exceptions.resx | 124 +++++++++++++++++++ Timeline/Services/ConflictException.cs | 21 ---- Timeline/Services/EntityNames.cs | 14 +++ .../Services/Exceptions/EntityAlreadyExistError.cs | 63 ++++++++++ .../Services/Exceptions/EntityNotExistError.cs | 55 +++++++++ .../Services/Exceptions/ExceptionMessageHelper.cs | 13 ++ .../Exceptions/TimelineNotExistException.cs | 21 ++++ .../Exceptions/TimelinePostNotExistException.cs | 33 +++++ .../Services/Exceptions/UserNotExistException.cs | 40 ++++++ Timeline/Services/TimelineNotExistException.cs | 19 --- Timeline/Services/TimelinePostNotExistException.cs | 28 ----- Timeline/Services/TimelineService.cs | 9 +- Timeline/Services/UserNotExistException.cs | 41 ------- Timeline/Services/UserService.cs | 3 +- Timeline/Services/UserTokenManager.cs | 1 + Timeline/Timeline.csproj | 9 ++ 25 files changed, 542 insertions(+), 185 deletions(-) create mode 100644 Timeline/Resources/Services/Exceptions.Designer.cs create mode 100644 Timeline/Resources/Services/Exceptions.resx delete mode 100644 Timeline/Services/ConflictException.cs create mode 100644 Timeline/Services/EntityNames.cs create mode 100644 Timeline/Services/Exceptions/EntityAlreadyExistError.cs create mode 100644 Timeline/Services/Exceptions/EntityNotExistError.cs create mode 100644 Timeline/Services/Exceptions/ExceptionMessageHelper.cs create mode 100644 Timeline/Services/Exceptions/TimelineNotExistException.cs create mode 100644 Timeline/Services/Exceptions/TimelinePostNotExistException.cs create mode 100644 Timeline/Services/Exceptions/UserNotExistException.cs delete mode 100644 Timeline/Services/TimelineNotExistException.cs delete mode 100644 Timeline/Services/TimelinePostNotExistException.cs delete mode 100644 Timeline/Services/UserNotExistException.cs diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 5b894e26..6c7cfa95 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -13,6 +13,7 @@ using Timeline.Models; using Timeline.Models.Http; using Timeline.Models.Validation; using Timeline.Services; +using Timeline.Services.Exceptions; namespace Timeline.Controllers { @@ -283,7 +284,7 @@ namespace Timeline.Controllers var result = _mapper.Map(timeline); return result; } - catch (ConflictException) + catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.Timeline) { return BadRequest(ErrorResponse.TimelineController.NameConflict()); } diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index 1fb0b17a..cd67225c 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Timeline.Helpers; using Timeline.Models.Http; using Timeline.Services; +using Timeline.Services.Exceptions; using static Timeline.Resources.Controllers.TokenController; namespace Timeline.Controllers diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index 4062837b..b2e2e852 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -10,6 +10,7 @@ using Timeline.Helpers; using Timeline.Models.Http; using Timeline.Models.Validation; using Timeline.Services; +using Timeline.Services.Exceptions; using static Timeline.Resources.Controllers.UserAvatarController; namespace Timeline.Controllers diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 0bc8bcda..c8c1e610 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -11,6 +11,7 @@ using Timeline.Models; using Timeline.Models.Http; using Timeline.Models.Validation; using Timeline.Services; +using Timeline.Services.Exceptions; using static Timeline.Resources.Controllers.UserController; using static Timeline.Resources.Messages; @@ -70,7 +71,7 @@ namespace Timeline.Controllers _logger.LogInformation(e, Log.Format(LogPatchUserNotExist, ("Username", username))); return NotFound(ErrorResponse.UserCommon.NotExist()); } - catch (ConflictException) + catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User) { return BadRequest(ErrorResponse.UserController.UsernameConflict()); } @@ -116,7 +117,7 @@ namespace Timeline.Controllers var user = await _userService.CreateUser(_mapper.Map(body)); return Ok(ConvertToUserInfo(user)); } - catch (ConflictException) + catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User) { return BadRequest(ErrorResponse.UserController.UsernameConflict()); } diff --git a/Timeline/Filters/Timeline.cs b/Timeline/Filters/Timeline.cs index 76e8d751..90b87223 100644 --- a/Timeline/Filters/Timeline.cs +++ b/Timeline/Filters/Timeline.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Timeline.Models.Http; -using Timeline.Services; +using Timeline.Services.Exceptions; namespace Timeline.Filters { diff --git a/Timeline/Properties/launchSettings.json b/Timeline/Properties/launchSettings.json index daa4b011..4baafa62 100644 --- a/Timeline/Properties/launchSettings.json +++ b/Timeline/Properties/launchSettings.json @@ -1,19 +1,19 @@ -{ - "profiles": { - "Timeline": { - "commandName": "Project", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development", - "ASPNETCORE_FRONTENDPROXYONLY": "true", - "ASPNETCORE_WORKDIR": "D:\\timeline-development" - } - }, - "Timeline-Staging": { - "commandName": "Project", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Staging", - "ASPNETCORE_WORKDIR": "D:\\timeline-development" - } - } - } -} +{ + "profiles": { + "Timeline": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_FRONTENDPROXYONLY": "true", + "ASPNETCORE_WORKDIR": "D:\\timeline-development" + } + }, + "Timeline-Staging": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Staging", + "ASPNETCORE_WORKDIR": "D:\\timeline-development" + } + } + } +} diff --git a/Timeline/Resources/Services/Exception.Designer.cs b/Timeline/Resources/Services/Exception.Designer.cs index 0c721d92..889aa1a7 100644 --- a/Timeline/Resources/Services/Exception.Designer.cs +++ b/Timeline/Resources/Services/Exception.Designer.cs @@ -69,15 +69,6 @@ namespace Timeline.Resources.Services { } } - /// - /// Looks up a localized string similar to A present resource conflicts with the given resource.. - /// - internal static string ConflictException { - get { - return ResourceManager.GetString("ConflictException", resourceCulture); - } - } - /// /// Looks up a localized string similar to The hashes password is of bad format. It might not be created by server.. /// @@ -258,33 +249,6 @@ namespace Timeline.Resources.Services { } } - /// - /// Looks up a localized string similar to Timeline does not exist. If this is a personal timeline, it means the user does not exist and inner exception should be a UserNotExistException.. - /// - internal static string TimelineNotExistException { - get { - return ResourceManager.GetString("TimelineNotExistException", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The timeline post does not exist. You can't do operation on it.. - /// - internal static string TimelinePostNotExistException { - get { - return ResourceManager.GetString("TimelinePostNotExistException", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The user does not exist.. - /// - internal static string UserNotExistException { - get { - return ResourceManager.GetString("UserNotExistException", resourceCulture); - } - } - /// /// Looks up a localized string similar to The token is of bad format, which means it may not be created by the server.. /// diff --git a/Timeline/Resources/Services/Exception.resx b/Timeline/Resources/Services/Exception.resx index 660e5b3d..d455f0fc 100644 --- a/Timeline/Resources/Services/Exception.resx +++ b/Timeline/Resources/Services/Exception.resx @@ -120,9 +120,6 @@ The password is wrong. - - A present resource conflicts with the given resource. - The hashes password is of bad format. It might not be created by server. @@ -183,15 +180,6 @@ Password is of bad format. - - Timeline does not exist. If this is a personal timeline, it means the user does not exist and inner exception should be a UserNotExistException. - - - The timeline post does not exist. You can't do operation on it. - - - The user does not exist. - The token is of bad format, which means it may not be created by the server. diff --git a/Timeline/Resources/Services/Exceptions.Designer.cs b/Timeline/Resources/Services/Exceptions.Designer.cs new file mode 100644 index 00000000..84439716 --- /dev/null +++ b/Timeline/Resources/Services/Exceptions.Designer.cs @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// 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.Resources.Services { + 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 Exceptions { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Exceptions() { + } + + /// + /// 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.Resources.Services.Exceptions", typeof(Exceptions).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 A entity of type "{0}" already exists.. + /// + internal static string EntityAlreadyExistError { + get { + return ResourceManager.GetString("EntityAlreadyExistError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The entity already exists.. + /// + internal static string EntityAlreadyExistErrorDefault { + get { + return ResourceManager.GetString("EntityAlreadyExistErrorDefault", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The required entity of type "{0}" does not exist.. + /// + internal static string EntityNotExistError { + get { + return ResourceManager.GetString("EntityNotExistError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The required entity does not exist.. + /// + internal static string EntityNotExistErrorDefault { + get { + return ResourceManager.GetString("EntityNotExistErrorDefault", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Request timeline name is "{0}". If this is a personal timeline whose name starts with '@', it means the user does not exist and inner exception should be a UserNotExistException.. + /// + internal static string TimelineNotExistException { + get { + return ResourceManager.GetString("TimelineNotExistException", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Request timeline name is "{0}". Request timeline post id is "{1}".. + /// + internal static string TimelinePostNotExistException { + get { + return ResourceManager.GetString("TimelinePostNotExistException", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Request timeline name is "{0}". Request timeline post id is "{1}". The post does not exist because it has been deleted.. + /// + internal static string TimelinePostNotExistExceptionDeleted { + get { + return ResourceManager.GetString("TimelinePostNotExistExceptionDeleted", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Request username is "{0}". Request id is "{1}".. + /// + internal static string UserNotExistException { + get { + return ResourceManager.GetString("UserNotExistException", resourceCulture); + } + } + } +} diff --git a/Timeline/Resources/Services/Exceptions.resx b/Timeline/Resources/Services/Exceptions.resx new file mode 100644 index 00000000..b064fd44 --- /dev/null +++ b/Timeline/Resources/Services/Exceptions.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + The required entity of type "{0}" does not exist. + + + The entity already exists. + + + A entity of type "{0}" already exists. + + + The required entity does not exist. + + + Request timeline name is "{0}". If this is a personal timeline whose name starts with '@', it means the user does not exist and inner exception should be a UserNotExistException. + + + Request timeline name is "{0}". Request timeline post id is "{1}". The post does not exist because it has been deleted. + + + Request timeline name is "{0}". Request timeline post id is "{1}". + + + Request username is "{0}". Request id is "{1}". + + \ No newline at end of file diff --git a/Timeline/Services/ConflictException.cs b/Timeline/Services/ConflictException.cs deleted file mode 100644 index 6ede183a..00000000 --- a/Timeline/Services/ConflictException.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace Timeline.Services -{ - /// - /// Thrown when a resource already exists and conflicts with the given resource. - /// - /// - /// For example a username already exists and conflicts with the given username. - /// - [Serializable] - public class ConflictException : Exception - { - public ConflictException() : base(Resources.Services.Exception.ConflictException) { } - public ConflictException(string message) : base(message) { } - public ConflictException(string message, Exception inner) : base(message, inner) { } - protected ConflictException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } -} diff --git a/Timeline/Services/EntityNames.cs b/Timeline/Services/EntityNames.cs new file mode 100644 index 00000000..0ce1de3b --- /dev/null +++ b/Timeline/Services/EntityNames.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Timeline.Services +{ + public static class EntityNames + { + public const string User = "User"; + public const string Timeline = "Timeline"; + public const string TimelinePost = "TimelinePost"; + } +} diff --git a/Timeline/Services/Exceptions/EntityAlreadyExistError.cs b/Timeline/Services/Exceptions/EntityAlreadyExistError.cs new file mode 100644 index 00000000..7db2e860 --- /dev/null +++ b/Timeline/Services/Exceptions/EntityAlreadyExistError.cs @@ -0,0 +1,63 @@ +using System; +using System.Globalization; +using System.Text; + +namespace Timeline.Services.Exceptions +{ + /// + /// Thrown when an entity is already exists. + /// + /// + /// For example, want to create a timeline but a timeline with the same name already exists. + /// + [Serializable] + public class EntityAlreadyExistException : Exception + { + private readonly string? _entityName; + + public EntityAlreadyExistException() : this(null, null, null, null) { } + + public EntityAlreadyExistException(string? entityName) : this(entityName, null) { } + + public EntityAlreadyExistException(string? entityName, Exception? inner) : this(entityName, null, null, null, inner) { } + + public EntityAlreadyExistException(string? entityName, object? entity = null) : this(entityName, null, entity, null, null) { } + public EntityAlreadyExistException(Type? entityType, object? entity = null) : this(null, entityType, entity, null, null) { } + public EntityAlreadyExistException(string? entityName, Type? entityType, object? entity = null, string? message = null, Exception? inner = null) : base(MakeMessage(entityName, entityType, message), inner) + { + _entityName = entityName; + EntityType = entityType; + Entity = entity; + } + + private static string MakeMessage(string? entityName, Type? entityType, string? message) + { + string? name = entityName ?? (entityType?.Name); + + var result = new StringBuilder(); + + if (name == null) + result.Append(Resources.Services.Exceptions.EntityAlreadyExistErrorDefault); + else + result.AppendFormat(CultureInfo.InvariantCulture, Resources.Services.Exceptions.EntityAlreadyExistError, name); + + if (message != null) + { + result.Append(' '); + result.Append(message); + } + + return result.ToString(); + } + + protected EntityAlreadyExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public string? EntityName => _entityName ?? (EntityType?.Name); + + public Type? EntityType { get; } + + public object? Entity { get; } + } +} diff --git a/Timeline/Services/Exceptions/EntityNotExistError.cs b/Timeline/Services/Exceptions/EntityNotExistError.cs new file mode 100644 index 00000000..e79496d3 --- /dev/null +++ b/Timeline/Services/Exceptions/EntityNotExistError.cs @@ -0,0 +1,55 @@ +using System; +using System.Globalization; +using System.Text; + +namespace Timeline.Services.Exceptions +{ + /// + /// Thrown when you want to get an entity that does not exist. + /// + /// + /// For example, you want to get a timeline with given name but it does not exist. + /// + [Serializable] + public class EntityNotExistException : Exception + { + public EntityNotExistException() : this(null, null, null, null) { } + public EntityNotExistException(string? entityName) : this(entityName, null, null, null) { } + public EntityNotExistException(Type? entityType) : this(null, entityType, null, null) { } + public EntityNotExistException(string? entityName, Exception? inner) : this(entityName, null, null, inner) { } + public EntityNotExistException(Type? entityType, Exception? inner) : this(null, entityType, null, inner) { } + public EntityNotExistException(string? entityName, Type? entityType, string? message = null, Exception? inner = null) : base(MakeMessage(entityName, entityType, message), inner) + { + EntityName = entityName; + EntityType = entityType; + } + + private static string MakeMessage(string? entityName, Type? entityType, string? message) + { + string? name = entityName ?? (entityType?.Name); + + var result = new StringBuilder(); + + if (name == null) + result.Append(Resources.Services.Exceptions.EntityNotExistErrorDefault); + else + result.AppendFormat(CultureInfo.InvariantCulture, Resources.Services.Exceptions.EntityNotExistError, name); + + if (message != null) + { + result.Append(' '); + result.Append(message); + } + + return result.ToString(); + } + + protected EntityNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public string? EntityName { get; } + + public Type? EntityType { get; } + } +} diff --git a/Timeline/Services/Exceptions/ExceptionMessageHelper.cs b/Timeline/Services/Exceptions/ExceptionMessageHelper.cs new file mode 100644 index 00000000..be3c42a4 --- /dev/null +++ b/Timeline/Services/Exceptions/ExceptionMessageHelper.cs @@ -0,0 +1,13 @@ +namespace Timeline.Services.Exceptions +{ + public static class ExceptionMessageHelper + { + public static string AppendAdditionalMessage(this string origin, string? message) + { + if (message == null) + return origin; + else + return origin + " " + message; + } + } +} diff --git a/Timeline/Services/Exceptions/TimelineNotExistException.cs b/Timeline/Services/Exceptions/TimelineNotExistException.cs new file mode 100644 index 00000000..70970b24 --- /dev/null +++ b/Timeline/Services/Exceptions/TimelineNotExistException.cs @@ -0,0 +1,21 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + [Serializable] + public class TimelineNotExistException : EntityNotExistException + { + public TimelineNotExistException() : this(null, null) { } + public TimelineNotExistException(string? timelineName) : this(timelineName, null) { } + public TimelineNotExistException(string? timelineName, Exception? inner) : this(timelineName, null, inner) { } + public TimelineNotExistException(string? timelineName, string? message, Exception? inner = null) + : base(EntityNames.Timeline, null, string.Format(CultureInfo.InvariantCulture, Resources.Services.Exceptions.TimelineNotExistException, timelineName ?? "").AppendAdditionalMessage(message), inner) { TimelineName = timelineName; } + + protected TimelineNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public string? TimelineName { get; set; } + } +} diff --git a/Timeline/Services/Exceptions/TimelinePostNotExistException.cs b/Timeline/Services/Exceptions/TimelinePostNotExistException.cs new file mode 100644 index 00000000..bbb9e908 --- /dev/null +++ b/Timeline/Services/Exceptions/TimelinePostNotExistException.cs @@ -0,0 +1,33 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + [Serializable] + public class TimelinePostNotExistException : EntityNotExistException + { + public TimelinePostNotExistException() { } + [Obsolete("This has no meaning.")] + public TimelinePostNotExistException(string? message) : this(message, null) { } + [Obsolete("This has no meaning.")] + public TimelinePostNotExistException(string? message, Exception? inner) : this(null, null, false, message, inner) { } + protected TimelinePostNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public TimelinePostNotExistException(string? timelineName, long? id, bool isDelete, string? message = null, Exception? inner = null) : base(EntityNames.TimelinePost, null, MakeMessage(timelineName, id, isDelete).AppendAdditionalMessage(message), inner) { TimelineName = timelineName; Id = id; IsDelete = isDelete; } + + private static string MakeMessage(string? timelineName, long? id, bool isDelete) + { + return string.Format(CultureInfo.InvariantCulture, isDelete ? Resources.Services.Exceptions.TimelinePostNotExistExceptionDeleted : Resources.Services.Exceptions.TimelinePostNotExistException, timelineName ?? "", id); + } + + public string? TimelineName { get; set; } + public long? Id { get; set; } + + /// + /// True if the post is deleted. False if the post does not exist at all. + /// + public bool IsDelete { get; set; } + } +} diff --git a/Timeline/Services/Exceptions/UserNotExistException.cs b/Timeline/Services/Exceptions/UserNotExistException.cs new file mode 100644 index 00000000..7ef714df --- /dev/null +++ b/Timeline/Services/Exceptions/UserNotExistException.cs @@ -0,0 +1,40 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + /// + /// The user requested does not exist. + /// + [Serializable] + public class UserNotExistException : EntityNotExistException + { + public UserNotExistException() : this(null, null, null, null) { } + public UserNotExistException(string? username, Exception? inner) : this(username, null, null, inner) { } + + public UserNotExistException(string? username) : this(username, null, null, null) { } + + public UserNotExistException(long id) : this(null, id, null, null) { } + + public UserNotExistException(string? username, long? id, string? message, Exception? inner) : base(EntityNames.User, null, + string.Format(CultureInfo.InvariantCulture, Resources.Services.Exceptions.UserNotExistException, username ?? "", id).AppendAdditionalMessage(message), inner) + { + Username = username; + Id = id; + } + + protected UserNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + /// + /// The username of the user that does not exist. + /// + public string? Username { get; set; } + + /// + /// The id of the user that does not exist. + /// + public long? Id { get; set; } + } +} diff --git a/Timeline/Services/TimelineNotExistException.cs b/Timeline/Services/TimelineNotExistException.cs deleted file mode 100644 index 6dfd0bab..00000000 --- a/Timeline/Services/TimelineNotExistException.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace Timeline.Services -{ - [Serializable] - public class TimelineNotExistException : Exception - { - public TimelineNotExistException() : base(Resources.Services.Exception.TimelineNotExistException) { } - public TimelineNotExistException(string name) - : base(Resources.Services.Exception.TimelineNotExistException) { Name = name; } - public TimelineNotExistException(string name, Exception inner) - : base(Resources.Services.Exception.TimelineNotExistException, inner) { Name = name; } - protected TimelineNotExistException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - public string? Name { get; set; } - } -} diff --git a/Timeline/Services/TimelinePostNotExistException.cs b/Timeline/Services/TimelinePostNotExistException.cs deleted file mode 100644 index c542e63e..00000000 --- a/Timeline/Services/TimelinePostNotExistException.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -namespace Timeline.Services -{ - [Serializable] - public class TimelinePostNotExistException : Exception - { - public TimelinePostNotExistException() { } - public TimelinePostNotExistException(string message) : base(message) { } - public TimelinePostNotExistException(string message, Exception inner) : base(message, inner) { } - protected TimelinePostNotExistException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - public TimelinePostNotExistException(string timelineName, long id, bool isDelete = false) : base(Resources.Services.Exception.TimelinePostNotExistException) { TimelineName = timelineName; Id = id; IsDelete = isDelete; } - - public TimelinePostNotExistException(string timelineName, long id, bool isDelete, string message) : base(message) { TimelineName = timelineName; Id = id; IsDelete = isDelete; } - - public TimelinePostNotExistException(string timelineName, long id, bool isDelete, string message, Exception inner) : base(message, inner) { TimelineName = timelineName; Id = id; IsDelete = isDelete; } - - public string TimelineName { get; set; } = ""; - public long Id { get; set; } - /// - /// True if the post is deleted. False if the post does not exist at all. - /// - public bool IsDelete { get; set; } = false; - } -} diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index aecfeeec..a473ae66 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -10,6 +10,7 @@ using Timeline.Entities; using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Validation; +using Timeline.Services.Exceptions; using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services @@ -428,7 +429,7 @@ namespace Timeline.Services var postEntity = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync(); if (postEntity == null) - throw new TimelinePostNotExistException(name, postId); + throw new TimelinePostNotExistException(name, postId, false); if (postEntity.Content == null) throw new TimelinePostNotExistException(name, postId, true); @@ -450,7 +451,7 @@ namespace Timeline.Services var postEntity = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync(); if (postEntity == null) - throw new TimelinePostNotExistException(name, postId); + throw new TimelinePostNotExistException(name, postId, false); if (postEntity.Content == null) throw new TimelinePostNotExistException(name, postId, true); @@ -586,7 +587,7 @@ namespace Timeline.Services var post = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == id).SingleOrDefaultAsync(); if (post == null || post.Content == null) - throw new TimelinePostNotExistException(name, id); + throw new TimelinePostNotExistException(name, id, false); string? dataTag = null; @@ -977,7 +978,7 @@ namespace Timeline.Services var conflict = await _database.Timelines.AnyAsync(t => t.Name == name); if (conflict) - throw new ConflictException(ExceptionTimelineNameConflict); + throw new EntityAlreadyExistException(EntityNames.Timeline, null, ExceptionTimelineNameConflict); var newEntity = new TimelineEntity { diff --git a/Timeline/Services/UserNotExistException.cs b/Timeline/Services/UserNotExistException.cs deleted file mode 100644 index fd0b5ecf..00000000 --- a/Timeline/Services/UserNotExistException.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using Timeline.Helpers; - -namespace Timeline.Services -{ - /// - /// The user requested does not exist. - /// - [Serializable] - public class UserNotExistException : Exception - { - public UserNotExistException() : base(Resources.Services.Exception.UserNotExistException) { } - public UserNotExistException(string message, Exception inner) : base(message, inner) { } - - public UserNotExistException(string username) - : base(Log.Format(Resources.Services.Exception.UserNotExistException, ("Username", username))) - { - Username = username; - } - - public UserNotExistException(long id) - : base(Log.Format(Resources.Services.Exception.UserNotExistException, ("Id", id))) - { - Id = id; - } - - protected UserNotExistException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - /// - /// The username of the user that does not exist. - /// - public string Username { get; set; } = ""; - - /// - /// The id of the user that does not exist. - /// - public long Id { get; set; } - } -} diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index e0a5ab50..c20b180c 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -8,6 +8,7 @@ using Timeline.Entities; using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Validation; +using Timeline.Services.Exceptions; using static Timeline.Resources.Services.UserService; namespace Timeline.Services @@ -197,7 +198,7 @@ namespace Timeline.Services private static void ThrowUsernameConflict() { - throw new ConflictException(ExceptionUsernameConflict); + throw new EntityAlreadyExistException(EntityNames.User, ExceptionUsernameConflict); } private static User CreateUserFromEntity(UserEntity entity) diff --git a/Timeline/Services/UserTokenManager.cs b/Timeline/Services/UserTokenManager.cs index 6decf8f9..a016ff96 100644 --- a/Timeline/Services/UserTokenManager.cs +++ b/Timeline/Services/UserTokenManager.cs @@ -2,6 +2,7 @@ using System; using System.Threading.Tasks; using Timeline.Models; +using Timeline.Services.Exceptions; namespace Timeline.Services { diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj index 4f5c0f1f..55acd805 100644 --- a/Timeline/Timeline.csproj +++ b/Timeline/Timeline.csproj @@ -149,6 +149,11 @@ True Exception.resx + + True + True + Exceptions.resx + True True @@ -237,6 +242,10 @@ ResXFileCodeGenerator Exception.Designer.cs + + ResXFileCodeGenerator + Exceptions.Designer.cs + ResXFileCodeGenerator TimelineService.Designer.cs -- cgit v1.2.3