refactor cultivation service

This commit is contained in:
DismissedLight
2023-07-20 16:49:36 +08:00
parent d03f8185b8
commit d665ba22e5
8 changed files with 109 additions and 59 deletions

View File

@@ -52,6 +52,8 @@ internal sealed class DailyNoteEntry : ObservableObject, IMappingFrom<DailyNoteE
/// </summary> /// </summary>
public DailyNote? DailyNote { get; set; } public DailyNote? DailyNote { get; set; }
// TODO: Add RefreshTime
/// <summary> /// <summary>
/// 树脂提醒阈值 /// 树脂提醒阈值
/// </summary> /// </summary>

View File

@@ -217,7 +217,7 @@ internal sealed partial class AvatarInfoDbBulkOperation
// This means that there are duplicate items. // This means that there are duplicate items.
if (distinctCount < dbInfos.Count) if (distinctCount < dbInfos.Count)
{ {
avatarInfoDbService.DeleteAvatarInfoByUid(uid); avatarInfoDbService.DeleteAvatarInfoRangeByUid(uid);
dbInfos = new(); dbInfos = new();
} }
} }

View File

@@ -37,7 +37,7 @@ internal sealed partial class AvatarInfoDbService : IAvatarInfoDbService
} }
} }
public void DeleteAvatarInfoByUid(string uid) public void DeleteAvatarInfoRangeByUid(string uid)
{ {
using (IServiceScope scope = serviceProvider.CreateScope()) using (IServiceScope scope = serviceProvider.CreateScope())
{ {

View File

@@ -8,7 +8,7 @@ namespace Snap.Hutao.Service.AvatarInfo;
internal interface IAvatarInfoDbService internal interface IAvatarInfoDbService
{ {
void DeleteAvatarInfoByUid(string uid); void DeleteAvatarInfoRangeByUid(string uid);
List<EnkaAvatarInfo> GetAvatarInfoInfoListByUid(string uid); List<EnkaAvatarInfo> GetAvatarInfoInfoListByUid(string uid);

View File

@@ -93,4 +93,64 @@ internal sealed partial class CultivationDbService : ICultivationDbService
appDbContext.CultivateItems.UpdateAndSave(item); appDbContext.CultivateItems.UpdateAndSave(item);
} }
} }
public async ValueTask<CultivateEntry?> GetCultivateEntryByProjectIdAndItemIdAsync(Guid projectId, uint itemId)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return await appDbContext.CultivateEntries
.SingleOrDefaultAsync(e => e.ProjectId == projectId && e.Id == itemId)
.ConfigureAwait(false);
}
}
public async ValueTask InsertCultivateEntryAsync(CultivateEntry entry)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateEntries.AddAndSaveAsync(entry).ConfigureAwait(false);
}
}
public async ValueTask DeleteCultivateItemRangeByEntryIdAsync(Guid entryId)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateItems
.ExecuteDeleteWhereAsync(i => i.EntryId == entryId)
.ConfigureAwait(false);
}
}
public async ValueTask InsertCultivateItemRangeAsync(IEnumerable<CultivateItem> toAdd)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateItems.AddRangeAndSaveAsync(toAdd).ConfigureAwait(false);
}
}
public async ValueTask AddCultivateProjectAsync(CultivateProject project)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateProjects.AddAndSaveAsync(project).ConfigureAwait(false);
}
}
public async ValueTask DeleteCultivateProjectByIdAsync(Guid projectId)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateProjects
.ExecuteDeleteWhereAsync(p => p.InnerId == projectId)
.ConfigureAwait(false);
}
}
} }

View File

@@ -50,44 +50,36 @@ internal sealed partial class CultivationService
return ProjectAddResult.InvalidName; return ProjectAddResult.InvalidName;
} }
if (projects!.SingleOrDefault(a => a.Name == project.Name) != null) ArgumentNullException.ThrowIfNull(projects);
if (projects.Any(a => a.Name == project.Name))
{ {
return ProjectAddResult.AlreadyExists; return ProjectAddResult.AlreadyExists;
} }
else
{
// Sync cache // Sync cache
await taskContext.SwitchToMainThreadAsync(); await taskContext.SwitchToMainThreadAsync();
projects!.Add(project); projects.Add(project);
// Sync database // Sync database
await taskContext.SwitchToBackgroundAsync(); await taskContext.SwitchToBackgroundAsync();
using (IServiceScope scope = serviceProvider.CreateScope()) await cultivationDbService.AddCultivateProjectAsync(project).ConfigureAwait(false);
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateProjects.AddAndSaveAsync(project).ConfigureAwait(false);
}
return ProjectAddResult.Added; return ProjectAddResult.Added;
} }
}
/// <inheritdoc/> /// <inheritdoc/>
public async Task RemoveProjectAsync(CultivateProject project) public async Task RemoveProjectAsync(CultivateProject project)
{ {
ArgumentNullException.ThrowIfNull(projects);
// Sync cache // Sync cache
// Keep this on main thread. // Keep this on main thread.
await taskContext.SwitchToMainThreadAsync(); await taskContext.SwitchToMainThreadAsync();
projects!.Remove(project); projects.Remove(project);
// Sync database // Sync database
await taskContext.SwitchToBackgroundAsync(); await taskContext.SwitchToBackgroundAsync();
using (IServiceScope scope = serviceProvider.CreateScope()) await cultivationDbService.DeleteCultivateProjectByIdAsync(project.InnerId).ConfigureAwait(false);
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await appDbContext.CultivateProjects
.ExecuteDeleteWhereAsync(p => p.InnerId == project.InnerId)
.ConfigureAwait(false);
}
} }
} }

