public async Task <RecipeResponse> SaveAsync(Recipe recipe) { var new_recipe = new Recipe() { Name = recipe.Name, MealId = recipe.MealId, LevelId = recipe.LevelId, CreatorId = recipe.CreatorId, Ingredients = recipe.Ingredients, Directions = recipe.Directions, Photo = recipe.Photo, Time = recipe.Time, //Mark = recipe.Mark, TotalMarks = 0 }; try { await repository.AddAsync(new_recipe); await unitOfWork.CompleteAsync(); return(new RecipeResponse(new_recipe)); } catch (Exception ex) { return(new RecipeResponse($"Error occured while saving recipe: {ex.Message}")); } }
public async Task <IActionResult> Post([FromBody] Recipe model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var response = await _recipeRepo.AddAsync(model); return(Ok(_mapper.Map <RecipeDTO>(response))); }
public async Task <GetRecipeDTO> AddRecipeAsync(SaveRecipeDTO recipeDTO) { var recipes = _mapper.Map <SaveRecipeDTO, Recipe>(recipeDTO); await _recipeRepository.AddAsync(recipes); await _recipeRepository.SaveAsync(); var result = _mapper.Map <SaveRecipeDTO, GetRecipeDTO>(recipeDTO); return(result); }
public async Task <RecipeResponse> SaveAsync(Recipe recipe) { try { if (this.IsExists(recipe)) { return(new RecipeResponse($"Recipe already exists: ", false)); } await _recipeRepository.AddAsync(recipe); await _unitOfWork.CompleteAsync(); return(new RecipeResponse(recipe)); } catch (Exception ex) { return(new RecipeResponse($"An error occurred when saving the recipe: {ex.Message}", false)); } }
public async Task <bool> AddOrUpdateAsync(IEnumerable <RecipeModel> recipes, Guid brewId) { bool result = true; foreach (var recipe in recipes) { Guid id = resourceRepository.GetByName(recipe.ResourceName).ResourceId; if (id == Guid.Empty) { result = false; } else { if (recipe.IsNew) { await recipeRepository.AddAsync(new RecipeModel() { Amount = recipe.Amount, Name = recipe.ResourceName, BrewId = brewId, ResourceId = id, Description = recipe.Description }); } else { var entity = new RecipeModel { Amount = recipe.Amount, Name = recipe.ResourceName, BrewId = brewId, ResourceId = id, Description = recipe.Description, }; recipeRepository.Update(entity); } recipeRepository.SaveAsync(); } } return(result); }
public async Task <RecipeResponse> SaveAsync(Recipe recipe, int userChefId) { var existingUserChef = await _userChefRepository.FindById(userChefId); if (existingUserChef == null) { return(new RecipeResponse("User not found")); } recipe.Author = existingUserChef; try { await _recipeRepository.AddAsync(recipe); await _unitOfWork.CompleteAsync(); return(new RecipeResponse(recipe)); } catch (Exception ex) { return(new RecipeResponse($"An error ocurred while saving the Recipe: {ex.Message}")); } }