From b3296b38176e26e410306bb19ef43da1523811b8 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 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Timeline/Services/DatabaseExtensions.cs (limited to 'Timeline/Services/DatabaseExtensions.cs') 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; + } + } +} -- cgit v1.2.3