public async Task <IActionResult> CopyRecipes(AppCopyRecipesDto dto) { Metadata metadata = new Metadata { IsValid = false, Message = "", TotalCount = 0 }; try { var recipes = await _appRecipesServices.CopyRecipes(dto); return(Ok(recipes)); } catch (Exception e) { metadata.IsValid = false; metadata.Message = e.InnerException.Message; var responseError = new ApiResponse <AppDetailQuotesGetDto>(null) { Meta = metadata }; return(Ok(responseError)); } }
public async Task <ApiResponse <List <AppRecipesGetDto> > > CopyRecipes(AppCopyRecipesDto appCopyRecipesDto) { List <AppRecipesGetDto> resultDto = new List <AppRecipesGetDto>(); Metadata metadata = new Metadata { IsValid = true, Message = "" }; ApiResponse <List <AppRecipesGetDto> > response = new ApiResponse <List <AppRecipesGetDto> >(resultDto); try { var productSince = await _unitOfWork.AppProductsRepository.GetById(appCopyRecipesDto.productIdSince); if (productSince == null) { metadata.IsValid = false; metadata.Message = "Producto desde no existe!!"; response.Data = null; response.Meta = metadata; return(response); } var productUntil = await _unitOfWork.AppProductsRepository.GetById(appCopyRecipesDto.productIdUntil); if (productUntil == null) { metadata.IsValid = false; metadata.Message = "Producto hasta no existe!!"; response.Data = null; response.Meta = metadata; return(response); } var recipesUntil = await GetAllRecipesByProductId(productUntil.Id); if (recipesUntil != null && recipesUntil.Count > 0) { metadata.IsValid = false; metadata.Message = "Producto hasta ya tiene una receta asociada!!"; response.Data = null; response.Meta = metadata; return(response); } var recipes = await GetAllRecipesByProductId(productSince.Id); if (recipes == null) { metadata.IsValid = false; metadata.Message = "Producto desdeno tiene una receta asociada!!"; response.Data = null; response.Meta = metadata; return(response); } foreach (var item in recipes) { AppRecipes itemNew = new AppRecipes(); itemNew.AppproductsId = appCopyRecipesDto.productIdUntil; itemNew.AppIngredientsId = item.AppIngredientsId; itemNew.AppVariableId = item.AppVariableId; itemNew.Code = item.Code; itemNew.Description = item.Description; itemNew.Formula = item.Formula; itemNew.FormulaValue = item.FormulaValue; itemNew.IncludeInSearch = item.IncludeInSearch; itemNew.OrderCalculate = item.OrderCalculate; itemNew.Quantity = item.Quantity; itemNew.SumValue = item.SumValue; itemNew.TotalCost = item.TotalCost; itemNew.AfectaCosto = item.AfectaCosto; itemNew.Secuencia = item.Secuencia; var inserted = await Insert(itemNew); } await UpdateVariableSearchByProduct((int)appCopyRecipesDto.productIdUntil); var listRecipeCalculate = await CalulateRecipeByProduct((int)appCopyRecipesDto.productIdUntil); response.Data = listRecipeCalculate; response.Meta = metadata; return(response); } catch (Exception ex) { metadata.IsValid = false; metadata.Message = ex.InnerException.Message; response.Data = null; response.Meta = metadata; return(response); } }