From 88002173a1155883d1fb46683a9a7ad1f521eb56 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 16:52:55 +0800 Subject: refactor: ... --- BackEnd/Timeline/Services/User/BasicUserService.cs | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'BackEnd/Timeline/Services/User/BasicUserService.cs') diff --git a/BackEnd/Timeline/Services/User/BasicUserService.cs b/BackEnd/Timeline/Services/User/BasicUserService.cs index 1f1b25f5..0ee8dabd 100644 --- a/BackEnd/Timeline/Services/User/BasicUserService.cs +++ b/BackEnd/Timeline/Services/User/BasicUserService.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; @@ -18,6 +19,18 @@ namespace Timeline.Services.User _database = database; } + protected static EntityNotExistException CreateUserNotExistException(string username) + { + return new EntityNotExistException(EntityTypes.User, + new Dictionary { ["username"] = username }); + } + + protected static EntityNotExistException CreateUserNotExistException(long id) + { + return new EntityNotExistException(EntityTypes.User, + new Dictionary { ["id"] = id }); + } + public async Task CheckUserExistenceAsync(long id) { return await _database.Users.AnyAsync(u => u.Id == id); @@ -34,7 +47,7 @@ namespace Timeline.Services.User var entity = await _database.Users.Where(user => user.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); if (entity == null) - throw new UserNotExistException(username); + throw CreateUserNotExistException(username); return entity.Id; } @@ -44,20 +57,9 @@ namespace Timeline.Services.User var entity = await _database.Users.Where(u => u.Id == userId).Select(u => new { u.UsernameChangeTime }).SingleOrDefaultAsync(); if (entity is null) - throw new UserNotExistException(userId); + throw CreateUserNotExistException(userId); return entity.UsernameChangeTime; } } - - public static class BasicUserServiceExtensions - { - public static async Task ThrowIfUserNotExist(this IBasicUserService service, long userId) - { - if (!await service.CheckUserExistenceAsync(userId)) - { - throw new UserNotExistException(userId); - } - } - } } -- cgit v1.2.3