[CLEAR-38] Returning data with nutrition history
This commit is contained in:
parent
1624011258
commit
b17d515a9c
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -20,4 +20,6 @@ interface UsersRepositoryInterface
|
||||
public function getWhereEquals($field, $actual);
|
||||
|
||||
public function saveRecipe(int $userID, int $recipeID);
|
||||
|
||||
public function getRecipeHistory(int $userID);
|
||||
}
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user