From b0d5051957e352d6d1d557cc32cbafdfd5bbfc5f Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Thu, 25 Apr 2024 17:29:14 +0800 Subject: [PATCH] refactor --- .../Service/AvatarInfo/AvatarInfoDbService.cs | 36 ++---- .../Service/AvatarInfo/AvatarInfoService.cs | 103 +++++++++--------- .../AvatarInfo/IAvatarInfoDbService.cs | 7 +- 3 files changed, 62 insertions(+), 84 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoDbService.cs b/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoDbService.cs index f8683628..31014a30 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoDbService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoDbService.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using Snap.Hutao.Core.Database; using Snap.Hutao.Model.Entity.Database; +using Snap.Hutao.Service.Abstraction; using EntityAvatarInfo = Snap.Hutao.Model.Entity.AvatarInfo; namespace Snap.Hutao.Service.AvatarInfo; @@ -14,44 +15,25 @@ internal sealed partial class AvatarInfoDbService : IAvatarInfoDbService { private readonly IServiceProvider serviceProvider; + public IServiceProvider ServiceProvider { get => serviceProvider; } + public List GetAvatarInfoListByUid(string uid) { - using (IServiceScope scope = serviceProvider.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - IQueryable result = appDbContext.AvatarInfos.AsNoTracking().Where(i => i.Uid == uid); - return [.. result]; - } + return this.List(i => i.Uid == uid); } - public async ValueTask> GetAvatarInfoListByUidAsync(string uid) + public ValueTask> GetAvatarInfoListByUidAsync(string uid, CancellationToken token = default) { - using (IServiceScope scope = serviceProvider.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - return await appDbContext.AvatarInfos - .AsNoTracking() - .Where(i => i.Uid == uid) - .ToListAsync() - .ConfigureAwait(false); - } + return this.ListAsync(i => i.Uid == uid, token); } public void RemoveAvatarInfoRangeByUid(string uid) { - using (IServiceScope scope = serviceProvider.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.AvatarInfos.ExecuteDeleteWhere(i => i.Uid == uid); - } + this.Execute(dbset => dbset.Where(i => i.Uid == uid).ExecuteDelete()); } - public async ValueTask RemoveAvatarInfoRangeByUidAsync(string uid) + public async ValueTask RemoveAvatarInfoRangeByUidAsync(string uid, CancellationToken token = default) { - using (IServiceScope scope = serviceProvider.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - await appDbContext.AvatarInfos.ExecuteDeleteWhereAsync(i => i.Uid == uid).ConfigureAwait(false); - } + await this.ExecuteAsync((dbset, token) => dbset.Where(i => i.Uid == uid).ExecuteDeleteAsync(token), token).ConfigureAwait(false); } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoService.cs b/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoService.cs index 1846f283..bbed4250 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/AvatarInfoService.cs @@ -27,63 +27,58 @@ internal sealed partial class AvatarInfoService : IAvatarInfoService public async ValueTask> GetSummaryAsync(UserAndUid userAndUid, RefreshOption refreshOption, CancellationToken token = default) { - if (await metadataService.InitializeAsync().ConfigureAwait(false)) - { - token.ThrowIfCancellationRequested(); - - switch (refreshOption) - { - case RefreshOption.RequestFromEnkaAPI: - { - EnkaResponse? resp = await GetEnkaResponseAsync(userAndUid.Uid, token).ConfigureAwait(false); - token.ThrowIfCancellationRequested(); - if (resp is null) - { - return new(RefreshResult.APIUnavailable, default); - } - - if (!string.IsNullOrEmpty(resp.Message)) - { - return new(RefreshResult.StatusCodeNotSucceed, new Summary { Message = resp.Message }); - } - - if (!resp.IsValid) - { - return new(RefreshResult.ShowcaseNotOpen, default); - } - - List list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByShowcaseAsync(userAndUid.Uid.Value, resp.AvatarInfoList, token); - Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); - return new(RefreshResult.Ok, summary); - } - - case RefreshOption.RequestFromHoyolabGameRecord: - { - List list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByGameRecordCharacterAsync(userAndUid, token).ConfigureAwait(false); - Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); - return new(RefreshResult.Ok, summary); - } - - case RefreshOption.RequestFromHoyolabCalculate: - { - List list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByCalculateAvatarDetailAsync(userAndUid, token).ConfigureAwait(false); - Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); - return new(RefreshResult.Ok, summary); - } - - default: - { - List list = await avatarInfoDbService.GetAvatarInfoListByUidAsync(userAndUid.Uid.Value).ConfigureAwait(false); - Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); - token.ThrowIfCancellationRequested(); - return new(RefreshResult.Ok, summary.Avatars.Count == 0 ? null : summary); - } - } - } - else + if (!await metadataService.InitializeAsync().ConfigureAwait(false)) { return new(RefreshResult.MetadataNotInitialized, null); } + + switch (refreshOption) + { + case RefreshOption.RequestFromEnkaAPI: + { + EnkaResponse? resp = await GetEnkaResponseAsync(userAndUid.Uid, token).ConfigureAwait(false); + + if (resp is null) + { + return new(RefreshResult.APIUnavailable, default); + } + + if (!string.IsNullOrEmpty(resp.Message)) + { + return new(RefreshResult.StatusCodeNotSucceed, new Summary { Message = resp.Message }); + } + + if (!resp.IsValid) + { + return new(RefreshResult.ShowcaseNotOpen, default); + } + + List list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByShowcaseAsync(userAndUid.Uid.Value, resp.AvatarInfoList, token).ConfigureAwait(false); + Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); + return new(RefreshResult.Ok, summary); + } + + case RefreshOption.RequestFromHoyolabGameRecord: + { + List list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByGameRecordCharacterAsync(userAndUid, token).ConfigureAwait(false); + Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); + return new(RefreshResult.Ok, summary); + } + + case RefreshOption.RequestFromHoyolabCalculate: + { + List list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByCalculateAvatarDetailAsync(userAndUid, token).ConfigureAwait(false); + Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); + return new(RefreshResult.Ok, summary); + } + + default: + { + List list = await avatarInfoDbService.GetAvatarInfoListByUidAsync(userAndUid.Uid.Value, token).ConfigureAwait(false); + Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false); + return new(RefreshResult.Ok, summary.Avatars.Count == 0 ? null : summary); + } + } } private async ValueTask GetEnkaResponseAsync(PlayerUid uid, CancellationToken token = default) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/IAvatarInfoDbService.cs b/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/IAvatarInfoDbService.cs index a2663cee..ad1ddb5f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/IAvatarInfoDbService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AvatarInfo/IAvatarInfoDbService.cs @@ -1,17 +1,18 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using Snap.Hutao.Service.Abstraction; using EntityAvatarInfo = Snap.Hutao.Model.Entity.AvatarInfo; namespace Snap.Hutao.Service.AvatarInfo; -internal interface IAvatarInfoDbService +internal interface IAvatarInfoDbService : IAppDbService { void RemoveAvatarInfoRangeByUid(string uid); List GetAvatarInfoListByUid(string uid); - ValueTask> GetAvatarInfoListByUidAsync(string uid); + ValueTask> GetAvatarInfoListByUidAsync(string uid, CancellationToken token = default); - ValueTask RemoveAvatarInfoRangeByUidAsync(string uid); + ValueTask RemoveAvatarInfoRangeByUidAsync(string uid, CancellationToken token = default); } \ No newline at end of file