public GenerateKeysForGameOutput GenerateKeysForGame(GenerateKeysForGameInput input) { Game gameEntity = GameRepository.Get(input.GameId); if (gameEntity == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } if (!GamePolicy.CanGenerateKeysForEntity(gameEntity) || !(input.Count > 0 && input.Count <= CityQuestConsts.MaxCountForKeyGeneration)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionCreateDenied, "\"Key\""); } IList <string> newKeys = KeyGenerationService.Generate(input.Count); IList <Key> newKeyEntities = new List <Key>(input.Count); foreach (string newKey in newKeys) { newKeyEntities.Add(new Key() { KeyValue = newKey, GameId = input.GameId }); } KeyRepository.AddRange(newKeyEntities); return(new GenerateKeysForGameOutput() { Keys = newKeys }); }