From b17d515a9cfe14f2d65f0e6007200def384eb28d Mon Sep 17 00:00:00 2001 From: Artur Nowakowski Date: Sun, 19 Jan 2020 14:02:14 +0100 Subject: [PATCH] [CLEAR-38] Returning data with nutrition history --- api/app/Http/Controllers/UsersController.php | 8 ++++++++ api/app/Models/UsersRecipes.php | 8 ++++++++ api/app/Repositories/UsersRepository.php | 12 ++++++++++++ api/app/Repositories/UsersRepositoryInterface.php | 2 ++ api/routes/api.php | 3 ++- 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/api/app/Http/Controllers/UsersController.php b/api/app/Http/Controllers/UsersController.php index e6d91c9..6885c27 100644 --- a/api/app/Http/Controllers/UsersController.php +++ b/api/app/Http/Controllers/UsersController.php @@ -110,4 +110,12 @@ class UsersController extends Controller return response()->json(['success' => true, 'message' => 'Recipe saved successfully in history']); } + public function getRecipeHistory() + { + $userID = auth()->id(); + $recipeHistory = $this->usersRepository->getRecipeHistory($userID); + + return response()->json(['success' => true, 'data' => ['history' => $recipeHistory]], 200); + } + } diff --git a/api/app/Models/UsersRecipes.php b/api/app/Models/UsersRecipes.php index 7ec5311..c41b16e 100644 --- a/api/app/Models/UsersRecipes.php +++ b/api/app/Models/UsersRecipes.php @@ -2,11 +2,19 @@ namespace App; +use App\Models\Recipe; use Illuminate\Database\Eloquent\Model; class UsersRecipes extends Model { protected $fillable = ['user_id', 'recipe_id']; + protected $with = ['recipe']; + public function setUpdatedAtAttribute($value) {} + + public function recipe() + { + return $this->belongsTo(Recipe::class); + } } diff --git a/api/app/Repositories/UsersRepository.php b/api/app/Repositories/UsersRepository.php index b4d4863..6691fca 100644 --- a/api/app/Repositories/UsersRepository.php +++ b/api/app/Repositories/UsersRepository.php @@ -45,4 +45,16 @@ class UsersRepository implements UsersRepositoryInterface { return UsersRecipes::create(['user_id' => $userID, 'recipe_id' => $recipeID]); } + + public function getRecipeHistory(int $userID) + { + $userRecipes = UsersRecipes::where('user_id', $userID)->get(); + $recipeHistory = []; + + foreach ($userRecipes as $userRecipe) { + $recipeHistory[] = array_merge(['date' => $userRecipe->created_at], $userRecipe->recipe->nutrition); + } + + return $recipeHistory; + } } diff --git a/api/app/Repositories/UsersRepositoryInterface.php b/api/app/Repositories/UsersRepositoryInterface.php index 4c443f8..1e2da23 100644 --- a/api/app/Repositories/UsersRepositoryInterface.php +++ b/api/app/Repositories/UsersRepositoryInterface.php @@ -20,4 +20,6 @@ interface UsersRepositoryInterface public function getWhereEquals($field, $actual); public function saveRecipe(int $userID, int $recipeID); + + public function getRecipeHistory(int $userID); } diff --git a/api/routes/api.php b/api/routes/api.php index 53ffdef..1ae0c5c 100644 --- a/api/routes/api.php +++ b/api/routes/api.php @@ -6,7 +6,8 @@ Route::group(['prefix' => 'user', 'middleware' => ['assign.guard:users']], funct Route::post('/register', 'UsersController@register'); Route::post('/login', 'UsersController@login'); Route::get('/me', 'UsersController@me'); - Route::post('/save-recipe', 'UsersController@saveRecipe'); + Route::post('/save-recipe', 'UsersController@saveRecipe')->middleware(['jwt.auth']); + Route::get('/history', 'UsersController@getRecipeHistory')->middleware(['jwt.auth']); }); Route::prefix('product')->group(function () {