Esempio n. 1
0
        public virtual async Task EditAsync(Game.EditCommand command, CancellationToken cancellationToken = default)
        {
            var session = command.Session;
            var context = CommandContext.GetCurrent();

            var user = await AuthService.GetUserAsync(session, cancellationToken);

            user = user.MustBeAuthenticated();
            var parsedIntro = command.Intro == null
                ? null
                : await MessageParser.ParseAsync(command.Intro, cancellationToken);

            await using var dbContext = await CreateCommandDbContextAsync(cancellationToken);

            var dbGame = await GetDbGame(dbContext, command.Id, cancellationToken);

            if (command.IsPublic.HasValue)
            {
                dbGame.IsPublic = command.IsPublic.Value;
            }
            if (parsedIntro != null)
            {
                dbGame.Intro = parsedIntro.Format();
            }
            var game = dbGame.ToModel();
            await dbContext.SaveChangesAsync(cancellationToken);

            context.Operation().Items.Set(game);
        }
Esempio n. 2
0
        public virtual async Task Edit(Game.EditCommand command, CancellationToken cancellationToken = default)
        {
            var session = command.Session;
            var context = CommandContext.GetCurrent();

            var user = await AuthService.GetUser(session, cancellationToken);

            user = user.MustBeAuthenticated();
            var parsedIntro = command.Intro == null
                ? null
                : await MessageParser.Parse(command.Intro, cancellationToken);

            await using var dbContext = await CreateCommandDbContext(cancellationToken);

            var dbGame = await GetDbGame(dbContext, command.Id, cancellationToken);

            if (command.IsPublic.HasValue)
            {
                dbGame.IsPublic = command.IsPublic.Value;
            }
            if (command.RoundCount.HasValue)
            {
                if (!dbGame.RoundCount.HasValue)
                {
                    throw new InvalidOperationException("This game doesn't have rounds.");
                }
                var roundCount = command.RoundCount.Value;
                if (roundCount is < 1 or > 100)
                {
                    throw new InvalidOperationException("Round count must be an integer in [1..100] range.");
                }
                dbGame.RoundCount = roundCount;
            }
            if (parsedIntro != null)
            {
                dbGame.Intro = parsedIntro.Format();
            }
            var game = dbGame.ToModel();
            await dbContext.SaveChangesAsync(cancellationToken);

            context.Operation().Items.Set(game);
        }
Esempio n. 3
0
 public Task Edit([FromBody] Game.EditCommand command, CancellationToken cancellationToken = default)
 {
     command.UseDefaultSession(SessionResolver);
     return(Games.Edit(command, cancellationToken));
 }