View File

@@ -167,41 +167,27 @@ internal sealed partial class CultivationService : ICultivationService
} }
await taskContext.SwitchToBackgroundAsync(); await taskContext.SwitchToBackgroundAsync();
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
try if (Current is null)
{
Current ??= appDbContext.CultivateProjects.SelectedOrDefault();
}
catch (InvalidOperationException ex)
{
ThrowHelper.UserdataCorrupted(SH.ServiceCultivationProjectCurrentUserdataCourrpted2, ex);
}
if (Current == null)
{ {
return false; return false;
} }
Guid projectId = Current!.InnerId; CultivateEntry? entry = await cultivationDbService
CultivateEntry? entry = await appDbContext.CultivateEntries .GetCultivateEntryByProjectIdAndItemIdAsync(Current.InnerId, itemId)
.SingleOrDefaultAsync(e => e.ProjectId == projectId && e.Id == itemId)
.ConfigureAwait(false); .ConfigureAwait(false);
if (entry == null) if (entry == null)
{ {
entry = CultivateEntry.From(projectId, type, itemId); entry = CultivateEntry.From(Current.InnerId, type, itemId);
await appDbContext.CultivateEntries.AddAndSaveAsync(entry).ConfigureAwait(false); await cultivationDbService.InsertCultivateEntryAsync(entry).ConfigureAwait(false);
} }
Guid entryId = entry.InnerId; Guid entryId = entry.InnerId;
await appDbContext.CultivateItems.ExecuteDeleteWhereAsync(i => i.EntryId == entryId).ConfigureAwait(false); await cultivationDbService.DeleteCultivateItemRangeByEntryIdAsync(entryId).ConfigureAwait(false);
IEnumerable<CultivateItem> toAdd = items.Select(item => CultivateItem.From(entryId, item)); IEnumerable<CultivateItem> toAdd = items.Select(item => CultivateItem.From(entryId, item));
await appDbContext.CultivateItems.AddRangeAndSaveAsync(toAdd).ConfigureAwait(false); await cultivationDbService.InsertCultivateItemRangeAsync(toAdd).ConfigureAwait(false);
}
return true; return true;
} }

View File

@@ -7,8 +7,14 @@ namespace Snap.Hutao.Service.Cultivation;
internal interface ICultivationDbService internal interface ICultivationDbService
{ {
ValueTask AddCultivateProjectAsync(CultivateProject project);
ValueTask DeleteCultivateEntryByIdAsync(Guid entryId); ValueTask DeleteCultivateEntryByIdAsync(Guid entryId);
ValueTask DeleteCultivateItemRangeByEntryIdAsync(Guid entryId);
ValueTask DeleteCultivateProjectByIdAsync(Guid projectId);
ValueTask<CultivateEntry?> GetCultivateEntryByProjectIdAndItemIdAsync(Guid projectId, uint itemId);
ValueTask<List<CultivateEntry>> GetCultivateEntryListByProjectIdAsync(Guid projectId); ValueTask<List<CultivateEntry>> GetCultivateEntryListByProjectIdAsync(Guid projectId);
ValueTask<List<CultivateItem>> GetCultivateItemListByEntryIdAsync(Guid entryId); ValueTask<List<CultivateItem>> GetCultivateItemListByEntryIdAsync(Guid entryId);
@@ -17,6 +23,10 @@ internal interface ICultivationDbService
ValueTask<List<InventoryItem>> GetInventoryItemListByProjectIdAsync(Guid projectId); ValueTask<List<InventoryItem>> GetInventoryItemListByProjectIdAsync(Guid projectId);
ValueTask InsertCultivateEntryAsync(CultivateEntry entry);
ValueTask InsertCultivateItemRangeAsync(IEnumerable<CultivateItem> toAdd);
void UpdateCultivateItem(CultivateItem item); void UpdateCultivateItem(CultivateItem item);
void UpdateInventoryItem(InventoryItem item); void UpdateInventoryItem(InventoryItem item);