diff options
author | crupest <crupest@outlook.com> | 2021-04-25 21:20:04 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-25 21:20:04 +0800 |
commit | a4a75188bd17e31b39a02511bbd6d628bab5c909 (patch) | |
tree | f2f9c63eb5beabb7a1a2f2605c2d5022f6a72c08 /BackEnd/Timeline/Services/User/UserDeleteService.cs | |
parent | 434be212c77bdade04722046e92c3dac25d0aff3 (diff) | |
download | timeline-a4a75188bd17e31b39a02511bbd6d628bab5c909.tar.gz timeline-a4a75188bd17e31b39a02511bbd6d628bab5c909.tar.bz2 timeline-a4a75188bd17e31b39a02511bbd6d628bab5c909.zip |
...
Diffstat (limited to 'BackEnd/Timeline/Services/User/UserDeleteService.cs')
-rw-r--r-- | BackEnd/Timeline/Services/User/UserDeleteService.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/User/UserDeleteService.cs b/BackEnd/Timeline/Services/User/UserDeleteService.cs new file mode 100644 index 00000000..8da4678a --- /dev/null +++ b/BackEnd/Timeline/Services/User/UserDeleteService.cs @@ -0,0 +1,70 @@ +using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Globalization;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Models.Validation;
+using Timeline.Services.Timeline;
+
+namespace Timeline.Services.User
+{
+ public interface IUserDeleteService
+ {
+ /// <summary>
+ /// Delete a user of given username.
+ /// </summary>
+ /// <param name="username">Username of the user to delete. Can't be null.</param>
+ /// <returns>True if user is deleted, false if user not exist.</returns>
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="username"/> is null.</exception>
+ /// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
+ /// <exception cref="InvalidOperationOnRootUserException">Thrown when deleting root user.</exception>
+ Task<bool> DeleteUser(string username);
+ }
+
+ public class UserDeleteService : IUserDeleteService
+ {
+ private readonly ILogger<UserDeleteService> _logger;
+
+ private readonly DatabaseContext _databaseContext;
+
+ private readonly ITimelinePostService _timelinePostService;
+
+ private readonly UsernameValidator _usernameValidator = new UsernameValidator();
+
+ public UserDeleteService(ILogger<UserDeleteService> logger, DatabaseContext databaseContext, ITimelinePostService timelinePostService)
+ {
+ _logger = logger;
+ _databaseContext = databaseContext;
+ _timelinePostService = timelinePostService;
+ }
+
+ public async Task<bool> DeleteUser(string username)
+ {
+ if (username == null)
+ throw new ArgumentNullException(nameof(username));
+
+ if (!_usernameValidator.Validate(username, out var message))
+ {
+ throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resource.ExceptionUsernameBadFormat, message), nameof(username));
+ }
+
+ var user = await _databaseContext.Users.Where(u => u.Username == username).SingleOrDefaultAsync();
+ if (user == null)
+ return false;
+
+ if (user.Id == 1)
+ throw new InvalidOperationOnRootUserException("Can't delete root user.");
+
+ await _timelinePostService.DeleteAllPostsOfUser(user.Id);
+
+ _databaseContext.Users.Remove(user);
+
+ await _databaseContext.SaveChangesAsync();
+
+ return true;
+ }
+
+ }
+}
|