mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
refactor
This commit is contained in:
@@ -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<EntityAvatarInfo> GetAvatarInfoListByUid(string uid)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
IQueryable<EntityAvatarInfo> result = appDbContext.AvatarInfos.AsNoTracking().Where(i => i.Uid == uid);
|
||||
return [.. result];
|
||||
}
|
||||
return this.List(i => i.Uid == uid);
|
||||
}
|
||||
|
||||
public async ValueTask<List<EntityAvatarInfo>> GetAvatarInfoListByUidAsync(string uid)
|
||||
public ValueTask<List<EntityAvatarInfo>> GetAvatarInfoListByUidAsync(string uid, CancellationToken token = default)
|
||||
{
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
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>();
|
||||
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<AppDbContext>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -27,63 +27,58 @@ internal sealed partial class AvatarInfoService : IAvatarInfoService
|
||||
|
||||
public async ValueTask<ValueResult<RefreshResult, Summary?>> 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<EntityAvatarInfo> 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<EntityAvatarInfo> 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<EntityAvatarInfo> list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByCalculateAvatarDetailAsync(userAndUid, token).ConfigureAwait(false);
|
||||
Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false);
|
||||
return new(RefreshResult.Ok, summary);
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
List<EntityAvatarInfo> 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<EntityAvatarInfo> 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<EntityAvatarInfo> 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<EntityAvatarInfo> list = await avatarInfoDbBulkOperation.UpdateDbAvatarInfosByCalculateAvatarDetailAsync(userAndUid, token).ConfigureAwait(false);
|
||||
Summary summary = await GetSummaryCoreAsync(list, token).ConfigureAwait(false);
|
||||
return new(RefreshResult.Ok, summary);
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
List<EntityAvatarInfo> 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<EnkaResponse?> GetEnkaResponseAsync(PlayerUid uid, CancellationToken token = default)
|
||||
|
||||
@@ -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<EntityAvatarInfo>
|
||||
{
|
||||
void RemoveAvatarInfoRangeByUid(string uid);
|
||||
|
||||
List<EntityAvatarInfo> GetAvatarInfoListByUid(string uid);
|
||||
|
||||
ValueTask<List<EntityAvatarInfo>> GetAvatarInfoListByUidAsync(string uid);
|
||||
ValueTask<List<EntityAvatarInfo>> GetAvatarInfoListByUidAsync(string uid, CancellationToken token = default);
|
||||
|
||||
ValueTask RemoveAvatarInfoRangeByUidAsync(string uid);
|
||||
ValueTask RemoveAvatarInfoRangeByUidAsync(string uid, CancellationToken token = default);
|
||||
}
|
||||
Reference in New Issue
Block a user