public async Task SetUp() { _gameId1 = Guid.NewGuid(); _gameId2 = Guid.NewGuid(); _connection = new SqliteConnection("DataSource=:memory:"); _contextOptions = new DbContextOptionsBuilder().UseSqlite(_connection).Options; _service = new ScoreSqlQueryService(_contextOptions); _scores = new[] { BuildScore(_gameId1, 5, "Test User"), BuildScore(_gameId2, 25, "Another User"), BuildScore(_gameId2, 100, "Test User"), BuildScore(_gameId1, 26, "Third User"), BuildScore(_gameId1, 50, "Forth User"), }; await _connection.OpenAsync(); using (var context = new ScoreViewContext(_contextOptions)) { await context.Database.EnsureCreatedAsync(); await context.Scores.AddRangeAsync(_scores); await context.SaveChangesAsync(); } }
private async Task HandleSaveScore(byte[] data) { var view = new ScoreView(); using (var memStream = new MemoryStream(data)) { var binReader = new BinaryReader(memStream); view.FromString(binReader.ReadString()); } if (view == new ScoreView()) { _logger.LogError($"Score was not formatted correctly."); return; } using (var context = new ScoreViewContext(_contextOptions)) using (var transaction = await context.Database.BeginTransactionAsync()) { await context.Scores.AddAsync(view); await context.SaveChangesAsync(); transaction.Commit(); context.Dispose(); } _logger.LogDebug($"Score {view.Id} submitted successfully."); }