aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/DatabaseExtensions.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-24 16:56:41 +0800
committer杨宇千 <crupest@outlook.com>2019-10-24 16:56:41 +0800
commitc324d1dad0ffc1a1013b22792078415e7a50c470 (patch)
treec8f5032f86d8a9e5df0117d438ea741cb0a4f613 /Timeline/Services/DatabaseExtensions.cs
parent9a163719b76958374d1c27616393368e54e8b8a5 (diff)
downloadtimeline-c324d1dad0ffc1a1013b22792078415e7a50c470.tar.gz
timeline-c324d1dad0ffc1a1013b22792078415e7a50c470.tar.bz2
timeline-c324d1dad0ffc1a1013b22792078415e7a50c470.zip
...
Diffstat (limited to 'Timeline/Services/DatabaseExtensions.cs')
-rw-r--r--Timeline/Services/DatabaseExtensions.cs15
1 files changed, 10 insertions, 5 deletions
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
{
/// <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="ArgumentNullException">Thrown if <paramref name="username"/> is null.</exception>
+ /// <exception cref="UsernameBadFormatException">Thrown if <paramref name="username"/> is of bad format.</exception>
/// <exception cref="UserNotExistException">Thrown if user does not exist.</exception>
- public static async Task<long> CheckAndGetUser(DbSet<User> userDbSet, string username)
+ internal static async Task<long> CheckAndGetUser(DbSet<User> 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)