public async Task CreateToy(Toy toy) { await _repository.Create(toy); if (toy.GetType() != typeof(Toy)) { RunCustomLogic(toy); } }
public async Task <IActionResult> Create([FromBody] ToyCreateDTO toyDTO) { var location = GetControllerActionNames(); try { _logger.LogInfo($"{location}: Create Attempted"); if (toyDTO == null) { _logger.LogWarn($"{location}: Empty Request was submitted"); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { _logger.LogWarn($"{location}: Data was Incomplete"); return(BadRequest(ModelState)); } var toy = _mapper.Map <Toy>(toyDTO); var isSuccess = await _toyRepository.Create(toy); if (!isSuccess) { return(internalError($"{location}: Creation failed")); } if (!string.IsNullOrEmpty(toyDTO.File)) { var imgPath = GetImagePath(toyDTO.Image); byte[] imageBytes = Convert.FromBase64String(toyDTO.File); System.IO.File.WriteAllBytes(imgPath, imageBytes); } _logger.LogInfo($"{location}: Creation was successful"); return(Created("Create", new { toy })); } catch (Exception e) { return(internalError($"{location}: {e.Message} - {e.InnerException}")); } }