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
2 changes: 2 additions & 0 deletions Backend.Tests/Mocks/MongoDbContextMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public Task<IMongoTransaction> BeginTransaction()

private sealed class MongoTransactionMock : IMongoTransaction
{
public IClientSessionHandle Session => null!;

public Task CommitTransactionAsync()
{
return Task.CompletedTask;
Expand Down
2 changes: 2 additions & 0 deletions Backend/Contexts/MongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private class MongoTransactionWrapper(IClientSessionHandle session) : IMongoTran
{
private readonly IClientSessionHandle _session = session;

public IClientSessionHandle Session => _session;

public Task CommitTransactionAsync()
{
return _session.CommitTransactionAsync();
Expand Down
1 change: 1 addition & 0 deletions Backend/Interfaces/IMongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IMongoDbContext

public interface IMongoTransaction : IDisposable
{
IClientSessionHandle Session { get; }
Task CommitTransactionAsync();
Task AbortTransactionAsync();
}
77 changes: 65 additions & 12 deletions Backend/Repositories/WordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace BackendFramework.Repositories
[ExcludeFromCodeCoverage]
public class WordRepository(IMongoDbContext dbContext) : IWordRepository
{
private readonly IMongoDbContext _dbContext = dbContext;
private readonly IMongoCollection<Word> _frontier = dbContext.Db.GetCollection<Word>("FrontierCollection");
private readonly IMongoCollection<Word> _words = dbContext.Db.GetCollection<Word>("WordsCollection");

Expand Down Expand Up @@ -100,9 +101,19 @@ public async Task<bool> DeleteAllWords(string projectId)
var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.Eq(x => x.ProjectId, projectId);

var deleted = await _words.DeleteManyAsync(filter);
await _frontier.DeleteManyAsync(filter);
return deleted.DeletedCount != 0;
using var transaction = await _dbContext.BeginTransaction();
try
{
var deleted = await _words.DeleteManyAsync(transaction.Session, filter);
await _frontier.DeleteManyAsync(transaction.Session, filter);
await transaction.CommitTransactionAsync();
return deleted.DeletedCount != 0;
}
catch
{
await transaction.AbortTransactionAsync();
throw;
}
}

/// <summary> Removes all <see cref="Word"/>s from the Frontier for specified <see cref="Project"/> </summary>
Expand Down Expand Up @@ -146,8 +157,18 @@ public async Task<Word> Create(Word word)
OtelService.StartActivityWithTag(otelTagName, "creating a word in WordsCollection and Frontier");

PopulateBlankWordTimes(word);
await _words.InsertOneAsync(word);
await AddFrontier(word);
using var transaction = await _dbContext.BeginTransaction();
try
{
await _words.InsertOneAsync(transaction.Session, word);
await _frontier.InsertOneAsync(transaction.Session, word);
await transaction.CommitTransactionAsync();
}
catch
{
await transaction.AbortTransactionAsync();
throw;
}
return word;
}

Expand All @@ -170,8 +191,18 @@ public async Task<List<Word>> Create(List<Word> words)
{
PopulateBlankWordTimes(w);
}
await _words.InsertManyAsync(words);
await AddFrontier(words);
using var transaction = await _dbContext.BeginTransaction();
try
{
await _words.InsertManyAsync(transaction.Session, words);
await _frontier.InsertManyAsync(transaction.Session, words);
await transaction.CommitTransactionAsync();
}
catch
{
await transaction.AbortTransactionAsync();
throw;
}
return words;
}

Expand Down Expand Up @@ -204,9 +235,20 @@ public async Task<Word> CreateAndDeleteFrontier(Word newWord, string oldWordId)
otelTagName, "creating word in WordsCollection and Frontier, deleting old word from Frontier");

PopulateBlankWordTimes(newWord);
await _words.InsertOneAsync(newWord);
await _frontier.InsertOneAsync(newWord);
await _frontier.FindOneAndDeleteAsync(GetProjectWordFilter(newWord.ProjectId, oldWordId));
using var transaction = await _dbContext.BeginTransaction();
try
{
await _words.InsertOneAsync(transaction.Session, newWord);
await _frontier.InsertOneAsync(transaction.Session, newWord);
await _frontier.FindOneAndDeleteAsync(
transaction.Session, GetProjectWordFilter(newWord.ProjectId, oldWordId));
await transaction.CommitTransactionAsync();
}
catch
{
await transaction.AbortTransactionAsync();
throw;
}
return newWord;
}

Expand All @@ -224,8 +266,19 @@ public async Task<Word> AddAndDeleteFrontier(Word deletedWord, string wordId)
otelTagName, "adding word to WordsCollection, deleting word from Frontier");

PopulateBlankWordTimes(deletedWord);
await _words.InsertOneAsync(deletedWord);
await _frontier.FindOneAndDeleteAsync(GetProjectWordFilter(deletedWord.ProjectId, wordId));
using var transaction = await _dbContext.BeginTransaction();
try
{
await _words.InsertOneAsync(transaction.Session, deletedWord);
await _frontier.FindOneAndDeleteAsync(
transaction.Session, GetProjectWordFilter(deletedWord.ProjectId, wordId));
await transaction.CommitTransactionAsync();
}
catch
{
await transaction.AbortTransactionAsync();
throw;
}
return deletedWord;
}

Expand Down