Пример #1
0
        public async Task <bool> AddAnArmyAsync(Army request)
        {
            _logger.LogInformation($"Attempting to add an army with name: {request.Name} to the database...");
            var battle = await _battleRepository.GetInitializingBattleAsync();

            request.BattleId = battle?.Id ?? 0;

            if (battle is null)
            {
                request.BattleId = await _battleRepository.CreateBattleAsync();
            }

            if (battle?.Armies?.Count(x => x.BattleId == request.BattleId && x.Name == request.Name) > 0)
            {
                throw new Exception($"An army with the name {request.Name} already exist for the current battle. Please choose another army name.");
            }

            await _trackingContext.AddAsync(request);

            var result = await _trackingContext.SaveChangesAsync();

            if (result > 0)
            {
                _logger.LogInformation($"Adding an army with name: {request.Name} successful...");
                return(true);
            }
            else
            {
                _logger.LogError($"Adding an army with name: {request.Name} failed!");
                return(false);
            }
        }