public List <RecipeCuisine> MapToRecipeCuisines(RecipeRequest recipeRequest) { var cuisinesRecipeCuisines = new List <RecipeCuisine>(); foreach (var cuisines in recipeRequest.Cuisine) { if (!String.IsNullOrWhiteSpace(cuisines)) { var recipeCuisine = new RecipeCuisine(); { if (CheckCuisine(cuisines)) { var cuisine = GetCuisineId(cuisines); recipeCuisine.Cuisine = cuisine; recipeCuisine.CuisineId = cuisine.CuisineId; } else { var mapToCuisine = MapToCuisine(cuisines); recipeCuisine.CuisineId = mapToCuisine.CuisineId; recipeCuisine.Cuisine = mapToCuisine; } } cuisinesRecipeCuisines.Add(recipeCuisine); } } return(cuisinesRecipeCuisines); }
public IHttpActionResult CreateRecipe([FromBody] RecipeRequest recipe) { try { if (recipe == null) { return(BadRequest()); } if (recipe.FoodName.IsNullOrWhiteSpace()) { return(BadRequest()); } var recipeId = _recipeService.CreateRecipe(recipe); return(Ok(new Response { RecipeId = recipeId })); } catch (ArgumentException exception) { return(BadRequest()); } catch (Exception exception) { return(BadRequest()); } }
public List <RecipeMealType> MapToRecipeCuisines(RecipeRequest recipeRequest) { var recipeMealTypes = new List <RecipeMealType>(); foreach (var mealTypes in recipeRequest.MealType) { if (!String.IsNullOrWhiteSpace(mealTypes)) { var recipeMealType = new RecipeMealType(); { if (CheckMealType(mealTypes)) { var mealtype = GetMealTypeId(mealTypes); recipeMealType.MealType = mealtype; recipeMealType.MealTypeId = mealtype.MealTypeId; } else { var mapToMealType = MapToMealType(mealTypes); recipeMealType.MealType = mapToMealType; recipeMealType.MealTypeId = mapToMealType.MealTypeId; } } recipeMealTypes.Add(recipeMealType); } } return(recipeMealTypes); }
public List <RecipeCookingStyle> MapToRecipeCookingStyles(RecipeRequest recipeRequest) { var recipeCookingStyle = new List <RecipeCookingStyle>(); foreach (var cookingStyleName in recipeRequest.CookingStyle) { if (!String.IsNullOrWhiteSpace(cookingStyleName)) { var cookingStyle = new RecipeCookingStyle(); { if (CheckMealType(cookingStyleName)) { var cookingstyle = GetMealTypeId(cookingStyleName); cookingStyle.CookingStyle = cookingstyle; cookingStyle.CookingStyleId = cookingstyle.CookingStyleId; } else { var mapToMealType = MapToCookigStyle(cookingStyleName); cookingStyle.CookingStyle = mapToMealType; cookingStyle.CookingStyleId = mapToMealType.CookingStyleId; } } recipeCookingStyle.Add(cookingStyle); } } return(recipeCookingStyle); }
public List <RecipeDishType> MapToRecipeDishType(RecipeRequest recipeRequest) { var dishTypes = new List <RecipeDishType>(); foreach (var dishtype in recipeRequest.DishType) { if (!String.IsNullOrWhiteSpace(dishtype)) { var recipeDishType = new RecipeDishType(); { if (CheckDishType(dishtype)) { var recipeDishtype = GetDishTypeId(dishtype); recipeDishType.DishType = recipeDishtype; recipeDishType.DishTypeId = recipeDishtype.DishTypeId; } else { var mapToDishType = MapToDishType(dishtype); recipeDishType.DishTypeId = mapToDishType.DishTypeId; recipeDishType.DishType = mapToDishType; } } dishTypes.Add(recipeDishType); } } return(dishTypes); }
public List <RecipeIngredient> MapToRecipeIngredient(RecipeRequest recipeRequest) { var recipeIngredients = new List <RecipeIngredient>(); foreach (var ingredient in recipeRequest.Ingredients) { if (ingredient.IngredientName != null && ingredient.Quantity != null) { var recipeIngredient = new RecipeIngredient(); { recipeIngredient.Quantity = ingredient.Quantity; recipeIngredient.Unit = ingredient.Unit; recipeIngredient.MainIngredient = ingredient.MainIngredient; if (CheckIngredient(ingredient.IngredientName)) { var dbIngredient = GetIngredientId(ingredient.IngredientName); recipeIngredient.Ingredient = dbIngredient; } else { var ingredients = MapToIngredient(ingredient.IngredientName); if (ingredients != null) { recipeIngredient.IngredientId = ingredient.IngredientId; } recipeIngredient.Ingredient = ingredients; } } recipeIngredients.Add(recipeIngredient); } } return(recipeIngredients); }
public async Task <ActionResult <Response <RecipeResponse> > > AddRecipe([FromBody] RecipeRequest recipeRequest) { var token = HttpContext.Request.Headers["Authorization"].FirstOrDefault(); var userToken = token.Split(' ')[1]; var user = UserToken.FromToken(userToken); return(Ok(await _recipeService.AddRecipe(recipeRequest, user.UserId))); }
public IActionResult Create([FromBody] RecipeRequest request) { var entity = Mapper.Map <Recipe>(request); Repository.Recipe.Create(entity); Repository.Save(); Cache.Remove(CacheKey.CategoriesWithRecipes); return(CreatedAtAction(nameof(Recipes), new { id = entity.RecipeId }, Mapper.Map <RecipeResponse>(entity))); }
public async Task Update_WithIncorrectData_ShouldReturnBadRequest(RecipeRequest request) { //Arrange await AuthenticateAsync(); //Act var response = await _testClient.PutAsJsonAsync("/api/recipes/1", request); //Assert response.StatusCode.Should().Be(HttpStatusCode.BadRequest); }
public async Task <Response <RecipeResponse> > AddRecipe(RecipeRequest recipeRequest, Guid userId) { var amount = new RecipeAmount() { AmountTypeId = await GetAmountTypeId(recipeRequest.Amount.Type), Min = recipeRequest.Amount.Min, Max = recipeRequest.Amount.Max }; await _context.AddAsync(amount); await _context.SaveChangesAsync(); var recipe = new Recipe() { Description = recipeRequest.Description, Name = recipeRequest.Name, Private = recipeRequest.Private, RecipeAmountId = amount.Id, UserId = userId, VideoId = recipeRequest.VideoId }; await _context.AddAsync(recipe); await _context.SaveChangesAsync(); if (recipeRequest.Ingredients != null) { var ingredients = recipeRequest.Ingredients.Select(x => new Ingredient() { Amount = x.Amount, Name = x.Name, RecipeId = recipe.Id, Unit = x.Unit }); await _context.AddRangeAsync(ingredients); } if (recipeRequest.Links != null) { var links = recipeRequest.Links.Select(x => new Link() { RecipeId = recipe.Id, Url = x.Url }); await _context.AddRangeAsync(links); } await _context.SaveChangesAsync(); return(new Response <RecipeResponse>() { Data = _mapper.Convert(recipe, false, userId), Success = true, }); //var ingredients = recipeResponse.Ingredients.Select(x => new Ingredient() { RecipeId = recipeResponse }) }
private FoodRecipe GetMappingToFoodRecipe(RecipeRequest recipeRequest) { var recipe = new FoodRecipe { FoodName = recipeRequest.FoodName, PrepTime = recipeRequest.PrepTime, ReadyIn = recipeRequest.ReadyIn, CookingTime = recipeRequest.CookingTime, CreatedBy = recipeRequest.CreatedBy }; return(recipe); }
public async Task <Response <RecipeResponse> > GetRecipesByIngredientsAsync( string urlBase, string servicePrefix, string controller, string tokenType, string accessToken, List <string> ingredients) { try { var request = new RecipeRequest { Ingredients = ingredients }; var requestString = JsonConvert.SerializeObject(request); var content = new StringContent(requestString, Encoding.UTF8, "application/json"); var client = new HttpClient { BaseAddress = new Uri(urlBase) }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken); var url = $"{servicePrefix}{controller}"; var response = await client.PostAsync(url, content); var result = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(new Response <RecipeResponse> { IsSuccess = false, Message = result, }); } var recipes = JsonConvert.DeserializeObject <List <RecipeResponse> >(result); return(new Response <RecipeResponse> { IsSuccess = true, ResultList = recipes }); } catch (Exception ex) { return(new Response <RecipeResponse> { IsSuccess = false, Message = ex.Message }); } }
public FoodRecipe GetMappingToFoodRecipe(RecipeRequest recipeRequest) { var recipe = new FoodRecipe { FoodName = recipeRequest.FoodName, PrepTime = recipeRequest.PrepTime, ReadyIn = recipeRequest.ReadyIn, CookingTime = recipeRequest.CookingTime, CreatedBy = recipeRequest.CreatedBy, CreatedOn = DateTime.Now }; return(recipe); }
public IActionResult Create([FromBody] RecipeRequest request) { var recipe = Mapper.Map <FoodDetails>(request); recipe.UserId = CurrentUserId; recipe.IsRecipe = true; CalculateNutrients(recipe); CalculatePortionWeights(recipe); nutritionRepository.CreateFood(recipe); var result = Mapper.Map <RecipeDetailsResponse>(recipe); return(Ok(result)); }
public List <RecipeDirection> MapToRecipeDirections(RecipeRequest recipeRequest) { List <RecipeDirection> recipeDirections = new List <RecipeDirection>(); foreach (var direction in recipeRequest.Directions) { var recipeDirection = new RecipeDirection { Instructions = direction.Instruction, StepNo = direction.StepNo }; recipeDirections.Add(recipeDirection); } return(recipeDirections); }
public async Task Update(int id, RecipeRequest request) { var recipe = await _recipesRepository.GetWithRecipeIngredients(id); if (recipe is null) { throw new NotFoundException("Recipe not found"); } var authorizationResult = await _authorizationService.AuthorizeAsync(_userContextService.User, recipe, new RecipeOperationRequirement(ResourceOperation.Update)); if (!authorizationResult.Succeeded) { throw new ForbidException(); } var newRecipeIngredients = request.Ingredients.Select(x => new RecipeIngredient { IngredientId = x.IngredientId, Measure = x.Measure, RecipeId = id }).ToList(); foreach (var recipeIngredient in recipe.RecipeIngredients.ToList()) { if (!newRecipeIngredients.Contains(recipeIngredient)) { recipe.RecipeIngredients.Remove(recipeIngredient); } } foreach (var newRecipeIngredient in newRecipeIngredients) { if (!recipe.RecipeIngredients.Any(x => x.Equals(newRecipeIngredient))) { recipe.RecipeIngredients.Add(newRecipeIngredient); } } recipe.AreaId = request.AreaId; recipe.CategoryId = request.CategoryId; recipe.Instructions = request.Instructions; recipe.Name = request.Name; recipe.Youtube = recipe.Youtube; recipe.Source = recipe.Source; recipe.UpdatedAt = DateTime.UtcNow; await _recipesRepository.Update(recipe); }
public int AddFoodRecipe(RecipeRequest recipeRequest) { var foodrecipeMap = GetMappingToFoodRecipe(recipeRequest); _recipeContext.FoodRecipes.Add(foodrecipeMap); var recipeIngredients = MapToRecipeIngredient(recipeRequest); if (recipeIngredients != null) { foodrecipeMap.RecipeIngredients.AddRange(recipeIngredients); } _recipeContext.SaveChanges(); return(foodrecipeMap.RecipeId); }
public async Task <int> Create(RecipeRequest request) { var userId = _userContextService.GetUserId; if (userId is null) { throw new ForbidException(); } var newRecipe = _mapper.Map <Recipe>(request); newRecipe.UserId = userId; await _recipesRepository.Add(newRecipe); return(newRecipe.Id); }
public int CreateRecipe(RecipeRequest recipeRequest) { var foodRecipe = new MapFoodRecipe(); var foodrecipeMap = foodRecipe.GetMappingToFoodRecipe(recipeRequest); _recipeContext.FoodRecipes.Add(foodrecipeMap); var map = new MapIngredients(_recipeContext); var recipeIngredients = map.MapToRecipeIngredient(recipeRequest); if (recipeIngredients != null) { foodrecipeMap.RecipeIngredients.AddRange(recipeIngredients); } var mapdirection = new MapDirections(); var directions = mapdirection.MapToRecipeDirections(recipeRequest); _recipeContext.RecipeDirection.AddRange(directions); var mapCuisine = new MapRecipeCuisine(_recipeContext); var recipeCuisine = mapCuisine.MapToRecipeCuisines(recipeRequest); _recipeContext.RecipeCuisine.AddRange(recipeCuisine); var mapMealtype = new MapMealType(_recipeContext); var recipeMealType = mapMealtype.MapToRecipeCuisines(recipeRequest); _recipeContext.RecipeMealType.AddRange(recipeMealType); var mapDishType = new MapDishType(_recipeContext); var recipeDishType = mapDishType.MapToRecipeDishType(recipeRequest); _recipeContext.RecipeDishType.AddRange(recipeDishType); var mapCookingStyle = new MapCookingStyle(_recipeContext); var recipeCookingStyle = mapCookingStyle.MapToRecipeCookingStyles(recipeRequest); _recipeContext.RecipeCookingStyle.AddRange(recipeCookingStyle); _recipeContext.SaveChanges(); return(foodrecipeMap.RecipeId); }
public IActionResult Update(Guid id, [FromBody] RecipeRequest request) { var recipe = nutritionRepository.GetFood(id); if (!recipe.IsRecipe) { return(NotFound()); } if (recipe.UserId != CurrentUserId) { return(Unauthorized()); } Mapper.Map(request, recipe); CalculateNutrients(recipe); CalculatePortionWeights(recipe); nutritionRepository.UpdateFood(recipe); var result = Mapper.Map <RecipeDetailsResponse>(recipe); return(Ok(result)); }
public RecipeRequest MapToRecipe(int recipeId) { var foodrecipe = GetFoodRecipe(recipeId); var recipe = new RecipeRequest { FoodName = foodrecipe.FoodName, PrepTime = foodrecipe.PrepTime, ReadyIn = foodrecipe.ReadyIn, CookingTime = foodrecipe.CookingTime, CreatedBy = foodrecipe.CreatedBy, Ingredients = GetIngredients(recipeId), MainIngredient = GetMainIngredient(recipeId), Directions = GetDirection(recipeId), MealType = GetMealType(recipeId), DishType = GetDishType(recipeId), CookingStyle = GetCookingStyle(recipeId), Cuisine = GetCuisine(recipeId) }; return(recipe); }
private List <RecipeIngredient> MapToRecipeIngredient(RecipeRequest recipeRequest) { var recipeIngredients = new List <RecipeIngredient>(); foreach (var ingredient in recipeRequest.Ingredients) { var recipeIngredient = new RecipeIngredient(); { if (ingredient.IngredientName != null && ingredient.Quantity != null) { recipeIngredient.Quantity = ingredient.Quantity; } recipeIngredient.Unit = ingredient.Unit; recipeIngredient.MainIngredient = ingredient.MainIngredient; if (CheckIngredient(ingredient.IngredientName)) { var intgr = GetIngredientId(ingredient.IngredientName); recipeIngredient.IngredientId = intgr.IngredientId; recipeIngredient.Ingredient = new Ingredient(); //{ // IngredientName = intgr.IngredientName, // IngredientId = intgr.IngredientId //}; } else { var ingredients = MapToIngredient(ingredient.IngredientName); if (ingredients != null) { recipeIngredient.IngredientId = ingredients.IngredientId; } } } recipeIngredients.Add(recipeIngredient); } return(recipeIngredients); }
public async Task <IActionResult> UpdateAsync([FromBody] RecipeRequest request) { var entity = await Repository.Recipe .GetRecipeById(request.RecipeId) .FirstOrDefaultAsync() .ConfigureAwait(false); if (entity == null) { return(NotFound(request)); } else { entity = Mapper.Map <Recipe>(request); } Repository.Recipe.Update(entity); Repository.Save(); Cache.Remove(CacheKey.CategoriesWithRecipes); return(Ok(entity)); }
public ActionResult CreateRecipe([FromBody] RecipeRequest recipe) { if (!ModelState.IsValid) { return(BadRequest("Model State is Not Valid!!")); } Category category = _categoryService.GetById(recipe.CategoryId); Recipe newRecipe = new Recipe { RecipeName = recipe.RecipeName, Category = category, Description = recipe.Description, CookTime = recipe.CookTime, PrepareTime = recipe.PrepareTime, Steps = recipe.Steps }; _recipeService.Add(newRecipe); return(Ok(newRecipe)); }
//IngredientsRequest ingredientsRequest public async Task <IActionResult> GetRecipesAsync(RecipeRequest recipeRequest) { if (!ModelState.IsValid) { return(BadRequest()); } // TODO: Consulta de acuerdo a los ingredientes que escoga la persona, // por el momento se traeran todas la recetas disponibles //var recipes = await _dataContext.Recipes // .Include(r => r.RecipeType) // .Include(r => r.Comments) // .Include(r => r.RateRecipes) // .Include(r => r.FavouriteRecipes) // .Include(r => r.RecipeIngredients) // .ThenInclude(r => r.Ingredient) // .ToListAsync(); //.ToList(); //.All(r => r.RecipeIngredients.Count > 0); //.AnyAsync(r => r.RecipeIngredients.Count > 0); //.Any(r => r.RecipeIngredients.Any(ri => ri.Ingredient == )) //try //{ // var recipes = ; //} //catch (Exception e) //{ // throw; //} //try //{ // var recipes = _dataContext.AllRecipesViews // .Where(x => recipeRequest.Ingredients.Any(y => y == x.NameIngredient)) // .ToList(); //} //catch (Exception ex) //{ // throw; //} //var recipes = await _dataContext.RecipeIngredients // .Include(r => r.Recipe) // .ThenInclude(r => r.RecipeType) // .Include(r => r.Recipe) // .ThenInclude(r => r.Comments) // .Include(r => r.Recipe) // .ThenInclude(r => r.RateRecipes) // .Include(r => r.Recipe) // .ThenInclude(r => r.FavouriteRecipes) // .Include(r => r.Ingredient) // .Where(r => r.Ingredient.Name.Contains(recipeRequest.Ingredients[0])) // .OrderBy(r => r.Recipe.Name) // .ToListAsync(); var recipes = await _dataContext.RecipeIngredients .Include(r => r.Recipe) .ThenInclude(r => r.RecipeType) .Include(r => r.Recipe) .ThenInclude(r => r.Comments) .Include(r => r.Recipe) .ThenInclude(r => r.RateRecipes) .Include(r => r.Recipe) .ThenInclude(r => r.FavouriteRecipes) .Include(r => r.Ingredient) .OrderBy(r => r.Recipe.Name) .ToListAsync(); var recipesfilter = recipes.Where(r => recipeRequest.Ingredients.Any(i => i == r.Ingredient.Name)) .ToList(); var response = new List <RecipeResponse>(); foreach (var recipe in recipesfilter) { var recipeResponse = new RecipeResponse { Id = recipe.Recipe.Id, Name = recipe.Recipe.Name, Description = recipe.Recipe.Description, Instructions = recipe.Recipe.Instructions, ImageUrl = recipe.Recipe.ImageFullPath, RecipeType = recipe.Recipe.RecipeType.Name, IngredientRecipes = recipe.Recipe.RecipeIngredients.Select(ri => new IngredientRecipeResponse { Id = ri.Id, Amount = ri.Amount, Ingredient = ri.Ingredient.Name }).ToList(), CommentResponses = recipe.Recipe.Comments.Select(c => new CommentResponse { Id = c.Id, Remarks = c.Remarks, Date = c.DateLocal, Customer = c.Customer.User.FullName }).ToList() }; response.Add(recipeResponse); } ; //response.Distinct().ToList(); //var noDupsList = new HashSet<RecipeResponse>(response).ToList(); return(Ok(response)); }
public async Task <IActionResult> Update([FromRoute] int id, RecipeRequest request) { await _recipesService.Update(id, request); return(Ok()); }
public async Task <IActionResult> Create(RecipeRequest request) { var id = await _recipesService.Create(request); return(Created($"/api/recipes/{id}", null)); }
public override async Task <Recipe> RequestRecipe(RecipeRequest request, ServerCallContext context) { var recipe = await _dispatcher.Dispatch(new GetRecipeQuery(request.Slug)); return(ConvertDomainToDto(recipe)); }
public async Task <IActionResult> PostRecipe([FromBody] RecipeRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Foto1 var imageUrl1 = string.Empty; if (request.PictureArray1 != null && request.PictureArray1.Length > 0) { var stream = new MemoryStream(request.PictureArray1); var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; var folder = "wwwroot\\images\\Recipes"; var fullPath = $"~/images/Recipes/{file}"; var response = FilesHelper.UploadPhoto(stream, folder, file); if (response) { imageUrl1 = fullPath; } } //Foto2 var imageUrl2 = string.Empty; if (request.PictureArray2 != null && request.PictureArray2.Length > 0) { var stream = new MemoryStream(request.PictureArray2); var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; var folder = "wwwroot\\images\\Recipes"; var fullPath = $"~/images/Recipes/{file}"; var response = FilesHelper.UploadPhoto(stream, folder, file); if (response) { imageUrl2 = fullPath; } } //Foto3 var imageUrl3 = string.Empty; if (request.PictureArray3 != null && request.PictureArray3.Length > 0) { var stream = new MemoryStream(request.PictureArray3); var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; var folder = "wwwroot\\images\\Recipes"; var fullPath = $"~/images/Recipes/{file}"; var response = FilesHelper.UploadPhoto(stream, folder, file); if (response) { imageUrl3 = fullPath; } } //Foto4 var imageUrl4 = string.Empty; if (request.PictureArray4 != null && request.PictureArray4.Length > 0) { var stream = new MemoryStream(request.PictureArray4); var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; var folder = "wwwroot\\images\\Recipes"; var fullPath = $"~/images/Recipes/{file}"; var response = FilesHelper.UploadPhoto(stream, folder, file); if (response) { imageUrl4 = fullPath; } } var recipe = new RecipeEntity { DischargeDate = request.DischargeDate, Flag1 = request.Flag1, Flag2 = request.Flag2, Flag3 = request.Flag3, Flag4 = request.Flag4, Foto1 = imageUrl1, Foto2 = imageUrl2, Foto3 = imageUrl3, Foto4 = imageUrl4, Name = request.Name, RecipeDate = request.RecipeDate, State = request.State, StateDate = request.StateDate, SocialWork = await _context.SocialWorks.FindAsync(request.SocialWorkId), User = await _context.Users.FindAsync(request.UserId), }; _context.Recipes.Add(recipe); await _context.SaveChangesAsync(); var responseFinal = new RecipeRequest { Id = recipe.Id, Name = recipe.Name, DischargeDate = recipe.DischargeDate, RecipeDate = recipe.RecipeDate, SocialWorkId = recipe.SocialWork.Id, UserId = recipe.User.Id, Flag1 = recipe.Flag1, Flag2 = recipe.Flag2, Flag3 = recipe.Flag3, Flag4 = recipe.Flag4, State = recipe.State, StateDate = recipe.StateDate, }; return(Ok(responseFinal)); }
public int CreateRecipe(RecipeRequest recipeRequest) { return(AddFoodRecipe(recipeRequest)); }