diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-22 15:29:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 15:29:03 +0800 |
commit | 11f01c56b4ea1dbb09d04258bec89f800c6ee2b6 (patch) | |
tree | af83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline/Services | |
parent | fd95f9abc017575b13a31dd16ac72ef663e984d6 (diff) | |
parent | 96c18fb2e17c94ff04094608c705db087400f510 (diff) | |
download | timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.gz timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.bz2 timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.zip |
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline/Services')
-rw-r--r-- | Timeline/Services/DatabaseExtensions.cs | 30 | ||||
-rw-r--r-- | Timeline/Services/UserAvatarService.cs | 21 | ||||
-rw-r--r-- | Timeline/Services/UserDetailService.cs | 135 |
3 files changed, 168 insertions, 18 deletions
diff --git a/Timeline/Services/DatabaseExtensions.cs b/Timeline/Services/DatabaseExtensions.cs new file mode 100644 index 00000000..a37cf05b --- /dev/null +++ b/Timeline/Services/DatabaseExtensions.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+
+namespace Timeline.Services
+{
+ public static class DatabaseExtensions
+ {
+ /// <summary>
+ /// Check the existence and get the id of the user.
+ /// </summary>
+ /// <param name="username">The username of the user.</param>
+ /// <returns>The user id.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user does not exist.</exception>
+ public static async Task<long> CheckAndGetUser(DbSet<User> userDbSet, string username)
+ {
+ if (string.IsNullOrEmpty(username))
+ throw new ArgumentException("Username is null or empty.", nameof(username));
+
+ var userId = await userDbSet.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
+ if (userId == 0)
+ throw new UserNotExistException(username);
+ return userId;
+ }
+ }
+}
diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs index 7b1f405c..5c380dd8 100644 --- a/Timeline/Services/UserAvatarService.cs +++ b/Timeline/Services/UserAvatarService.cs @@ -219,12 +219,7 @@ namespace Timeline.Services public async Task<string> GetAvatarETag(string username)
{
- if (string.IsNullOrEmpty(username))
- throw new ArgumentException("Username is null or empty.", nameof(username));
-
- var userId = await _database.Users.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
- if (userId == 0)
- throw new UserNotExistException(username);
+ var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username);
var eTag = (await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.ETag }).SingleAsync()).ETag;
if (eTag == null)
@@ -235,12 +230,7 @@ namespace Timeline.Services public async Task<AvatarInfo> GetAvatar(string username)
{
- if (string.IsNullOrEmpty(username))
- throw new ArgumentException("Username is null or empty.", nameof(username));
-
- var userId = await _database.Users.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
- if (userId == 0)
- throw new UserNotExistException(username);
+ var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username);
var avatar = await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.Type, a.Data, a.LastModified }).SingleAsync();
@@ -272,9 +262,6 @@ namespace Timeline.Services public async Task SetAvatar(string username, Avatar avatar)
{
- if (string.IsNullOrEmpty(username))
- throw new ArgumentException("Username is null or empty.", nameof(username));
-
if (avatar != null)
{
if (string.IsNullOrEmpty(avatar.Type))
@@ -283,9 +270,7 @@ namespace Timeline.Services throw new ArgumentException("Data of avatar is null.", nameof(avatar));
}
- var userId = await _database.Users.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
- if (userId == 0)
- throw new UserNotExistException(username);
+ var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username);
var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleAsync();
diff --git a/Timeline/Services/UserDetailService.cs b/Timeline/Services/UserDetailService.cs new file mode 100644 index 00000000..a8ed662b --- /dev/null +++ b/Timeline/Services/UserDetailService.cs @@ -0,0 +1,135 @@ +using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Models;
+
+namespace Timeline.Services
+{
+ public interface IUserDetailService
+ {
+ /// <summary>
+ /// Get the nickname of user.
+ /// </summary>
+ /// <param name="username">The username to get nickname of.</param>
+ /// <returns>The user's nickname. Null if not set.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user doesn't exist.</exception>
+ Task<string> GetUserNickname(string username);
+
+ /// <summary>
+ /// Get the detail of user.
+ /// </summary>
+ /// <param name="username">The username to get user detail of.</param>
+ /// <returns>The user detail.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user doesn't exist.</exception>
+ Task<UserDetail> GetUserDetail(string username);
+
+ /// <summary>
+ /// Update the detail of user. This function does not do data check.
+ /// </summary>
+ /// <param name="username">The username to get user detail of.</param>
+ /// <param name="detail">The detail to update. Can't be null. Any null member means not set.</param>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty or <paramref name="detail"/> is null.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user doesn't exist.</exception>
+ Task UpdateUserDetail(string username, UserDetail detail);
+ }
+
+ public class UserDetailService : IUserDetailService
+ {
+ private readonly ILogger<UserDetailService> _logger;
+
+ private readonly DatabaseContext _databaseContext;
+
+ public UserDetailService(ILogger<UserDetailService> logger, DatabaseContext databaseContext)
+ {
+ _logger = logger;
+ _databaseContext = databaseContext;
+ }
+
+ private async Task<UserDetailEntity> CreateEntity(long userId)
+ {
+ var entity = new UserDetailEntity()
+ {
+ UserId = userId
+ };
+ _databaseContext.UserDetails.Add(entity);
+ await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation("An entity is created in user_details.");
+ return entity;
+ }
+
+ // Check the existence of user detail entry
+ private async Task<UserDetailEntity> CheckAndInit(long userId)
+ {
+ var detail = await _databaseContext.UserDetails.Where(e => e.UserId == userId).SingleOrDefaultAsync();
+ if (detail == null)
+ {
+ detail = await CreateEntity(userId);
+ }
+ return detail;
+ }
+
+ public async Task<string> GetUserNickname(string username)
+ {
+ var userId = await DatabaseExtensions.CheckAndGetUser(_databaseContext.Users, username);
+ var detail = await _databaseContext.UserDetails.Where(e => e.UserId == userId).Select(e => new { e.Nickname }).SingleOrDefaultAsync();
+ if (detail == null)
+ {
+ var entity = await CreateEntity(userId);
+ return null;
+ }
+ else
+ {
+ var nickname = detail.Nickname;
+ return string.IsNullOrEmpty(nickname) ? null : nickname;
+ }
+ }
+
+ public async Task<UserDetail> GetUserDetail(string username)
+ {
+ var userId = await DatabaseExtensions.CheckAndGetUser(_databaseContext.Users, username);
+ var detailEntity = await CheckAndInit(userId);
+ return UserDetail.From(detailEntity);
+ }
+
+ public async Task UpdateUserDetail(string username, UserDetail detail)
+ {
+ if (detail == null)
+ throw new ArgumentNullException(nameof(detail));
+
+ var userId = await DatabaseExtensions.CheckAndGetUser(_databaseContext.Users, username);
+ var detailEntity = await CheckAndInit(userId);
+
+ if (detail.Nickname != null)
+ detailEntity.Nickname = detail.Nickname;
+
+ if (detail.QQ != null)
+ detailEntity.QQ = detail.QQ;
+
+ if (detail.EMail != null)
+ detailEntity.EMail = detail.EMail;
+
+ if (detail.PhoneNumber != null)
+ detailEntity.PhoneNumber = detail.PhoneNumber;
+
+ if (detail.Description != null)
+ detailEntity.Description = detail.Description;
+
+ await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation("An entity is updated in user_details.");
+ }
+ }
+
+ public static class UserDetailServiceCollectionExtensions
+ {
+ public static void AddUserDetailService(this IServiceCollection services)
+ {
+ services.AddScoped<IUserDetailService, UserDetailService>();
+ }
+ }
+}
|