Skip to content

Commit

Permalink
Allow editing recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
cozyGalvinism committed Apr 21, 2022
1 parent 0f0cfd7 commit 7ca5431
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 44 deletions.
12 changes: 6 additions & 6 deletions TGPToolchain/Export/ChangeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public static void Export(string path, string name, string version, string autho
itemsObject.Add("added", addedItemsArray);
itemsObject.Add("changed", changedItemsArray);
itemsObject.Add("deleted", deletedItemsArray);
var addedRecipesArray = LiteDbEntitiesToJson(addedRecipes.DistinctLast(i => i.Id));
var changedRecipesArray = LiteDbEntitiesToJson(changedRecipes.DistinctLast(i => i.Id));
var deletedRecipesArray = JArray.FromObject(deletedRecipes.DistinctLast(i => i));
recipesObject.Add("added", addedRecipesArray);
recipesObject.Add("changed", changedRecipesArray);
recipesObject.Add("deleted", deletedRecipesArray);
// var addedRecipesArray = LiteDbEntitiesToJson(addedRecipes.DistinctLast(i => i.Id));
// var changedRecipesArray = LiteDbEntitiesToJson(changedRecipes.DistinctLast(i => i.Id));
// var deletedRecipesArray = JArray.FromObject(deletedRecipes.DistinctLast(i => i));
// recipesObject.Add("added", addedRecipesArray);
// recipesObject.Add("changed", changedRecipesArray);
// recipesObject.Add("deleted", deletedRecipesArray);

exportObject.Add("name", name);
exportObject.Add("version", version);
Expand Down
4 changes: 3 additions & 1 deletion TGPToolchain/Models/LDBRecipe.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LiteDB;
using System;
using LiteDB;
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable PropertyCanBeMadeInitOnly.Global
#pragma warning disable 8618
Expand All @@ -14,6 +15,7 @@ public enum Methods
}

[BsonId]
[BsonField("_id")]
public ObjectId Id { get; set; }
public LDBItem Result { get; set; }
public string ItemA { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion TGPToolchain/Services/DummyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public bool CreateRecipe(LDBRecipe recipe)
return true;
}

public bool EditRecipe(LDBRecipe recipe)
public bool EditRecipe(LDBRecipe oldRecipe, LDBRecipe recipe)
{
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions TGPToolchain/Services/IDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public interface IDataService
IEnumerable<LDBRecipe> GetRecipes();
IEnumerable<LDBRecipe> FindRecipes(string query);
bool CreateRecipe(LDBRecipe recipe);
bool EditRecipe(LDBRecipe recipe);
bool EditRecipe(LDBRecipe oldRecipe, LDBRecipe recipe);
bool DeleteRecipe(LDBRecipe recipe);
LDBRecipe? GetRecipe(string id);
}
}
42 changes: 11 additions & 31 deletions TGPToolchain/Services/LiteDBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public bool DoesRecipeExist(string itemA, LDBRecipe.Methods method, string itemB

public bool CreateRecipe(LDBRecipe recipe)
{
var exists = recipe.Id != null && _recipeCollection.FindById(recipe.Id) != null ||
DoesRecipeExist(recipe.ItemA, recipe.Method, recipe.ItemB);
var exists = DoesRecipeExist(recipe.ItemA, recipe.Method, recipe.ItemB);
if (exists)
{
_lastError = "A recipe like that already exists!";
Expand All @@ -137,50 +136,31 @@ public bool CreateRecipe(LDBRecipe recipe)
return true;
}

public bool EditRecipe(LDBRecipe recipe)
public bool EditRecipe(LDBRecipe oldRecipe, LDBRecipe recipe)
{
// Recipe has no ID
if (recipe.Id == null)
{
_lastError = "The recipe you're editing has no ID!";
return false;
}
// Find recipe by ID
var existingRecipe = _recipeCollection.FindById(recipe.Id);
// No recipe exists to edit
if (existingRecipe == null)
{
_lastError = "This recipe does not exist!";
return false;
}
// A recipe like this already exists
if (DoesRecipeExist(recipe.ItemA, recipe.Method, recipe.ItemB))
{
_lastError = "A recipe like that already exists!";
return false;
}
_recipeCollection.Update(recipe);
_recipeCollection.UpdateMany(r => new LDBRecipe
{
ItemA = recipe.ItemA,
ItemB = recipe.ItemB,
Method = recipe.Method,
Result = recipe.Result
}, r => r.ItemA == oldRecipe.ItemA && r.ItemB == oldRecipe.ItemB && r.Method == oldRecipe.Method);
return true;
}

public bool DeleteRecipe(LDBRecipe recipe)
{
var existingRecipe = _recipeCollection.FindById(recipe.Id);
if (existingRecipe == null)
{
_lastError = "This recipe does not exist!";
return false;
}
_recipeCollection.Delete(recipe.Id);
_recipeCollection.DeleteMany(r =>
r.ItemA == recipe.ItemA && r.ItemB == recipe.ItemB && r.Method == recipe.Method);
return true;
}

public LDBRecipe? GetRecipe(string id)
{
ObjectId objectId = new(id);
return _recipeCollection.FindById(objectId);
}

public void Dispose()
{
_ldb?.Dispose();
Expand Down
4 changes: 2 additions & 2 deletions TGPToolchain/TGPToolchain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<AssemblyVersion>1.0.6</AssemblyVersion>
<FileVersion>1.0.6</FileVersion>
<AssemblyVersion>1.0.7</AssemblyVersion>
<FileVersion>1.0.7</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion TGPToolchain/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ await MessageBox.MessageBox.ShowDialog(null, _dataService.LastError, "An error o
var editRecipe = new EditRecipeViewModel(SelectedRecipe, _dataService.GetItems());
var result = await EditRecipeDialog.Handle(editRecipe);
if (result == null) return;
var dataServiceResult = _dataService.EditRecipe(result);
var dataServiceResult = _dataService.EditRecipe(SelectedRecipe, result);
if (!dataServiceResult)
await MessageBox.MessageBox.ShowDialog(null, _dataService.LastError, "An error occurred",
MessageBox.MessageBox.MessageBoxButtons.Ok);
Expand Down

0 comments on commit 7ca5431

Please sign in to comment.