diff options
author | crupest <crupest@outlook.com> | 2020-12-17 23:49:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 23:49:16 +0800 |
commit | ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754 (patch) | |
tree | f27b279237a6a7ab3b6a09206f3fec6a5524674b /BackEnd/Timeline/Services/BasicUserService.cs | |
parent | b0ee9afddd4f7ecf8a183ab8d8e9e575324a2b68 (diff) | |
parent | d452781c81e6d19076cf9d356877e154b2e34e91 (diff) | |
download | timeline-ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754.tar.gz timeline-ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754.tar.bz2 timeline-ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754.zip |
Merge pull request #192 from crupest/highlight-timeline
Highlight timeline.
Diffstat (limited to 'BackEnd/Timeline/Services/BasicUserService.cs')
-rw-r--r-- | BackEnd/Timeline/Services/BasicUserService.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/BasicUserService.cs b/BackEnd/Timeline/Services/BasicUserService.cs new file mode 100644 index 00000000..fbbb6677 --- /dev/null +++ b/BackEnd/Timeline/Services/BasicUserService.cs @@ -0,0 +1,66 @@ +using Microsoft.EntityFrameworkCore;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Models.Validation;
+using Timeline.Services.Exceptions;
+
+namespace Timeline.Services
+{
+ /// <summary>
+ /// This service provide some basic user features, which should be used internally for other services.
+ /// </summary>
+ public interface IBasicUserService
+ {
+ /// <summary>
+ /// Check if a user exists.
+ /// </summary>
+ /// <param name="id">The id of the user.</param>
+ /// <returns>True if exists. Otherwise false.</returns>
+ Task<bool> CheckUserExistence(long id);
+
+ /// <summary>
+ /// Get the user id of given username.
+ /// </summary>
+ /// <param name="username">Username of the user.</param>
+ /// <returns>The id of the user.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> is null.</exception>
+ /// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
+ /// <exception cref="UserNotExistException">Thrown when the user with given username does not exist.</exception>
+ Task<long> GetUserIdByUsername(string username);
+ }
+
+ public class BasicUserService : IBasicUserService
+ {
+ private readonly DatabaseContext _database;
+
+ private readonly UsernameValidator _usernameValidator = new UsernameValidator();
+
+ public BasicUserService(DatabaseContext database)
+ {
+ _database = database;
+ }
+
+ public async Task<bool> CheckUserExistence(long id)
+ {
+ return await _database.Users.AnyAsync(u => u.Id == id);
+ }
+
+ public async Task<long> GetUserIdByUsername(string username)
+ {
+ if (username == null)
+ throw new ArgumentNullException(nameof(username));
+
+ if (!_usernameValidator.Validate(username, out var message))
+ throw new ArgumentException(message);
+
+ var entity = await _database.Users.Where(user => user.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync();
+
+ if (entity == null)
+ throw new UserNotExistException(username);
+
+ return entity.Id;
+ }
+ }
+}
|