From ac769e656b122ff569c3f1534701b71e00fed586 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 19:21:35 +0800 Subject: Split front and back end. --- Timeline/Services/UserDeleteService.cs | 69 ---------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 Timeline/Services/UserDeleteService.cs (limited to 'Timeline/Services/UserDeleteService.cs') diff --git a/Timeline/Services/UserDeleteService.cs b/Timeline/Services/UserDeleteService.cs deleted file mode 100644 index 845de573..00000000 --- a/Timeline/Services/UserDeleteService.cs +++ /dev/null @@ -1,69 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Timeline.Entities; -using Timeline.Helpers; -using Timeline.Models.Validation; -using static Timeline.Resources.Services.UserService; - -namespace Timeline.Services -{ - public interface IUserDeleteService - { - /// - /// Delete a user of given username. - /// - /// Username of the user to delete. Can't be null. - /// True if user is deleted, false if user not exist. - /// Thrown if is null. - /// Thrown when is of bad format. - Task DeleteUser(string username); - } - - public class UserDeleteService : IUserDeleteService - { - private readonly ILogger _logger; - - private readonly DatabaseContext _databaseContext; - - private readonly ITimelineService _timelineService; - - private readonly UsernameValidator _usernameValidator = new UsernameValidator(); - - public UserDeleteService(ILogger logger, DatabaseContext databaseContext, ITimelineService timelineService) - { - _logger = logger; - _databaseContext = databaseContext; - _timelineService = timelineService; - } - - public async Task 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, ExceptionUsernameBadFormat, message), nameof(username)); - } - - var user = await _databaseContext.Users.Where(u => u.Username == username).SingleOrDefaultAsync(); - if (user == null) - return false; - - await _timelineService.DeleteAllPostsOfUser(user.Id); - - _databaseContext.Users.Remove(user); - - await _databaseContext.SaveChangesAsync(); - _logger.LogInformation(Log.Format(LogDatabaseRemove, ("Id", user.Id), ("Username", user.Username))); - - return true; - } - - } -} -- cgit v1.2.3