diff options
author | crupest <crupest@outlook.com> | 2021-04-30 16:52:55 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-30 16:52:55 +0800 |
commit | 88002173a1155883d1fb46683a9a7ad1f521eb56 (patch) | |
tree | 742cce92b6e424d7bcd4a51a8da63dd10e18c4c7 /BackEnd/Timeline/Services/User/BasicUserService.cs | |
parent | bf4c48980f81e566065c07f3fe534ce7551ebdcc (diff) | |
download | timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.tar.gz timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.tar.bz2 timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Services/User/BasicUserService.cs')
-rw-r--r-- | BackEnd/Timeline/Services/User/BasicUserService.cs | 28 |
1 files changed, 15 insertions, 13 deletions
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<string, object> { ["username"] = username });
+ }
+
+ protected static EntityNotExistException CreateUserNotExistException(long id)
+ {
+ return new EntityNotExistException(EntityTypes.User,
+ new Dictionary<string, object> { ["id"] = id });
+ }
+
public async Task<bool> 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);
- }
- }
- }
}
|