Example #1
0
        public async Task <IActionResult> CreateAsync([FromBody] GameConsoleType gameConsoleType)
        {
            if (gameConsoleType == null)
            {
                var ex = new ArgumentException($"{nameof(gameConsoleType)} can't be null");
                _logger.LogError(ex.ToString());
                throw ex;
            }

            if (!_validator.Validate(gameConsoleType).IsValid)
            {
                var ex = new ArgumentException($"{nameof(gameConsoleType)} is not valid");
                _logger.LogError(ex.ToString());
                throw ex;
            }

            try
            {
                _gameConsoleTypeRepository.Add(gameConsoleType);
                await _unitOfWork.SaveChangesAsync();

                return(Ok(gameConsoleType));
            }
            catch
            {
                var ex = new Exception($"Error while adding game console type nameof{nameof(gameConsoleType)}");
                _logger.LogError(ex.ToString());
                throw ex;
            }
        }
Example #2
0
        public async Task <IActionResult> UpdateById(int id, [FromBody] GameConsoleType gameConsoleType)
        {
            if (id == default)
            {
                var ex = new ArgumentException($"{nameof(id)} cannot be 0");
                _logger.LogError(ex.ToString());
                throw ex;
            }

            if (!_validator.Validate(gameConsoleType).IsValid)
            {
                var ex = new ArgumentException($"{nameof(gameConsoleType)} is not valid");
                _logger.LogError(ex.ToString());
                throw ex;
            }

            try
            {
                var currentGameConsoleType = await _gameConsoleTypeRepository.Get(x => x.Id == id);

                if (currentGameConsoleType == null)
                {
                    var ex = new NullReferenceException($"Error while updating game console type. Game console type with {nameof(id)}={id} not found");
                    _logger.LogError(ex.ToString());
                    throw ex;
                }

                currentGameConsoleType.Name = gameConsoleType.Name;

                _gameConsoleTypeRepository.Update(currentGameConsoleType);
                await _unitOfWork.SaveChangesAsync();

                return(Ok(currentGameConsoleType));
            }
            catch (Exception ex)
            {
                ex.Data["id"] = id;
                _logger.LogError(ex.ToString());
                throw ex;
            }
        }