public async Task AddUserAsync(string connectionId, string subId, string userName) { if (connectionId == null) { throw new ArgumentNullException(nameof(connectionId)); } if (subId == null) { throw new ArgumentNullException(nameof(subId)); } var user = await _context.Users.FirstOrDefaultAsync(u => u.UserSubId == subId); if (user == null) { await _context.Users.AddAsync(new User { ConnectionId = connectionId, UserSubId = subId, Name = String.IsNullOrWhiteSpace(userName) ? connectionId : userName }); } else { user.ConnectionId = connectionId; user.Name = String.IsNullOrWhiteSpace(userName) ? connectionId : userName; } await _context.SaveChangesAsync(); }
public async System.Threading.Tasks.Task DeletePlayerTestAsync() { DbContextOptions <GamesDbContext> options = new DbContextOptionsBuilder <GamesDbContext>().UseInMemoryDatabase("DeletePlayer").Options; using (GamesDbContext context = new GamesDbContext(options)) { Player newPlayer = new Player() { ID = "1", RoomID = "1", Name = "Farkus", Score = 500, Toad = true }; context.Add(newPlayer); await context.SaveChangesAsync(); Player foundPlayer = await context.Players.FirstOrDefaultAsync(player => player.ID == "1"); foundPlayer.Score = 250; context.Remove(foundPlayer); await context.SaveChangesAsync(); Player deletedPlayer = await context.Players.FirstOrDefaultAsync(player => player.ID == "1"); Assert.True(deletedPlayer == null); } }
public async Task <IActionResult> Delete(int id) { _logger.LogDebug($"Deleting game with id {id}"); var game = await _dbContext.Games.FindAsync(id); if (game == null) { return(NotFound()); } _dbContext.Games.Remove(game); await _dbContext.SaveChangesAsync(); return(Ok()); }
public async Task Create([FromBody] CreateGameInput input) { var entity = _mapper.Map <Game>(input); await _gamesDbContext.Games.AddAsync(entity); await _gamesDbContext.SaveChangesAsync(); }
// TODO: Split in two Methods public async Task AddUserAsync(User client) { // await using var tx = await _context.Database.BeginTransactionAsync(); var session = await _context.Sessions .Include(s => s.UserOne) .Include(s => s.UserTwo) .Where(s => s.UserTwo == null && s.UserOne != null) .FirstOrDefaultAsync(s => s.UserOne.Id != client.Id); if (session != null) { Console.WriteLine( $"Session found which is open and not started. {JsonSerializer.Serialize(session)}"); session.UserTwo = client; session.ActiveUser = session.UserOne.ConnectionId; } else { session = new GameSession { SessionId = $"Game{_context.Sessions.Count()}", UserOne = client }; await _context.Sessions.AddAsync(session); } await _context.SaveChangesAsync(); // await tx.CommitAsync(); await _hubContext.Groups.AddToGroupAsync(client.ConnectionId, session.SessionId); if (!String.IsNullOrWhiteSpace(session.ActiveUser)) { Console.WriteLine( $"New Game will start now. Session: {session.SessionId}, User One: {session.UserOne}, User Two: {session.UserTwo}, Active User: {session.ActiveUser}"); await _hubContext.Clients.Group(session.SessionId).SendAsync("StartGame", session); } }
public async Task PostUser([FromBody] User user) { using (var dbContext = new GamesDbContext()) { var currentUser = await dbContext.User.Where(x => x.Email == user.Email).FirstOrDefaultAsync(); if (currentUser == null) { user.Id = Guid.NewGuid(); await dbContext.Add(user); } else { currentUser.Email = user.Email; currentUser.Name = user.Name; await dbContext.Edit(currentUser); } await dbContext.SaveChangesAsync(); } }
public async Task <IHttpActionResult> PostUser([FromBody] User user, [FromUri] int bestScore = 0) { Guid userId; using (var dbContext = new GamesDbContext()) { var currentUser = await dbContext.User.Where(x => x.Email == user.Email).FirstOrDefaultAsync(); if (currentUser == null) { userId = Guid.NewGuid(); user.Id = userId; await dbContext.Add(user); } else { currentUser.Email = user.Email; currentUser.Name = user.Name; userId = currentUser.Id; await dbContext.Edit(currentUser); } if (bestScore > 0) { var currentGameScore = await dbContext.Score.Where(x => x.UserId == userId && x.ScorePoint == bestScore).FirstOrDefaultAsync(); if (currentGameScore == null) { await dbContext.Add(new Score { UserId = userId, GameId = 1, Id = Guid.NewGuid(), ScorePoint = bestScore }); } } await dbContext.SaveChangesAsync(); return(Ok()); } }
/// <summary> /// Saves a new instance of player to the database /// </summary> /// <param name="player">The player to be added</param> /// <returns>Nothing, creates new player object in database</returns> public async Task CreatePlayer(Player player) { _context.Players.Add(player); await _context.SaveChangesAsync(); }