From c324d1dad0ffc1a1013b22792078415e7a50c470 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 24 Oct 2019 16:56:41 +0800 Subject: ... --- Timeline/Services/DatabaseExtensions.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'Timeline/Services/DatabaseExtensions.cs') diff --git a/Timeline/Services/DatabaseExtensions.cs b/Timeline/Services/DatabaseExtensions.cs index a37cf05b..62b22f00 100644 --- a/Timeline/Services/DatabaseExtensions.cs +++ b/Timeline/Services/DatabaseExtensions.cs @@ -4,22 +4,27 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; +using Timeline.Models.Validation; namespace Timeline.Services { - public static class DatabaseExtensions + internal 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 is null. + /// Thrown if is of bad format. /// Thrown if user does not exist. - public static async Task CheckAndGetUser(DbSet userDbSet, string username) + internal static async Task CheckAndGetUser(DbSet userDbSet, UsernameValidator validator, string username) { - if (string.IsNullOrEmpty(username)) - throw new ArgumentException("Username is null or empty.", nameof(username)); + if (username == null) + throw new ArgumentNullException(nameof(username)); + var (result, messageGenerator) = validator.Validate(username); + if (!result) + throw new UsernameBadFormatException(username, messageGenerator(null)); var userId = await userDbSet.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync(); if (userId == 0) -- cgit v1.2.3