public async Task <IActionResult> SaveFocusProduct([FromBody] FocusProductDto focusProductDto) { try { if (ModelState.IsValid) { var result = await _productService.DoSaveFocusProduct(focusProductDto); if (!result) { _logger.Warn($"cadastro de produto Usuario com id {UserId} - Ocorreu um erro ao tenta cadastrar o produto"); return(BadRequest("Ocorreu um erro ao tenta cadastrar o produto")); } return(Ok("Produto cadastrado com sucesso!")); } else { _logger.Warn($"cadastro de produto Usuario com id {UserId} - campos obrigatórios não foram preenchidos"); return(BadRequest(ModelState)); } } catch (Exception ex) { #if (!DEBUG) _logger.Fatal($"cadastro de produto Usuario com id {UserId} - {ex.ToLogString(Environment.StackTrace)}"); #endif return(BadRequest($"cadastro de produto Usuario com id {UserId} - {ex.ToLogString(Environment.StackTrace)}")); } }
public async Task <bool> DoSaveFocusProduct(FocusProductDto focusProductDto) { var focusProduct = _mapper.Map(focusProductDto, new FocusProduct()); focusProduct.CreatedAt = DateTime.Now; focusProduct.Activated = true; focusProduct.GroupProduct = await _groupProductRepository.GetById(focusProductDto.GroupProductId); focusProduct.Product = await _productRepository.GetById(focusProductDto.ProductId); focusProduct.Network = await _networkRepository.GetById(focusProductDto.NetworkId); _focusProductRepository.Save(focusProduct); return(await _unitOfWork.CommitAsync()); }
public async Task <bool> DoUpdateFocusProduct(FocusProductDto focusProductDto) { var focusProduct = await _focusProductRepository.GetById(focusProductDto.Id); if (focusProduct == null) { return(false); } _mapper.Map(focusProductDto, focusProduct); focusProduct.GroupProduct = await _groupProductRepository.GetById(focusProductDto.GroupProductId); focusProduct.Product = await _productRepository.GetById(focusProductDto.ProductId); focusProduct.Network = await _networkRepository.GetById(focusProductDto.NetworkId); return(await _unitOfWork.CommitAsync()); }