コード例 #1
0
        public async Task <Unit> Handle(EditGameCommand request, CancellationToken cancellationToken)
        {
            var(id, title, maxPlayers, minPlayers, _) = request;

            _logger.LogInformation($"Getting game {request.Title} with id {request.Id.ToString()}");

            var game = await _gamesService.TryGetGameAsync(request.Id, cancellationToken);

            var isTitleUnique = await _gamesService.IsTitleUniqueAsync(title, id, cancellationToken);

            _logger.LogInformation($"Checking if {request.Title} is unique");

            if (!isTitleUnique)
            {
                throw new EntityExistsException(nameof(Game), id);
            }

            _logger.LogInformation("Updating game...");
            game.Title      = title;
            game.MaxPlayers = maxPlayers;
            game.MinPlayers = minPlayers;

            _context.Games.Update(game);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #2
0
        public async Task <Guid> Handle(AddGameCommand request, CancellationToken cancellationToken)
        {
            var isTitleUnique = await _gamesService.IsTitleUniqueAsync(request.Title, null, cancellationToken);

            if (!isTitleUnique)
            {
                _logger.LogError($"Game with title {request.Title} already exists");
                throw new EntityExistsException(nameof(Game), request.Title);
            }

            var id = _idGenerator.NewGuid();

            var game = new Game
            {
                Id         = id,
                Title      = request.Title,
                MaxPlayers = request.MaxPlayers,
                MinPlayers = request.MinPlayers,
                Added      = _dateTimeService.UtcNow
            };

            _logger.LogInformation($"Adding game with id {id.ToString()} title {request.Title}");

            await _context.Games.AddAsync(game, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(id);
        }
コード例 #3
0
        public async Task <Unit> Handle(DeleteGameCommand request, CancellationToken cancellationToken)
        {
            var game = await _context.Games.FirstOrDefaultAsync(g => g.Id == request.Id, cancellationToken);

            if (game == null)
            {
                _logger.LogError($"Game with id {request.Id} was not found");
                throw new NotFoundException(nameof(Game), request.Id);
            }

            _logger.LogInformation($"Deleting game with id {request.Id.ToString()}");

            _context.Games.Remove(game);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }