[CLEAR-38] Returning data with nutrition history

This commit is contained in:
Artur Nowakowski 2020-01-19 14:02:14 +01:00
parent 1624011258
commit b17d515a9c
5 changed files with 32 additions and 1 deletions

View File

@ -110,4 +110,12 @@ class UsersController extends Controller
return response()->json(['success' => true, 'message' => 'Recipe saved successfully in history']); 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);
}
} }

View File

@ -2,11 +2,19 @@
namespace App; namespace App;
use App\Models\Recipe;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class UsersRecipes extends Model class UsersRecipes extends Model
{ {
protected $fillable = ['user_id', 'recipe_id']; protected $fillable = ['user_id', 'recipe_id'];
protected $with = ['recipe'];
public function setUpdatedAtAttribute($value) {} public function setUpdatedAtAttribute($value) {}
public function recipe()
{
return $this->belongsTo(Recipe::class);
}
} }

View File

@ -45,4 +45,16 @@ class UsersRepository implements UsersRepositoryInterface
{ {
return UsersRecipes::create(['user_id' => $userID, 'recipe_id' => $recipeID]); 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;
}
} }

View File

@ -20,4 +20,6 @@ interface UsersRepositoryInterface
public function getWhereEquals($field, $actual); public function getWhereEquals($field, $actual);
public function saveRecipe(int $userID, int $recipeID); public function saveRecipe(int $userID, int $recipeID);
public function getRecipeHistory(int $userID);
} }

View File

@ -6,7 +6,8 @@ Route::group(['prefix' => 'user', 'middleware' => ['assign.guard:users']], funct
Route::post('/register', 'UsersController@register'); Route::post('/register', 'UsersController@register');
Route::post('/login', 'UsersController@login'); Route::post('/login', 'UsersController@login');
Route::get('/me', 'UsersController@me'); 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 () { Route::prefix('product')->group(function () {