From 3f4e88757f961532b84df85e86d21995655a29d4 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 27 Nov 2020 00:07:09 +0800 Subject: refactor: ... --- .../Timeline/Services/HighlightTimelineService.cs | 7 +-- BackEnd/Timeline/Services/TimelinePostService.cs | 24 +++++----- BackEnd/Timeline/Services/TimelineService.cs | 54 ++++++++++------------ BackEnd/Timeline/Services/UserCredentialService.cs | 2 - BackEnd/Timeline/Services/UserService.cs | 45 +++++++++--------- BackEnd/Timeline/Services/UserTokenManager.cs | 6 +-- 6 files changed, 65 insertions(+), 73 deletions(-) (limited to 'BackEnd/Timeline/Services') diff --git a/BackEnd/Timeline/Services/HighlightTimelineService.cs b/BackEnd/Timeline/Services/HighlightTimelineService.cs index 88ad4a4b..619bc33e 100644 --- a/BackEnd/Timeline/Services/HighlightTimelineService.cs +++ b/BackEnd/Timeline/Services/HighlightTimelineService.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; +using Timeline.Models; using Timeline.Services.Exceptions; namespace Timeline.Services @@ -14,7 +15,7 @@ namespace Timeline.Services /// Get all highlight timelines. /// /// A list of all highlight timelines. - Task> GetHighlightTimelines(); + Task> GetHighlightTimelines(); /// /// Add a timeline to highlight list. @@ -73,11 +74,11 @@ namespace Timeline.Services await _database.SaveChangesAsync(); } - public async Task> GetHighlightTimelines() + public async Task> GetHighlightTimelines() { var entities = await _database.HighlightTimelines.Select(t => new { t.Id }).ToListAsync(); - var result = new List(); + var result = new List(); foreach (var entity in entities) { diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index a1176a68..35513a36 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -39,7 +39,7 @@ namespace Timeline.Services /// Thrown when timeline with name does not exist. /// If it is a personal timeline, then inner exception is . /// - Task> GetPosts(string timelineName, DateTime? modifiedSince = null, bool includeDeleted = false); + Task> GetPosts(string timelineName, DateTime? modifiedSince = null, bool includeDeleted = false); /// /// Get the etag of data of a post. @@ -90,7 +90,7 @@ namespace Timeline.Services /// If it is a personal timeline, then inner exception is . /// /// Thrown if user of does not exist. - Task CreateTextPost(string timelineName, long authorId, string text, DateTime? time); + Task CreateTextPost(string timelineName, long authorId, string text, DateTime? time); /// /// Create a new image post in timeline. @@ -108,7 +108,7 @@ namespace Timeline.Services /// /// Thrown if user of does not exist. /// Thrown if data is not a image. Validated by . - Task CreateImagePost(string timelineName, long authorId, byte[] imageData, DateTime? time); + Task CreateImagePost(string timelineName, long authorId, byte[] imageData, DateTime? time); /// /// Delete a post. @@ -179,9 +179,9 @@ namespace Timeline.Services _clock = clock; } - private async Task MapTimelinePostFromEntity(TimelinePostEntity entity, string timelineName) + private async Task MapTimelinePostFromEntity(TimelinePostEntity entity, string timelineName) { - User? author = entity.AuthorId.HasValue ? await _userService.GetUser(entity.AuthorId.Value) : null; + UserInfo? author = entity.AuthorId.HasValue ? await _userService.GetUser(entity.AuthorId.Value) : null; ITimelinePostContent? content = null; @@ -197,7 +197,7 @@ namespace Timeline.Services }; } - return new TimelinePost( + return new TimelinePostInfo( id: entity.LocalId, author: author, content: content, @@ -207,7 +207,7 @@ namespace Timeline.Services ); } - public async Task> GetPosts(string timelineName, DateTime? modifiedSince = null, bool includeDeleted = false) + public async Task> GetPosts(string timelineName, DateTime? modifiedSince = null, bool includeDeleted = false) { modifiedSince = modifiedSince?.MyToUtc(); @@ -231,7 +231,7 @@ namespace Timeline.Services var postEntities = await query.ToListAsync(); - var posts = new List(); + var posts = new List(); foreach (var entity in postEntities) { posts.Add(await MapTimelinePostFromEntity(entity, timelineName)); @@ -309,7 +309,7 @@ namespace Timeline.Services }; } - public async Task CreateTextPost(string timelineName, long authorId, string text, DateTime? time) + public async Task CreateTextPost(string timelineName, long authorId, string text, DateTime? time) { time = time?.MyToUtc(); @@ -342,7 +342,7 @@ namespace Timeline.Services await _database.SaveChangesAsync(); - return new TimelinePost( + return new TimelinePostInfo( id: postEntity.LocalId, content: new TextTimelinePostContent(text), time: finalTime, @@ -352,7 +352,7 @@ namespace Timeline.Services ); } - public async Task CreateImagePost(string timelineName, long authorId, byte[] data, DateTime? time) + public async Task CreateImagePost(string timelineName, long authorId, byte[] data, DateTime? time) { time = time?.MyToUtc(); @@ -391,7 +391,7 @@ namespace Timeline.Services _database.TimelinePosts.Add(postEntity); await _database.SaveChangesAsync(); - return new TimelinePost( + return new TimelinePostInfo( id: postEntity.LocalId, content: new ImageTimelinePostContent(tag), time: finalTime, diff --git a/BackEnd/Timeline/Services/TimelineService.cs b/BackEnd/Timeline/Services/TimelineService.cs index b8ec354a..b65b3cf4 100644 --- a/BackEnd/Timeline/Services/TimelineService.cs +++ b/BackEnd/Timeline/Services/TimelineService.cs @@ -90,7 +90,7 @@ namespace Timeline.Services /// Thrown when timeline with name does not exist. /// If it is a personal timeline, then inner exception is . /// - Task GetTimeline(string timelineName); + Task GetTimeline(string timelineName); /// /// Get timeline by id. @@ -98,7 +98,7 @@ namespace Timeline.Services /// Id of timeline. /// The timeline. /// Thrown when timeline with given id does not exist. - Task GetTimelineById(long id); + Task GetTimelineById(long id); /// /// Set the properties of a timeline. @@ -113,8 +113,6 @@ namespace Timeline.Services /// Task ChangeProperty(string timelineName, TimelineChangePropertyRequest newProperties); - - /// /// Change member of timeline. /// @@ -174,7 +172,6 @@ namespace Timeline.Services /// Task HasReadPermission(string timelineName, long? visitorId); - /// /// Verify whether a user is member of a timeline. /// @@ -202,7 +199,7 @@ namespace Timeline.Services /// /// If user with related user id does not exist, empty list will be returned. /// - Task> GetTimelines(TimelineUserRelationship? relate = null, List? visibility = null); + Task> GetTimelines(TimelineUserRelationship? relate = null, List? visibility = null); /// /// Create a timeline. @@ -214,7 +211,7 @@ namespace Timeline.Services /// Thrown when timeline name is invalid. /// Thrown when the timeline already exists. /// Thrown when the owner user does not exist. - Task CreateTimeline(string timelineName, long ownerId); + Task CreateTimeline(string timelineName, long ownerId); /// /// Delete a timeline. @@ -238,7 +235,7 @@ namespace Timeline.Services /// /// You can only change name of general timeline. /// - Task ChangeTimelineName(string oldTimelineName, string newTimelineName); + Task ChangeTimelineName(string oldTimelineName, string newTimelineName); } public class TimelineService : BasicTimelineService, ITimelineService @@ -270,11 +267,11 @@ namespace Timeline.Services } /// Remember to include Members when query. - private async Task MapTimelineFromEntity(TimelineEntity entity) + private async Task MapTimelineFromEntity(TimelineEntity entity) { var owner = await _userService.GetUser(entity.OwnerId); - var members = new List(); + var members = new List(); foreach (var memberEntity in entity.Members) { members.Add(await _userService.GetUser(memberEntity.UserId)); @@ -282,19 +279,18 @@ namespace Timeline.Services var name = entity.Name ?? ("@" + owner.Username); - return new Models.Timeline - { - UniqueID = entity.UniqueId, - Name = name, - NameLastModified = entity.NameLastModified, - Title = string.IsNullOrEmpty(entity.Title) ? name : entity.Title, - Description = entity.Description ?? "", - Owner = owner, - Visibility = entity.Visibility, - Members = members, - CreateTime = entity.CreateTime, - LastModified = entity.LastModified - }; + return new TimelineInfo( + entity.UniqueId, + name, + entity.NameLastModified, + string.IsNullOrEmpty(entity.Title) ? name : entity.Title, + entity.Description ?? "", + owner, + entity.Visibility, + members, + entity.CreateTime, + entity.LastModified + ); } public async Task GetTimelineLastModifiedTime(string timelineName) @@ -321,7 +317,7 @@ namespace Timeline.Services return timelineEntity.UniqueId; } - public async Task GetTimeline(string timelineName) + public async Task GetTimeline(string timelineName) { if (timelineName == null) throw new ArgumentNullException(nameof(timelineName)); @@ -333,7 +329,7 @@ namespace Timeline.Services return await MapTimelineFromEntity(timelineEntity); } - public async Task GetTimelineById(long id) + public async Task GetTimelineById(long id) { var timelineEntity = await _database.Timelines.Where(t => t.Id == id).Include(t => t.Members).SingleOrDefaultAsync(); @@ -522,7 +518,7 @@ namespace Timeline.Services return await _database.TimelineMembers.AnyAsync(m => m.TimelineId == timelineId && m.UserId == userId); } - public async Task> GetTimelines(TimelineUserRelationship? relate = null, List? visibility = null) + public async Task> GetTimelines(TimelineUserRelationship? relate = null, List? visibility = null) { List entities; @@ -556,7 +552,7 @@ namespace Timeline.Services } } - var result = new List(); + var result = new List(); foreach (var entity in entities) { @@ -566,7 +562,7 @@ namespace Timeline.Services return result; } - public async Task CreateTimeline(string name, long owner) + public async Task CreateTimeline(string name, long owner) { if (name == null) throw new ArgumentNullException(nameof(name)); @@ -604,7 +600,7 @@ namespace Timeline.Services await _database.SaveChangesAsync(); } - public async Task ChangeTimelineName(string oldTimelineName, string newTimelineName) + public async Task ChangeTimelineName(string oldTimelineName, string newTimelineName) { if (oldTimelineName == null) throw new ArgumentNullException(nameof(oldTimelineName)); diff --git a/BackEnd/Timeline/Services/UserCredentialService.cs b/BackEnd/Timeline/Services/UserCredentialService.cs index e5c3581b..8aeef9ef 100644 --- a/BackEnd/Timeline/Services/UserCredentialService.cs +++ b/BackEnd/Timeline/Services/UserCredentialService.cs @@ -1,12 +1,10 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Helpers; -using Timeline.Models; using Timeline.Models.Validation; using Timeline.Services.Exceptions; diff --git a/BackEnd/Timeline/Services/UserService.cs b/BackEnd/Timeline/Services/UserService.cs index 9395cc52..96068e44 100644 --- a/BackEnd/Timeline/Services/UserService.cs +++ b/BackEnd/Timeline/Services/UserService.cs @@ -32,13 +32,13 @@ namespace Timeline.Services /// The id of the user. /// The user info. /// Thrown when the user with given id does not exist. - Task GetUser(long id); + Task GetUser(long id); /// /// List all users. /// /// The user info of users. - Task> GetUsers(); + Task> GetUsers(); /// /// Create a user with given info. @@ -49,7 +49,7 @@ namespace Timeline.Services /// Thrown when or is null. /// Thrown when or is of bad format. /// Thrown when a user with given username already exists. - Task CreateUser(string username, string password); + Task CreateUser(string username, string password); /// /// Modify a user. @@ -62,7 +62,7 @@ namespace Timeline.Services /// /// Version will increase if password is changed. /// - Task ModifyUser(long id, ModifyUserParams? param); + Task ModifyUser(long id, ModifyUserParams? param); } public class UserService : BasicUserService, IUserService @@ -116,26 +116,23 @@ namespace Timeline.Services throw new EntityAlreadyExistException(EntityNames.User, ExceptionUsernameConflict); } - private async Task CreateUserFromEntity(UserEntity entity) + private async Task CreateUserFromEntity(UserEntity entity) { var permission = await _userPermissionService.GetPermissionsOfUserAsync(entity.Id); - return new User - { - UniqueId = entity.UniqueId, - Username = entity.Username, - Permissions = permission, - Nickname = string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname, - Id = entity.Id, - Version = entity.Version, - CreateTime = entity.CreateTime, - UsernameChangeTime = entity.UsernameChangeTime, - LastModified = entity.LastModified - }; + return new UserInfo( + entity.Id, + entity.UniqueId, + entity.Username, + string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname, + permission, + entity.UsernameChangeTime, + entity.CreateTime, + entity.LastModified, + entity.Version + ); } - - - public async Task GetUser(long id) + public async Task GetUser(long id) { var user = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); @@ -145,9 +142,9 @@ namespace Timeline.Services return await CreateUserFromEntity(user); } - public async Task> GetUsers() + public async Task> GetUsers() { - List result = new(); + List result = new(); foreach (var entity in await _databaseContext.Users.ToArrayAsync()) { result.Add(await CreateUserFromEntity(entity)); @@ -155,7 +152,7 @@ namespace Timeline.Services return result; } - public async Task CreateUser(string username, string password) + public async Task CreateUser(string username, string password) { if (username == null) throw new ArgumentNullException(nameof(username)); @@ -183,7 +180,7 @@ namespace Timeline.Services return await CreateUserFromEntity(newEntity); } - public async Task ModifyUser(long id, ModifyUserParams? param) + public async Task ModifyUser(long id, ModifyUserParams? param) { if (param != null) { diff --git a/BackEnd/Timeline/Services/UserTokenManager.cs b/BackEnd/Timeline/Services/UserTokenManager.cs index 831329e6..b887b987 100644 --- a/BackEnd/Timeline/Services/UserTokenManager.cs +++ b/BackEnd/Timeline/Services/UserTokenManager.cs @@ -10,7 +10,7 @@ namespace Timeline.Services public class UserTokenCreateResult { public string Token { get; set; } = default!; - public User User { get; set; } = default!; + public UserInfo User { get; set; } = default!; } public interface IUserTokenManager @@ -38,7 +38,7 @@ namespace Timeline.Services /// Thrown when the token is of bad version. /// Thrown when the token is of bad format. /// Thrown when the user specified by the token does not exist. Usually the user had been deleted after the token was issued. - public Task VerifyToken(string token); + public Task VerifyToken(string token); } public class UserTokenManager : IUserTokenManager @@ -75,7 +75,7 @@ namespace Timeline.Services } - public async Task VerifyToken(string token) + public async Task VerifyToken(string token) { if (token == null) throw new ArgumentNullException(nameof(token)); -- cgit v1.2.3