[#43] BaseDataWindow: Do not use Context from ContextWindow any more
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models.Entities;
|
||||
using LinqKit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
@ -18,19 +19,19 @@ namespace Elwig.Windows {
|
||||
private bool _modChanged = false;
|
||||
private bool _modUpdate = false;
|
||||
|
||||
private void ModifiersInitEditing() {
|
||||
private async Task ModifiersInitEditing(AppDbContext ctx) {
|
||||
SeasonList.IsEnabled = false;
|
||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||
_modList = new(Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToList());
|
||||
_modList = new(await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync());
|
||||
_mods = _modList.ToDictionary(m => m.ModId, m => (string?)m.ModId);
|
||||
_modIds = _modList.ToDictionary(m => m, m => m.ModId);
|
||||
ControlUtils.RenewItemsSource(SeasonModifierList, _modList, m => (m as Modifier)?.ModId);
|
||||
SeasonModifierList_SelectionChanged(null, null);
|
||||
}
|
||||
|
||||
private void ModifiersFinishEditing() {
|
||||
private async Task ModifiersFinishEditing(AppDbContext ctx) {
|
||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||
ControlUtils.RenewItemsSource(SeasonModifierList, Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToList(), m => (m as Modifier)?.ModId);
|
||||
ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync(), m => (m as Modifier)?.ModId);
|
||||
_modList = null;
|
||||
_mods = null;
|
||||
_modIds = null;
|
||||
@ -43,7 +44,7 @@ namespace Elwig.Windows {
|
||||
SeasonList.IsEnabled = true;
|
||||
}
|
||||
|
||||
private async Task ModifiersSave() {
|
||||
private async Task ModifiersSave(AppDbContext ctx) {
|
||||
if (!_modChanged || _modList == null || _mods == null || _modIds == null)
|
||||
return;
|
||||
|
||||
@ -52,26 +53,26 @@ namespace Elwig.Windows {
|
||||
|
||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||
foreach (var (modid, _) in _mods.Where(m => m.Value == null)) {
|
||||
Context.Remove(Context.Modifiers.Find(year, modid)!);
|
||||
ctx.Remove(ctx.Modifiers.Find(year, modid)!);
|
||||
}
|
||||
foreach (var (mod, old) in _modIds) {
|
||||
mod.ModId = old;
|
||||
}
|
||||
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
|
||||
Context.Update(Context.Modifiers.Find(year, old)!);
|
||||
ctx.Update(ctx.Modifiers.Find(year, old)!);
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
|
||||
await Context.Database.ExecuteSqlAsync($"UPDATE modifier SET modid = {modid} WHERE (year, modid) = ({year}, {old})");
|
||||
await ctx.Database.ExecuteSqlAsync($"UPDATE modifier SET modid = {modid} WHERE (year, modid) = ({year}, {old})");
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
foreach (var mod in _modList.Where(m => !_modIds.ContainsKey(m))) {
|
||||
if (mod.ModId == null) continue;
|
||||
await Context.AddAsync(mod);
|
||||
await ctx.AddAsync(mod);
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private void SeasonModifierUpButton_Click(object sender, RoutedEventArgs evt) {
|
||||
@ -102,11 +103,7 @@ namespace Elwig.Windows {
|
||||
if (_modList == null || SeasonList.SelectedItem is not Season s) return;
|
||||
_modChanged = true;
|
||||
var idx = (SeasonModifierList.SelectedIndex != -1) ? SeasonModifierList.SelectedIndex + 1 : _modList.Count;
|
||||
var item = new Modifier {
|
||||
Year = s.Year,
|
||||
ModId = "",
|
||||
Name = "",
|
||||
};
|
||||
var item = new Modifier { Year = s.Year, ModId = "", Name = "" };
|
||||
_modList.Insert(idx, item);
|
||||
SeasonModifierList.SelectedIndex = idx;
|
||||
UpdateButtons();
|
||||
|
Reference in New Issue
Block a user