7
7
use App \Models \Recipe ;
8
8
use App \Models \Step ;
9
9
use App \Traits \FIleTrait ;
10
+ use Illuminate \Http \Request ;
11
+ use Illuminate \Support \Collection ;
10
12
use Illuminate \Support \Facades \DB ;
11
13
12
14
class RecipeRepository
@@ -24,7 +26,6 @@ public function createRecipeWithDetails(RecipeData $recipeData): Recipe
24
26
$ recipe = $ this ->createRecipe ($ recipeData );
25
27
$ this ->createIngredients ($ recipe ->id , $ recipeData ->ingredients );
26
28
$ this ->createSteps ($ recipe ->id , $ recipeData ->steps );
27
- //!empty($recipeData->tags) ? $this->syncTags($recipe, $recipeData->tags) : null;
28
29
29
30
if (!empty ($ recipeData ->tags )) {
30
31
$ this ->updateTags ($ recipe , $ recipeData ->tags );
@@ -66,11 +67,6 @@ protected function createSteps(int $recipeId, array $steps): void
66
67
}
67
68
}
68
69
69
- /*protected function syncTags(Recipe $recipe, array $tagIds): void
70
- {
71
- $recipe->tags()->sync($tagIds);
72
- }*/
73
-
74
70
protected function updateTags (Recipe $ recipe , array $ tagIds ): void
75
71
{
76
72
$ recipe ->tags ()->detach ();
@@ -86,7 +82,6 @@ public function updateRecipeWithDetails(int $recipeId, RecipeData $recipeData):
86
82
$ recipe = $ this ->updateRecipe ($ recipeId , $ recipeData );
87
83
$ this ->updateIngredients ($ recipeId , $ recipeData ->ingredients );
88
84
$ this ->updateSteps ($ recipeId , $ recipeData ->steps );
89
- //!empty($recipeData->tags) ? $this->syncTags($recipe, $recipeData->tags) : null;
90
85
91
86
if (!empty ($ recipeData ->tags )) {
92
87
$ this ->updateTags ($ recipe , $ recipeData ->tags );
@@ -144,4 +139,35 @@ protected function updateSteps(int $recipeId, array $steps): void
144
139
$ this ->stepModel ->create (array_merge ($ stepDataArray , ['recipe_id ' => $ recipeId ]));
145
140
}
146
141
}
142
+
143
+ public function getPublishedRecipesFromMembers (Collection $ users , Request $ request )
144
+ {
145
+ return Recipe::whereIn ('user_id ' , $ users )
146
+ ->where ('is_published ' , true )
147
+
148
+ ->when ($ request ->title , function ($ query , $ title ) {
149
+ return $ query ->where ('title ' , 'like ' , '% ' . $ title . '% ' );
150
+ })
151
+ ->when ($ request ->tags , function ($ query , $ tags ) {
152
+ return $ query ->whereHas ('tags ' , function ($ query ) use ($ tags ) {
153
+ $ query ->whereIn ('tag_id ' , $ tags );
154
+ });
155
+ })->paginate (2 );
156
+ }
157
+
158
+ public function applySorting (Request $ request , $ recipes ) {
159
+ switch ($ request ->sort ) {
160
+ case 'rating_asc ' :
161
+ $ recipes = $ recipes ->sortBy (function ($ recipe ) {
162
+ return $ recipe ->countAverageRatingForRecipe ();
163
+ });
164
+ break ;
165
+ case 'rating_desc ' :
166
+ $ recipes = $ recipes ->sortByDesc (function ($ recipe ) {
167
+ return $ recipe ->countAverageRatingForRecipe ();
168
+ });
169
+ break ;
170
+ }
171
+ return $ recipes ;
172
+ }
147
173
}
0 commit comments