public ResultDto <RecipeDto> Insert(IngredientViewModel ingredientViewModel) { var result = new ResultDto <RecipeDto>(); if (!_recipeRepository.Exist(x => x.Id == ingredientViewModel.RecipeId)) { result.Errors.Add(_Errors.RecipeDoesNotExist); } if (result.IsError) { return(result); } var ingredient = _mapper.Map <Ingredient>(ingredientViewModel); if (_ingredientRepository.Insert(ingredient) == 0) { result.Errors.Add(_Errors.SaveFail); return(result); } var recipe = _recipeRepository.GetBy(x => x.Id == ingredient.RecipeId, x => x.Picture); if (recipe == null) { result.Errors.Add(_Errors.RecipeDoesNotExist); return(result); } result.SuccessResult = _mapper.Map <RecipeDto>(recipe); return(result); }
public IEnumerable <RecipeDto> GetAll(int option) { var recipes = _recipeRepository.GetAll(x => x.Ratings); if (recipes == null) { return(null); } if (option == 0) { recipes = recipes.OrderByDescending(x => x.Ratings.Where(r => r.Value > 0).Count() > 0 ? x.Ratings.Where(r => r.Value > 0).Average(r => r.Value) : 0.0); } var recipesDto = _mapper.Map <IEnumerable <Recipe>, IEnumerable <RecipeDto> >(recipes); if (option == 1) { recipesDto = recipesDto.OrderByDescending(x => x.DifficultyRating); } if (option == 2) { recipesDto = recipesDto.OrderByDescending(x => x.PreparationTime); } return(recipesDto); }
public ResultDto <PictureDto> AddRecipePicture(IFormFile pictureFile, int recipeId) { var result = new ResultDto <PictureDto>(); if (pictureFile == null) { result.Errors.Add(_Errors.PictureFileNotExist); return(result); } if (pictureFile.Length > 5242880) { result.Errors.Add(_Errors.PictureTooBig); } var recipe = _recipeRepository.GetBy(x => x.Id == recipeId, x => x.Picture); if (recipe == null) { result.Errors.Add(_Errors.RecipeDoesNotExist); } if (result.IsError) { return(result); } if (recipe.Picture != null) { DeletePictureFiles(recipe.Picture, _env.WebRootPath); _pictureRepository.Delete(x => x.Id == recipe.Picture.Id); } var picture = SavePictureFiles(pictureFile); picture.Recipe = recipe; if (_pictureRepository.Insert(picture) == 0) { result.Errors.Add(_Errors.SaveFail); return(result); } result.SuccessResult = _mapper.Map <PictureDto>(picture); return(result); }
public ResultDto <BaseDto> InsertRating(RatingViewModel ratingViewModel) { var result = new ResultDto <BaseDto>(); if (!_recipeRepository.Exist(x => x.Id == ratingViewModel.RecipeId)) { result.Errors.Add(_Errors.RecipeDoesNotExist); } if (result.IsError) { return(result); } var rating = _mapper.Map <Rating>(ratingViewModel); if (_ratingRepository.Insert(rating) == 0) { result.Errors.Add(_Errors.SaveFail); return(result); } return(result); }