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 | 3716d2431de08194d3a107ef640febc47c3ee72a (patch) | |
tree | af83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline/Services/DatabaseExtensions.cs | |
parent | a585c6e35829e9f2b4b0b8ce8c6b395e5ea84f2c (diff) | |
parent | 968f9688dd3ff7cae6f66af0e69bb03392311c88 (diff) | |
download | timeline-3716d2431de08194d3a107ef640febc47c3ee72a.tar.gz timeline-3716d2431de08194d3a107ef640febc47c3ee72a.tar.bz2 timeline-3716d2431de08194d3a107ef640febc47c3ee72a.zip |
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline/Services/DatabaseExtensions.cs')
-rw-r--r-- | Timeline/Services/DatabaseExtensions.cs | 30 |
1 files changed, 30 insertions, 0 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;
+ }
+ }
+}
|