Skip to content

Commit

Permalink
Fix BucketGroup creation handling due to changes in DB handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAxelander committed Oct 3, 2023
1 parent c5db397 commit a45d1ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion OpenBudgeteer.Blazor/Pages/Bucket.razor
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@
_newBucketGroupDialogDataContext = new BucketGroup
{
BucketGroupId = Guid.Empty,
Name = string.Empty
Name = string.Empty,
Position = 1
};
_isNewBucketGroupModalDialogVisible = true;
}
Expand Down
2 changes: 1 addition & 1 deletion OpenBudgeteer.Blazor/Shared/NewBucketGroupDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
private void PositionSelectionChanged(ChangeEventArgs eventArgs)
{
var selectedOption = eventArgs.Value as string;
DataContext.Position = selectedOption == "firstPosition" ? 0 : -1;
DataContext.Position = selectedOption == "firstPosition" ? 1 : -1;
}
}
34 changes: 25 additions & 9 deletions OpenBudgeteer.Core/ViewModels/BucketViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,36 @@ public ViewModelOperationResult CreateGroup(BucketGroup newBucketGroup)

// Set Id to 0 to enable creation
newBucketGroup.BucketGroupId = Guid.Empty;

using var dbContext = new DatabaseContext(_dbOptions);
using var transaction = dbContext.Database.BeginTransaction();

// Update positions of existing BucketGroups in case requested position is first
if (newBucketGroup.Position == 1)
{
try
{
var bucketGroups = dbContext.BucketGroup
.Where(i => i.Position > 0)
.ToList();
foreach (var bucketGroup in bucketGroups)
{
bucketGroup.Position++;
}

// Save Position, append Bucket Group and later move it to requested Position
var requestedPosition = newBucketGroup.Position;
newBucketGroup.Position = BucketGroups.Count + 1;
dbContext.UpdateBucketGroups(bucketGroups);
}
catch (Exception e)
{
transaction.Rollback();
return new ViewModelOperationResult(false, $"Unable to move Bucket Groups: {e.Message}");
}
}

using var dbContext = new DatabaseContext(_dbOptions);
if (dbContext.CreateBucketGroup(newBucketGroup) == 0)
return new ViewModelOperationResult(false, "Unable to write changes to database");

var newlyCreatedBucketGroup = dbContext.BucketGroup.OrderBy(i => i.BucketGroupId).Last();
var newBucketGroupViewModelItem = new BucketGroupViewModelItem(_dbOptions, newlyCreatedBucketGroup,
_yearMonthViewModel.CurrentMonth);
newBucketGroupViewModelItem.MoveGroup(requestedPosition - newBucketGroupViewModelItem.BucketGroup.Position);

transaction.Commit();
return new ViewModelOperationResult(true, true);
}

Expand Down

0 comments on commit a45d1ec

Please sign in to comment.