// Commands public virtual async Task <Game> CreateAsync(Game.CreateCommand command, CancellationToken cancellationToken = default) { var(session, engineId) = command; var engine = GameEngines[engineId]; // Just to check it exists var context = CommandContext.GetCurrent(); var user = await AuthService.GetUserAsync(session, cancellationToken); user = user.MustBeAuthenticated(); var userId = long.Parse(user.Id); await using var dbContext = await CreateCommandDbContextAsync(cancellationToken); var game = new Game() { Id = Ulid.NewUlid().ToString(), EngineId = engineId, UserId = userId, Intro = "", CreatedAt = Clock.Now, Stage = GameStage.New, Players = ImmutableList <GamePlayer> .Empty.Add(new GamePlayer(userId)) }; var dbGame = new DbGame(); dbGame.UpdateFrom(game); dbContext.Add(dbGame); await dbContext.SaveChangesAsync(cancellationToken); context.Operation().Items.Set(game); return(game); }
public Task <Game> Create([FromBody] Game.CreateCommand command, CancellationToken cancellationToken = default) { command.UseDefaultSession(SessionResolver); return(Games.Create(command, cancellationToken)); }