diff options
Diffstat (limited to 'Timeline/Services')
-rw-r--r-- | Timeline/Services/ConflictException.cs | 21 | ||||
-rw-r--r-- | Timeline/Services/EntityNames.cs | 14 | ||||
-rw-r--r-- | Timeline/Services/Exceptions/EntityAlreadyExistError.cs | 63 | ||||
-rw-r--r-- | Timeline/Services/Exceptions/EntityNotExistError.cs | 55 | ||||
-rw-r--r-- | Timeline/Services/Exceptions/ExceptionMessageHelper.cs | 13 | ||||
-rw-r--r-- | Timeline/Services/Exceptions/TimelineNotExistException.cs | 21 | ||||
-rw-r--r-- | Timeline/Services/Exceptions/TimelinePostNotExistException.cs | 33 | ||||
-rw-r--r-- | Timeline/Services/Exceptions/UserNotExistException.cs | 40 | ||||
-rw-r--r-- | Timeline/Services/TimelineNotExistException.cs | 19 | ||||
-rw-r--r-- | Timeline/Services/TimelinePostNotExistException.cs | 28 | ||||
-rw-r--r-- | Timeline/Services/TimelineService.cs | 9 | ||||
-rw-r--r-- | Timeline/Services/UserNotExistException.cs | 41 | ||||
-rw-r--r-- | Timeline/Services/UserService.cs | 3 | ||||
-rw-r--r-- | Timeline/Services/UserTokenManager.cs | 1 |
14 files changed, 247 insertions, 114 deletions
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
-{
- /// <summary>
- /// Thrown when a resource already exists and conflicts with the given resource.
- /// </summary>
- /// <remarks>
- /// For example a username already exists and conflicts with the given username.
- /// </remarks>
- [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
+{
+ /// <summary>
+ /// Thrown when an entity is already exists.
+ /// </summary>
+ /// <remarks>
+ /// For example, want to create a timeline but a timeline with the same name already exists.
+ /// </remarks>
+ [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
+{
+ /// <summary>
+ /// Thrown when you want to get an entity that does not exist.
+ /// </summary>
+ /// <example>
+ /// For example, you want to get a timeline with given name but it does not exist.
+ /// </example>
+ [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; }
+
+ /// <summary>
+ /// True if the post is deleted. False if the post does not exist at all.
+ /// </summary>
+ 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
+{
+ /// <summary>
+ /// The user requested does not exist.
+ /// </summary>
+ [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) { }
+
+ /// <summary>
+ /// The username of the user that does not exist.
+ /// </summary>
+ public string? Username { get; set; }
+
+ /// <summary>
+ /// The id of the user that does not exist.
+ /// </summary>
+ 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; }
- /// <summary>
- /// True if the post is deleted. False if the post does not exist at all.
- /// </summary>
- 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
-{
- /// <summary>
- /// The user requested does not exist.
- /// </summary>
- [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) { }
-
- /// <summary>
- /// The username of the user that does not exist.
- /// </summary>
- public string Username { get; set; } = "";
-
- /// <summary>
- /// The id of the user that does not exist.
- /// </summary>
- 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
{
|