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."); }
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(); } }
public async Task <IList <ScoreView> > Get(Guid gameId, int take = 10, int skip = 0) { if (gameId == Guid.Empty) { throw new ArgumentException("Game Id cannot be empty"); } if (take <= 0) { throw new ArgumentException("Take amount must be above 0"); } if (skip < 0) { throw new ArgumentException("Skip amnount must be 0 or above"); } return(await Task.Run(() => { using (var context = new ScoreViewContext(_contextOptions)) { return context.Scores.Where(s => s.GameId == gameId).OrderByDescending(s => s.Score).Skip(skip).Take(take).ToList(); } })); }