From 5f9f9a9e40306f83bf360c3d27e4e33e78565fce Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 7 Nov 2019 22:06:06 +0800 Subject: Complete PersonalTimelineController and write attribute test. --- .../TimelineMemberOperationUserException.cs | 15 ++++-- Timeline/Services/TimelinePostNotExistException.cs | 23 ++++++++ Timeline/Services/TimelineService.cs | 62 ++++++++++++++++++---- 3 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 Timeline/Services/TimelinePostNotExistException.cs (limited to 'Timeline/Services') diff --git a/Timeline/Services/TimelineMemberOperationUserException.cs b/Timeline/Services/TimelineMemberOperationUserException.cs index 998f1a6e..543ee160 100644 --- a/Timeline/Services/TimelineMemberOperationUserException.cs +++ b/Timeline/Services/TimelineMemberOperationUserException.cs @@ -6,6 +6,12 @@ namespace Timeline.Services [Serializable] public class TimelineMemberOperationUserException : Exception { + public enum MemberOperation + { + Add, + Remove + } + public TimelineMemberOperationUserException() : base(Resources.Services.Exception.TimelineMemberOperationException) { } public TimelineMemberOperationUserException(string message) : base(message) { } public TimelineMemberOperationUserException(string message, Exception inner) : base(message, inner) { } @@ -13,10 +19,13 @@ namespace Timeline.Services System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - public TimelineMemberOperationUserException(int index, string username, Exception inner) : base(MakeIndexMessage(index), inner) { Index = index; Username = username; } + public TimelineMemberOperationUserException(int index, MemberOperation operation, string username, Exception inner) + : base(MakeMessage(operation, index), inner) { Operation = operation; Index = index; Username = username; } + + private static string MakeMessage(MemberOperation operation, int index) => string.Format(CultureInfo.CurrentCulture, + Resources.Services.Exception.TimelineMemberOperationExceptionDetail, operation, index); - private static string MakeIndexMessage(int index) => string.Format(CultureInfo.CurrentCulture, - Resources.Services.Exception.TimelineMemberOperationExceptionIndex, index); + public MemberOperation? Operation { get; set; } /// /// The index of the member on which the operation failed. diff --git a/Timeline/Services/TimelinePostNotExistException.cs b/Timeline/Services/TimelinePostNotExistException.cs new file mode 100644 index 00000000..97e5d550 --- /dev/null +++ b/Timeline/Services/TimelinePostNotExistException.cs @@ -0,0 +1,23 @@ +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(long id) : base(Resources.Services.Exception.TimelinePostNotExistException) { Id = id; } + + public TimelinePostNotExistException(long id, string message) : base(message) { Id = id; } + + public TimelinePostNotExistException(long id, string message, Exception inner) : base(message, inner) { Id = id; } + + public long Id { get; set; } + } +} diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 7fe32cac..28b1f91d 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Models; +using Timeline.Models.Http; namespace Timeline.Services { @@ -45,7 +46,7 @@ namespace Timeline.Services /// The author's username. /// The content. /// The time of the post. If null, then use current time. - /// + /// The info of the created post. /// Thrown when or or is null. /// /// Thrown when timeline name is of bad format. @@ -61,14 +62,14 @@ namespace Timeline.Services /// /// Thrown if is of bad format. /// Thrown if does not exist. - Task CreatePost(string name, string author, string content, DateTime? time); + Task CreatePost(string name, string author, string content, DateTime? time); /// - /// Set the visibility permission of a timeline. + /// Delete a post /// /// Username or the timeline name. See remarks of . - /// The new visibility. - /// Thrown when is null. + /// The id of the post to delete. + /// Thrown when or is null. /// /// Thrown when timeline name is of bad format. /// For normal timeline, it means name is an empty string. @@ -81,14 +82,21 @@ namespace Timeline.Services /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - Task SetVisibility(string name, TimelineVisibility visibility); + /// + /// Thrown when the post with given id does not exist or is deleted already. + /// + /// + /// First use + /// to check the permission. + /// + Task DeletePost(string name, long id); /// - /// Set the description of a timeline. + /// Set the properties of a timeline. /// /// Username or the timeline name. See remarks of . - /// The new description. - /// Thrown when or is null. + /// The new properties. Null member means not to change. + /// Thrown when or is null. /// /// Thrown when timeline name is of bad format. /// For normal timeline, it means name is an empty string. @@ -101,7 +109,7 @@ namespace Timeline.Services /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - Task SetDescription(string name, string description); + Task ChangeProperty(string name, TimelinePropertyChangeRequest newProperties); /// /// Remove members to a timeline. @@ -159,6 +167,40 @@ namespace Timeline.Services /// True if can read, false if can't read. Task HasReadPermission(string name, string? username); + /// + /// Verify whether a user has the permission to modify a post. + /// + /// Username or the timeline name. See remarks of . + /// The user to check on. + /// True if can modify, false if can't modify. + /// Thrown when or is null. + /// + /// Thrown when timeline name is of bad format. + /// For normal timeline, it means name is an empty string. + /// For personal timeline, it means the username is of bad format, + /// the inner exception should be a . + /// + /// + /// Thrown when timeline does not exist. + /// For normal timeline, it means the name does not exist. + /// For personal timeline, it means the user of that username does not exist + /// and the inner exception should be a . + /// + /// + /// Thrown when the post with given id does not exist or is deleted already. + /// + /// + /// Thrown when is of bad format. + /// + /// + /// Thrown when does not exist. + /// + /// + /// This method does not check whether the user is administrator. + /// It only checks whether he is the author of the post or the owner of the timeline. + /// + Task HasPostModifyPermission(string name, long id, string username); + /// /// Verify whether a user is member of a timeline. /// -- cgit v1.2.3