From 62a3557ab62e1fa188e7498643d7cf0221a18322 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Wed, 21 Aug 2019 18:33:07 +0800 Subject: Add database entity and service. --- Timeline/Services/DatabaseExtensions.cs | 30 +++++++++++ Timeline/Services/UserAvatarService.cs | 21 ++------ Timeline/Services/UserDetailService.cs | 90 +++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 Timeline/Services/DatabaseExtensions.cs create mode 100644 Timeline/Services/UserDetailService.cs (limited to 'Timeline/Services') 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 + { + /// + /// Check the existence and get the id of the user. + /// + /// The username of the user. + /// The user id. + /// Thrown if is null or empty. + /// Thrown if user does not exist. + public static async Task CheckAndGetUser(DbSet 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 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 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..c3a2a1af --- /dev/null +++ b/Timeline/Services/UserDetailService.cs @@ -0,0 +1,90 @@ +using Microsoft.EntityFrameworkCore; +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 + { + /// + /// Get the detail of user. + /// + /// The username to get user detail of. + /// The user detail. + /// Thrown if is null or empty. + /// Thrown if user doesn't exist. + Task GetUserDetail(string username); + + /// + /// Update the detail of user. This function does not do data check. + /// + /// The username to get user detail of. + /// The detail to update. Can't be null. Any null member means not set. + /// Thrown if is null or empty or is null. + /// Thrown if user doesn't exist. + Task UpdateUserDetail(string username, UserDetail detail); + } + + public class UserDetailService : IUserDetailService + { + private readonly ILogger _logger; + + private readonly DatabaseContext _databaseContext; + + public UserDetailService(ILogger logger, DatabaseContext databaseContext) + { + _logger = logger; + _databaseContext = databaseContext; + } + + // Check the existence of user detail entry + private async Task CheckAndInit(long userId) + { + var detail = await _databaseContext.UserDetails.Where(e => e.UserId == userId).SingleOrDefaultAsync(); + if (detail == null) + { + detail = new UserDetailEntity() + { + UserId = userId + }; + _databaseContext.UserDetails.Add(detail); + await _databaseContext.SaveChangesAsync(); + } + return detail; + } + + public async Task 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.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(); + } + } +} -- cgit v1.2.3