Skip to content

Commit

Permalink
- update next item id logic
Browse files Browse the repository at this point in the history
Preventing reserving already reserved id
  • Loading branch information
piotr-shishkov committed Jul 1, 2024
1 parent 6e6327c commit 4a4dcbc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions SteamInventoryServiceTool/SteamInventoryServiceTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<ApplicationIcon>Resources\AppLogo_Small.ico</ApplicationIcon>
<AssemblyVersion>0.1.0.2</AssemblyVersion>
<Version>0.1.0.2</Version>
<AssemblyVersion>0.1.0.3</AssemblyVersion>
<Version>0.1.0.3</Version>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>false</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
34 changes: 19 additions & 15 deletions SteamInventoryServiceTool/Workspaces/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Workspace
public string Name { get; set; }
public Dictionary<string, List<string>> Tags { get; set; } = new();
public List<Item> Items { get; set; } = new();
public int LastCreatedId { get; set; } = 1;

[JsonIgnore]
public string FilePath { get; set; }
Expand Down Expand Up @@ -70,6 +71,8 @@ public void AddItem(Item item)
Items.Remove(existingItem);
}
Items.Add(item);
if(item.Id > LastCreatedId)
LastCreatedId = item.Id;
SortItems();
Update();
Save();
Expand All @@ -93,21 +96,22 @@ private void SortItems()

public int GetNextItemId()
{
if (Items.Count == 0)
return 1;

// Extract IDs and sort them
var ids = Items.Select(item => item.Id).OrderBy(id => id).ToList();

// Check for the first missing ID
for (var i = 0; i < ids.Count; i++)
{
if (ids[i] != i + 1)
return i + 1;
}

// If all IDs are sequential, return the next ID
return ids.Count + 1;
return LastCreatedId + 1;

// Deprecated cause can create item with same id which was another item before
//
// // Extract IDs and sort them
// var ids = Items.Select(item => item.Id).OrderBy(id => id).ToList();
//
// // Check for the first missing ID
// for (var i = 0; i < ids.Count; i++)
// {
// if (ids[i] != i + 1)
// return i + 1;
// }
//
// // If all IDs are sequential, return the next ID
// return ids.Count + 1;
}

public void Save()
Expand Down
3 changes: 3 additions & 0 deletions SteamInventoryServiceTool/Workspaces/WorkspaceManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using SteamInventoryServiceTool.Utility;

namespace SteamInventoryServiceTool.Workspaces;
Expand Down Expand Up @@ -35,6 +36,8 @@ private void SetActiveWorkspace(Workspace workspace)
}
_activeWorkspace = workspace;
_activeWorkspace.Updated += OnActiveWorkspaceUpdated;
var maxItemId = _activeWorkspace.Items.MaxBy(x => x.Id)?.Id ?? 0;
_activeWorkspace.LastCreatedId = Math.Max(_activeWorkspace.LastCreatedId, Math.Max(1, maxItemId));
WorkspaceChanged?.Invoke(_activeWorkspace);
}

Expand Down

0 comments on commit 4a4dcbc

Please sign in to comment.