Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Stack.Tests/Persistence/StackRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ public void SaveChanges_AfterRemovingStack_SavesUpdatedData()
}

[Fact]
public void RemoveStack_RemoteUriComparisonIsCaseInsensitive()
public void SaveChanges_WhenStackIsModified_SavesUpdatedData()
{
// Arrange
var remoteUri = Some.HttpsUri().ToString();
var stack1 = new StackDataItem("Stack1", remoteUri, "main", []);
var stack2 = new StackDataItem("Stack2", remoteUri.ToUpper(), "main", []);
var stack2 = new StackDataItem("Stack2", remoteUri, "main", []);

var dataStore = new MockStackDataStore(new StackData([stack1, stack2]));

Expand All @@ -244,11 +244,16 @@ public void RemoveStack_RemoteUriComparisonIsCaseInsensitive()
var repository = new StackRepository(dataStore, gitClientFactory, executionContext);

// Act
repository.RemoveStack(new Model.Stack("Stack1", "main", []));
var stacks = repository.GetStacks();
var stack1Modified = stacks.First(s => s.Name == "Stack1");
stack1Modified.Branches.Add(new Model.Branch("feature1", []));
repository.SaveChanges();

// Assert
dataStore.Data.Stacks.Should().BeEquivalentTo([stack2]);
dataStore.Data.Stacks.Should().BeEquivalentTo([
new StackDataItem("Stack1", remoteUri, "main", [new StackBranchItem("feature1", [])]),
stack2
]);
}

[Fact]
Expand Down
40 changes: 21 additions & 19 deletions src/Stack/Persistence/StackRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class StackRepository : IStackRepository
private readonly IStackDataStore dataStore;
private readonly IGitClientFactory gitClientFactory;
private readonly CliExecutionContext executionContext;
private readonly Lazy<StackData> stackData;
private readonly Lazy<List<Model.Stack>> allStacks;

public StackRepository(
IStackDataStore dataStore,
Expand All @@ -29,47 +29,49 @@ public StackRepository(
this.dataStore = dataStore;
this.gitClientFactory = gitClientFactory;
this.executionContext = executionContext;
this.stackData = new Lazy<StackData>(() => dataStore.Load());
allStacks = new Lazy<List<Model.Stack>>(() => LoadData());
}

public List<Model.Stack> GetStacks()
{
var remoteUri = GetRemoteUri();

return [.. stackData.Value.Stacks
.Where(s => s.RemoteUri.Equals(remoteUri, StringComparison.OrdinalIgnoreCase))
.Select(s => new Model.Stack(
s.Name,
s.SourceBranch,
[.. s.Branches.Select(b => MapToModelBranch(b))]))];
return allStacks.Value;
}

public void AddStack(Model.Stack stack)
{
var remoteUri = GetRemoteUri();

stackData.Value.Stacks.Add(
new StackDataItem(stack.Name, remoteUri, stack.SourceBranch, [.. stack.Branches.Select(b => MapToDataBranch(b))]));
allStacks.Value.Add(stack);
}

public void RemoveStack(Model.Stack stack)
{
var remoteUri = GetRemoteUri();
var stackToRemove = stackData.Value.Stacks.FirstOrDefault(s =>
s.Name == stack.Name &&
s.RemoteUri.Equals(remoteUri, StringComparison.OrdinalIgnoreCase));
var stackToRemove = allStacks.Value.FirstOrDefault(s => s.Name == stack.Name);

if (stackToRemove == null)
{
throw new InvalidOperationException($"Stack '{stack.Name}' does not exist in the current repository.");
}

stackData.Value.Stacks.Remove(stackToRemove);
allStacks.Value.Remove(stackToRemove);
}

public void SaveChanges()
{
dataStore.Save(stackData.Value);
var remoteUri = GetRemoteUri();
var stackData = dataStore.Load();
stackData.Stacks.RemoveAll(s => s.RemoteUri.Equals(remoteUri, StringComparison.OrdinalIgnoreCase));
stackData.Stacks.AddRange(allStacks.Value.Select(s => new StackDataItem(s.Name, remoteUri, s.SourceBranch, [.. s.Branches.Select(b => MapToDataBranch(b))])));
dataStore.Save(stackData);
}

private List<Model.Stack> LoadData()
{
var remoteUri = GetRemoteUri();

var stackData = dataStore.Load();
return [.. stackData.Stacks
.Where(s => s.RemoteUri.Equals(remoteUri, StringComparison.OrdinalIgnoreCase))
.Select(s => new Model.Stack(s.Name, s.SourceBranch, [.. s.Branches.Select(b => MapToModelBranch(b))]))];
}

private string GetRemoteUri()
Expand Down