コード例 #1
0
    public async Task DeleteAsync(int id)
    {
        Challenge?challenge = await _context.Challenges.FindAsync(id);

        if (challenge is not null)
        {
            _context.Challenges.Remove(challenge);
            await _context.SaveChangesAsync();
        }
    }
コード例 #2
0
    public async Task <int> CreateAsync(GameCreateOrEditDto dto)
    {
        _ = dto ?? throw new ArgumentNullException(nameof(dto));

        IEnumerable <string> geoGuessrIds = dto.Challenges.Select(c => c.GeoGuessrId);

        if (_context.Challenges.Select(c => c.GeoGuessrId).AsEnumerable().Intersect(geoGuessrIds).Any())
        {
            throw new InvalidOperationException("One or more challenge GeoGuessr ids are already registered.");
        }

        if (dto.Challenges.GroupBy(x => x.GeoGuessrId).Any(g => g.Count() > 1))
        {
            throw new InvalidOperationException("Two or more challenge GeoGuessr ids are the same.");
        }

        IEnumerable <string> mapIds = dto.Challenges.Select(c => c.MapId);

        if (mapIds.Except(_context.Maps.Select(c => c.Id).AsEnumerable()).Any())
        {
            throw new InvalidOperationException("One or more maps do not exists.");
        }

        IEnumerable <string> playerIds = dto.PlayerIds;

        if (playerIds.Except(_context.Players.Select(c => c.Id).AsEnumerable()).Any())
        {
            throw new InvalidOperationException("One or more players do not exists.");
        }

        EntityEntry <Game> game = await _context.Games.AddAsync(new Game
        {
            Name       = dto.Name,
            Date       = dto.Date,
            Challenges = dto.Challenges.Select(x => new Challenge
            {
                GeoGuessrId  = x.GeoGuessrId,
                MapId        = x.MapId,
                PlayerScores = dto.PlayerIds.Select(p => new PlayerScore {
                    PlayerId = p
                }).ToList()
            }).ToList()
        });

        await _context.SaveChangesAsync();

        return(game.Entity.Id);
    }
コード例 #3
0
    public async Task ClearLogsAsync()
    {
        IEnumerable <Log> logsToRemove = _context.Logs.Where(c => EF.Functions.DateDiffDay(c.Timestamp, DateTime.UtcNow) > 15).ToList();

        _context.Logs.RemoveRange(logsToRemove);
        await _context.SaveChangesAsync();
    }
コード例 #4
0
    public async Task <PlayerDto?> UpdateAsync(string id, PlayerEditDto dto)
    {
        _ = dto ?? throw new ArgumentNullException(nameof(dto));

        Player?player = await _context.Players.FindAsync(id);

        if (player is not null)
        {
            player.Name = dto.Name;
            player.AssociatedPlayerId = string.IsNullOrWhiteSpace(dto.AssociatedPlayerId) ? null : dto.AssociatedPlayerId;

            _context.Players.Update(player);
            await _context.SaveChangesAsync();

            _cache.Remove(CacheKeys.PlayerServiceGetAllAsync);
        }

        return(await GetAsync(id));
    }
コード例 #5
0
    public async Task <MapDto?> UpdateAsync(string id, MapEditDto dto)
    {
        _ = dto ?? throw new ArgumentNullException(nameof(dto));

        Map?map = await _context.Maps.FindAsync(id);

        if (map is not null)
        {
            map.Name         = dto.Name;
            map.IsMapForGame = dto.IsMapForGame;

            _context.Maps.Update(map);
            await _context.SaveChangesAsync();

            _cache.Remove(CacheKeys.MapServiceGetAllAsync);
        }

        return(await GetAsync(id));
    }