public async Task <GamesView> Create(ShortGame shortGame) { try { Game gameExist = FindByUsersAssociation(shortGame.Player1Id, (int)shortGame.Player2Id); if (gameExist != null) { throw new Exception("You are already playing against this user, please end this game first."); } Game game = new Game() { GameTypeId = shortGame.GameTypeId, GameStateId = (int)GameStateEnum.WaitingAcceptance, Player1Id = shortGame.Player1Id, Player2Id = shortGame.Player2Id, UserIdTurn = (int)shortGame.Player2Id }; if (shortGame.GameTypeId == (int)GameTypeEnum.Cards) { throw new NotImplementedException(); } await _wargameContext.AddAsync(game); await _wargameContext.SaveChangesAsync(); return(_wargameContext.GamesView.SingleOrDefault(x => x.Player1Id == shortGame.Player1Id && x.Player2Id == shortGame.Player2Id)); } catch (Exception e) { throw new Exception("Error in friend's request : " + e.Message); } }
/// <summary> /// Création d'un nouvel utilisateur. /// </summary> /// <param name="utilisateur">L'objet utilisateur.</param> /// <returns>Retourne un objet Utilisateur.</returns> public async Task <User> Create(ShortUser user) { try { if (_wargameContext.User.SingleOrDefault(x => x.Email == user.Email) != null) { throw new Exception("Email already exist. Try with another or login with your nickname."); } if (_wargameContext.User.SingleOrDefault(x => x.Nickname == user.Nickname) != null) { throw new Exception("Nickname already exist. Choose another one or login with your account."); } // Create user. User newUser = new User() { Email = user.Email, Nickname = user.Nickname, Password = GenericService.EncryptText(user.Password, "SHA1"), Points = 0, }; // Create user. await _wargameContext.AddAsync(newUser); await _wargameContext.SaveChangesAsync(); return(newUser); } catch (Exception e) { throw new Exception(e.Message); } }
public async Task <GamesView> Create(List <Position> positions) { try { foreach (Position position in positions) { await _wargameContext.AddAsync(position); } await _wargameContext.SaveChangesAsync(); return(await _gameService.updatePosition(positions[0])); } catch (Exception e) { throw new Exception("Error in positions : " + e.Message); } }
public async Task <FriendsView> Create(ShortFriend friend) { try { Friends friendShip = new Friends() { UserId1 = friend.Id1, UserId2 = friend.Id2, Indemand = true }; await _wargameContext.AddAsync(friendShip); await _wargameContext.SaveChangesAsync(); return(_wargameContext.FriendsView.SingleOrDefault(x => x.Id1 == friend.Id1 && x.Id2 == friend.Id2)); } catch (Exception e) { throw new Exception("Erreur lors de la création de la demande : " + e.Message); } }
public async Task <bool> Create(Message message) { try { await _wargameContext.AddAsync(message); await _wargameContext.SaveChangesAsync(); return(true); } catch (Exception e) { throw new Exception("Error when creating message : " + e.Message); } }