From e619eac394b4e1c7a3cb0c5902b2776314c7b979 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Apr 2021 19:37:11 +0800 Subject: refactor: ... --- .../Services/UserAvatarService.Designer.cs | 108 --------- .../Resources/Services/UserAvatarService.resx | 135 ----------- .../User/Avatar/DefaultUserAvatarProvider.cs | 51 ++++ .../User/Avatar/IDefaultUserAvatarProvider.cs | 27 +++ .../Services/User/Avatar/IUserAvatarService.cs | 45 ++++ .../Services/User/Avatar/UserAvatarService.cs | 154 ++++++++++++ ...serAvatarServicesServiceCollectionExtensions.cs | 14 ++ .../Timeline/Services/User/UserAvatarService.cs | 266 --------------------- .../UserServicesServiceCollectionExtensions.cs | 26 ++ BackEnd/Timeline/Startup.cs | 17 +- BackEnd/Timeline/Timeline.csproj | 9 - 11 files changed, 322 insertions(+), 530 deletions(-) delete mode 100644 BackEnd/Timeline/Resources/Services/UserAvatarService.Designer.cs delete mode 100644 BackEnd/Timeline/Resources/Services/UserAvatarService.resx create mode 100644 BackEnd/Timeline/Services/User/Avatar/DefaultUserAvatarProvider.cs create mode 100644 BackEnd/Timeline/Services/User/Avatar/IDefaultUserAvatarProvider.cs create mode 100644 BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs create mode 100644 BackEnd/Timeline/Services/User/Avatar/UserAvatarService.cs create mode 100644 BackEnd/Timeline/Services/User/Avatar/UserAvatarServicesServiceCollectionExtensions.cs delete mode 100644 BackEnd/Timeline/Services/User/UserAvatarService.cs create mode 100644 BackEnd/Timeline/Services/User/UserServicesServiceCollectionExtensions.cs (limited to 'BackEnd') diff --git a/BackEnd/Timeline/Resources/Services/UserAvatarService.Designer.cs b/BackEnd/Timeline/Resources/Services/UserAvatarService.Designer.cs deleted file mode 100644 index c72d4215..00000000 --- a/BackEnd/Timeline/Resources/Services/UserAvatarService.Designer.cs +++ /dev/null @@ -1,108 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Timeline.Resources.Services { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class UserAvatarService { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal UserAvatarService() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Resources.Services.UserAvatarService", typeof(UserAvatarService).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to Data of avatar is null.. - /// - internal static string ExceptionAvatarDataNull { - get { - return ResourceManager.GetString("ExceptionAvatarDataNull", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Type of avatar is null or empty.. - /// - internal static string ExceptionAvatarTypeNullOrEmpty { - get { - return ResourceManager.GetString("ExceptionAvatarTypeNullOrEmpty", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Database corupted! One of type and data of a avatar is null but the other is not.. - /// - internal static string ExceptionDatabaseCorruptedDataAndTypeNotSame { - get { - return ResourceManager.GetString("ExceptionDatabaseCorruptedDataAndTypeNotSame", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Created an entry in user_avatars.. - /// - internal static string LogCreateEntity { - get { - return ResourceManager.GetString("LogCreateEntity", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Updated an entry in user_avatars.. - /// - internal static string LogUpdateEntity { - get { - return ResourceManager.GetString("LogUpdateEntity", resourceCulture); - } - } - } -} diff --git a/BackEnd/Timeline/Resources/Services/UserAvatarService.resx b/BackEnd/Timeline/Resources/Services/UserAvatarService.resx deleted file mode 100644 index da9d7203..00000000 --- a/BackEnd/Timeline/Resources/Services/UserAvatarService.resx +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Data of avatar is null. - - - Type of avatar is null or empty. - - - Database corupted! One of type and data of a avatar is null but the other is not. - - - Created an entry in user_avatars. - - - Updated an entry in user_avatars. - - \ No newline at end of file diff --git a/BackEnd/Timeline/Services/User/Avatar/DefaultUserAvatarProvider.cs b/BackEnd/Timeline/Services/User/Avatar/DefaultUserAvatarProvider.cs new file mode 100644 index 00000000..9f8f1c62 --- /dev/null +++ b/BackEnd/Timeline/Services/User/Avatar/DefaultUserAvatarProvider.cs @@ -0,0 +1,51 @@ +using Microsoft.AspNetCore.Hosting; +using SixLabors.ImageSharp; +using System.IO; +using System.Threading.Tasks; +using Timeline.Helpers.Cache; +using Timeline.Models; +using Timeline.Services.Data; + +namespace Timeline.Services.User.Avatar +{ + // TODO! : Make this configurable. + public class DefaultUserAvatarProvider : IDefaultUserAvatarProvider + { + private readonly IETagGenerator _eTagGenerator; + + private readonly string _avatarPath; + + private CacheableDataDigest? _cacheDigest; + private ByteData? _cacheData; + + public DefaultUserAvatarProvider(IWebHostEnvironment environment, IETagGenerator eTagGenerator) + { + _avatarPath = Path.Combine(environment.ContentRootPath, "default-avatar.png"); + _eTagGenerator = eTagGenerator; + } + + private async Task CheckAndInit() + { + var path = _avatarPath; + if (_cacheData == null || File.GetLastWriteTime(path) > _cacheDigest!.LastModified) + { + var data = await File.ReadAllBytesAsync(path); + _cacheDigest = new CacheableDataDigest(await _eTagGenerator.GenerateETagAsync(data), File.GetLastWriteTime(path)); + Image.Identify(data, out var format); + _cacheData = new ByteData(data, format.DefaultMimeType); + } + } + + public async Task GetDefaultAvatarDigest() + { + await CheckAndInit(); + return _cacheDigest!; + } + + public async Task GetDefaultAvatar() + { + await CheckAndInit(); + return _cacheData!; + } + } +} diff --git a/BackEnd/Timeline/Services/User/Avatar/IDefaultUserAvatarProvider.cs b/BackEnd/Timeline/Services/User/Avatar/IDefaultUserAvatarProvider.cs new file mode 100644 index 00000000..50c9ac91 --- /dev/null +++ b/BackEnd/Timeline/Services/User/Avatar/IDefaultUserAvatarProvider.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using Timeline.Helpers.Cache; +using Timeline.Models; + +namespace Timeline.Services.User.Avatar +{ + /// + /// Provider for default user avatar. + /// + /// + /// Mainly for unit tests. + /// + public interface IDefaultUserAvatarProvider + { + /// + /// Get the digest of default avatar. + /// + /// The digest. + Task GetDefaultAvatarDigest(); + + /// + /// Get the default avatar. + /// + /// The avatar. + Task GetDefaultAvatar(); + } +} diff --git a/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs b/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs new file mode 100644 index 00000000..fda35aac --- /dev/null +++ b/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs @@ -0,0 +1,45 @@ +using System; +using System.Threading.Tasks; +using Timeline.Helpers.Cache; +using Timeline.Models; +using Timeline.Services.Imaging; + +namespace Timeline.Services.User.Avatar +{ + public interface IUserAvatarService + { + /// + /// Get avatar digest of a user. + /// + /// User id. + /// The avatar digest. + /// Thrown when user does not exist. + Task GetAvatarDigest(long userId); + + /// + /// Get avatar of a user. If the user has no avatar set, a default one is returned. + /// + /// User id. + /// The avatar. + /// Thrown when user does not exist. + Task GetAvatar(long userId); + + /// + /// Set avatar for a user. + /// + /// User id. + /// The new avatar data. + /// The digest of the avatar. + /// Thrown if is null. + /// Thrown when user does not exist. + /// Thrown if avatar is of bad format. + Task SetAvatar(long userId, ByteData avatar); + + /// + /// Remove avatar of a user. + /// + /// User id. + /// Thrown when user does not exist. + Task DeleteAvatar(long userId); + } +} diff --git a/BackEnd/Timeline/Services/User/Avatar/UserAvatarService.cs b/BackEnd/Timeline/Services/User/Avatar/UserAvatarService.cs new file mode 100644 index 00000000..4f943e3a --- /dev/null +++ b/BackEnd/Timeline/Services/User/Avatar/UserAvatarService.cs @@ -0,0 +1,154 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using System; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Entities; +using Timeline.Helpers.Cache; +using Timeline.Models; +using Timeline.Services.Data; +using Timeline.Services.Imaging; + +namespace Timeline.Services.User.Avatar +{ + public class UserAvatarService : IUserAvatarService + { + private readonly ILogger _logger; + private readonly DatabaseContext _database; + private readonly IBasicUserService _basicUserService; + private readonly IDefaultUserAvatarProvider _defaultUserAvatarProvider; + private readonly IImageService _imageService; + private readonly IDataManager _dataManager; + private readonly IClock _clock; + + public UserAvatarService( + ILogger logger, + DatabaseContext database, + IBasicUserService basicUserService, + IDefaultUserAvatarProvider defaultUserAvatarProvider, + IImageService imageValidator, + IDataManager dataManager, + IClock clock) + { + _logger = logger; + _database = database; + _basicUserService = basicUserService; + _defaultUserAvatarProvider = defaultUserAvatarProvider; + _imageService = imageValidator; + _dataManager = dataManager; + _clock = clock; + } + + public async Task GetAvatarDigest(long userId) + { + var usernameChangeTime = await _basicUserService.GetUsernameLastModifiedTimeAsync(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.DataTag, a.LastModified }).SingleOrDefaultAsync(); + + if (entity is null) + { + var defaultAvatarDigest = await _defaultUserAvatarProvider.GetDefaultAvatarDigest(); + return new CacheableDataDigest(defaultAvatarDigest.ETag, new DateTime[] { usernameChangeTime, defaultAvatarDigest.LastModified }.Max()); + } + else if (entity.DataTag is null) + { + var defaultAvatarDigest = await _defaultUserAvatarProvider.GetDefaultAvatarDigest(); + return new CacheableDataDigest(defaultAvatarDigest.ETag, new DateTime[] { usernameChangeTime, defaultAvatarDigest.LastModified, entity.LastModified }.Max()); + } + else + { + return new CacheableDataDigest(entity.DataTag, new DateTime[] { usernameChangeTime, entity.LastModified }.Max()); + } + } + + public async Task GetAvatar(long userId) + { + await _basicUserService.ThrowIfUserNotExist(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); + + if (entity is null || entity.DataTag is null) + { + return await _defaultUserAvatarProvider.GetDefaultAvatar(); + } + + var data = await _dataManager.GetEntryAndCheck(entity.DataTag, $"This is required by avatar of {userId}."); + + if (entity.Type is null) + { + var format = await _imageService.DetectFormatAsync(data); + entity.Type = format.DefaultMimeType; + await _database.SaveChangesAsync(); + } + + return new ByteData(data, entity.Type); + } + + public async Task SetAvatar(long userId, ByteData avatar) + { + if (avatar is null) + throw new ArgumentNullException(nameof(avatar)); + + await _imageService.ValidateAsync(avatar.Data, avatar.ContentType, true); + + await _basicUserService.ThrowIfUserNotExist(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); + + await using var transaction = await _database.Database.BeginTransactionAsync(); + + var tag = await _dataManager.RetainEntryAsync(avatar.Data); + + var now = _clock.GetCurrentTime(); + + if (entity is null) + { + var newEntity = new UserAvatarEntity + { + DataTag = tag, + Type = avatar.ContentType, + LastModified = now, + UserId = userId + }; + _database.Add(newEntity); + } + else + { + if (entity.DataTag is not null) + await _dataManager.FreeEntryAsync(entity.DataTag); + + entity.DataTag = tag; + entity.Type = avatar.ContentType; + entity.LastModified = now; + } + + await _database.SaveChangesAsync(); + + await transaction.CommitAsync(); + + return new CacheableDataDigest(tag, now); + } + + public async Task DeleteAvatar(long userId) + { + await _basicUserService.ThrowIfUserNotExist(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); + + if (entity is null || entity.DataTag is null) + return; + + await using var transaction = await _database.Database.BeginTransactionAsync(); + + await _dataManager.FreeEntryAsync(entity.DataTag); + + entity.DataTag = null; + entity.Type = null; + entity.LastModified = _clock.GetCurrentTime(); + + await _database.SaveChangesAsync(); + + await transaction.CommitAsync(); + } + } +} diff --git a/BackEnd/Timeline/Services/User/Avatar/UserAvatarServicesServiceCollectionExtensions.cs b/BackEnd/Timeline/Services/User/Avatar/UserAvatarServicesServiceCollectionExtensions.cs new file mode 100644 index 00000000..57279855 --- /dev/null +++ b/BackEnd/Timeline/Services/User/Avatar/UserAvatarServicesServiceCollectionExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Timeline.Services.User.Avatar +{ + public static class UserAvatarServicesServiceCollectionExtensions + { + public static void AddUserAvatarServices(this IServiceCollection services) + { + services.TryAddScoped(); + services.TryAddScoped(); + } + } +} diff --git a/BackEnd/Timeline/Services/User/UserAvatarService.cs b/BackEnd/Timeline/Services/User/UserAvatarService.cs deleted file mode 100644 index 9f59624d..00000000 --- a/BackEnd/Timeline/Services/User/UserAvatarService.cs +++ /dev/null @@ -1,266 +0,0 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using SixLabors.ImageSharp; -using System; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Timeline.Entities; -using Timeline.Helpers.Cache; -using Timeline.Models; -using Timeline.Services.Data; -using Timeline.Services.Imaging; - -namespace Timeline.Services.User -{ - /// - /// Provider for default user avatar. - /// - /// - /// Mainly for unit tests. - /// - public interface IDefaultUserAvatarProvider - { - /// - /// Get the digest of default avatar. - /// - /// The digest. - Task GetDefaultAvatarDigest(); - - /// - /// Get the default avatar. - /// - /// The avatar. - Task GetDefaultAvatar(); - } - - public interface IUserAvatarService - { - /// - /// Get avatar digest of a user. - /// - /// User id. - /// The avatar digest. - /// Thrown when user does not exist. - Task GetAvatarDigest(long userId); - - /// - /// Get avatar of a user. If the user has no avatar set, a default one is returned. - /// - /// User id. - /// The avatar. - /// Thrown when user does not exist. - Task GetAvatar(long userId); - - /// - /// Set avatar for a user. - /// - /// User id. - /// The new avatar data. - /// The digest of the avatar. - /// Thrown if is null. - /// Thrown when user does not exist. - /// Thrown if avatar is of bad format. - Task SetAvatar(long userId, ByteData avatar); - - /// - /// Remove avatar of a user. - /// - /// User id. - /// Thrown when user does not exist. - Task DeleteAvatar(long userId); - } - - // TODO! : Make this configurable. - public class DefaultUserAvatarProvider : IDefaultUserAvatarProvider - { - private readonly IETagGenerator _eTagGenerator; - - private readonly string _avatarPath; - - private CacheableDataDigest? _cacheDigest; - private ByteData? _cacheData; - - public DefaultUserAvatarProvider(IWebHostEnvironment environment, IETagGenerator eTagGenerator) - { - _avatarPath = Path.Combine(environment.ContentRootPath, "default-avatar.png"); - _eTagGenerator = eTagGenerator; - } - - private async Task CheckAndInit() - { - var path = _avatarPath; - if (_cacheData == null || File.GetLastWriteTime(path) > _cacheDigest!.LastModified) - { - var data = await File.ReadAllBytesAsync(path); - _cacheDigest = new CacheableDataDigest(await _eTagGenerator.GenerateETagAsync(data), File.GetLastWriteTime(path)); - Image.Identify(data, out var format); - _cacheData = new ByteData(data, format.DefaultMimeType); - } - } - - public async Task GetDefaultAvatarDigest() - { - await CheckAndInit(); - return _cacheDigest!; - } - - public async Task GetDefaultAvatar() - { - await CheckAndInit(); - return _cacheData!; - } - } - - public class UserAvatarService : IUserAvatarService - { - private readonly ILogger _logger; - private readonly DatabaseContext _database; - private readonly IBasicUserService _basicUserService; - private readonly IDefaultUserAvatarProvider _defaultUserAvatarProvider; - private readonly IImageService _imageValidator; - private readonly IDataManager _dataManager; - private readonly IClock _clock; - - public UserAvatarService( - ILogger logger, - DatabaseContext database, - IBasicUserService basicUserService, - IDefaultUserAvatarProvider defaultUserAvatarProvider, - IImageService imageValidator, - IDataManager dataManager, - IClock clock) - { - _logger = logger; - _database = database; - _basicUserService = basicUserService; - _defaultUserAvatarProvider = defaultUserAvatarProvider; - _imageValidator = imageValidator; - _dataManager = dataManager; - _clock = clock; - } - - public async Task GetAvatarDigest(long userId) - { - var usernameChangeTime = await _basicUserService.GetUsernameLastModifiedTimeAsync(userId); - - var entity = await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.DataTag, a.LastModified }).SingleOrDefaultAsync(); - - if (entity is null) - { - var defaultAvatarDigest = await _defaultUserAvatarProvider.GetDefaultAvatarDigest(); - return new CacheableDataDigest(defaultAvatarDigest.ETag, new DateTime[] { usernameChangeTime, defaultAvatarDigest.LastModified }.Max()); - } - else if (entity.DataTag is null) - { - var defaultAvatarDigest = await _defaultUserAvatarProvider.GetDefaultAvatarDigest(); - return new CacheableDataDigest(defaultAvatarDigest.ETag, new DateTime[] { usernameChangeTime, defaultAvatarDigest.LastModified, entity.LastModified }.Max()); - } - else - { - return new CacheableDataDigest(entity.DataTag, new DateTime[] { usernameChangeTime, entity.LastModified }.Max()); - } - } - - public async Task GetAvatar(long userId) - { - await _basicUserService.ThrowIfUserNotExist(userId); - - var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); - - if (entity is null || entity.DataTag is null) - { - return await _defaultUserAvatarProvider.GetDefaultAvatar(); - } - - var data = await _dataManager.GetEntryAndCheck(entity.DataTag, $"This is required by avatar of {userId}."); - - if (entity.Type is null) - { - Image.Identify(data, out var format); - entity.Type = format.DefaultMimeType; - await _database.SaveChangesAsync(); - } - - return new ByteData(data, entity.Type); - } - - public async Task SetAvatar(long userId, ByteData avatar) - { - if (avatar is null) - throw new ArgumentNullException(nameof(avatar)); - - await _imageValidator.ValidateAsync(avatar.Data, avatar.ContentType, true); - - await _basicUserService.ThrowIfUserNotExist(userId); - - var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); - - await using var transaction = await _database.Database.BeginTransactionAsync(); - - var tag = await _dataManager.RetainEntryAsync(avatar.Data); - - var now = _clock.GetCurrentTime(); - - if (entity is null) - { - var newEntity = new UserAvatarEntity - { - DataTag = tag, - Type = avatar.ContentType, - LastModified = now, - UserId = userId - }; - _database.Add(newEntity); - } - else - { - if (entity.DataTag is not null) - await _dataManager.FreeEntryAsync(entity.DataTag); - - entity.DataTag = tag; - entity.Type = avatar.ContentType; - entity.LastModified = now; - } - - await _database.SaveChangesAsync(); - - await transaction.CommitAsync(); - - return new CacheableDataDigest(tag, now); - } - - public async Task DeleteAvatar(long userId) - { - await _basicUserService.ThrowIfUserNotExist(userId); - - var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); - - if (entity is null || entity.DataTag is null) - return; - - await using var transaction = await _database.Database.BeginTransactionAsync(); - - await _dataManager.FreeEntryAsync(entity.DataTag); - - entity.DataTag = null; - entity.Type = null; - entity.LastModified = _clock.GetCurrentTime(); - - await _database.SaveChangesAsync(); - - await transaction.CommitAsync(); - } - } - - public static class UserAvatarServiceCollectionExtensions - { - public static void AddUserAvatarService(this IServiceCollection services) - { - services.AddScoped(); - services.AddScoped(); - } - } -} diff --git a/BackEnd/Timeline/Services/User/UserServicesServiceCollectionExtensions.cs b/BackEnd/Timeline/Services/User/UserServicesServiceCollectionExtensions.cs new file mode 100644 index 00000000..6320b608 --- /dev/null +++ b/BackEnd/Timeline/Services/User/UserServicesServiceCollectionExtensions.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Services.User.Avatar; + +namespace Timeline.Services.User +{ + public static class UserServicesServiceCollectionExtensions + { + public static IServiceCollection AddUserServices(this IServiceCollection services) + { + services.TryAddTransient(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + + services.AddUserAvatarServices(); + + return services; + } + } +} diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index c0873113..32208d53 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -86,11 +86,6 @@ namespace Timeline services.AddAuthorization(); services.AddSingleton(); - services.AddBasicServices(); - services.AddDatabaseManagementService(); - services.AddDataServices(); - services.AddImageServices(); - services.AddAutoMapper(GetType().Assembly); services.AddMappers(); @@ -100,13 +95,11 @@ namespace Timeline options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); }); - services.AddTransient(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddUserAvatarService(); - + services.AddBasicServices(); + services.AddDatabaseManagementService(); + services.AddDataServices(); + services.AddImageServices(); + services.AddUserServices(); services.AddTokenService(Configuration); services.AddScoped(); diff --git a/BackEnd/Timeline/Timeline.csproj b/BackEnd/Timeline/Timeline.csproj index dc41a7f8..4c40f209 100644 --- a/BackEnd/Timeline/Timeline.csproj +++ b/BackEnd/Timeline/Timeline.csproj @@ -128,11 +128,6 @@ True TimelineService.resx - - True - True - UserAvatarService.resx - True True @@ -241,10 +236,6 @@ ResXFileCodeGenerator TimelineService.Designer.cs - - ResXFileCodeGenerator - UserAvatarService.Designer.cs - ResXFileCodeGenerator UserTokenService.Designer.cs -- cgit v1.2.3