2019-11-30 18:19:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
2020-01-07 19:45:45 +01:00
|
|
|
use App\UsersRecipes;
|
2020-01-19 14:53:23 +01:00
|
|
|
use Carbon\Carbon;
|
2019-11-30 18:19:13 +01:00
|
|
|
|
|
|
|
class UsersRepository implements UsersRepositoryInterface
|
|
|
|
{
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return User::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paginate($chunkSize)
|
|
|
|
{
|
|
|
|
return User::paginate($chunkSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($data)
|
|
|
|
{
|
|
|
|
return User::create($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$user = User::find($id);
|
|
|
|
return $user->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getById($id)
|
|
|
|
{
|
|
|
|
$user = User::find($id);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWhereEquals($field, $actual)
|
|
|
|
{
|
|
|
|
return User::where($field, $actual);
|
|
|
|
}
|
2020-01-07 19:45:45 +01:00
|
|
|
|
|
|
|
public function saveRecipe(int $userID, int $recipeID)
|
|
|
|
{
|
|
|
|
return UsersRecipes::create(['user_id' => $userID, 'recipe_id' => $recipeID]);
|
|
|
|
}
|
2020-01-19 14:02:14 +01:00
|
|
|
|
2020-01-19 14:53:23 +01:00
|
|
|
public function getRecipeHistory(int $userID, string $groupBy = 'day')
|
2020-01-19 14:02:14 +01:00
|
|
|
{
|
2020-01-19 17:42:44 +01:00
|
|
|
$userRecipes = UsersRecipes::where('user_id', $userID)->orderBy('created_at')->get();
|
2020-01-19 14:02:14 +01:00
|
|
|
$recipeHistory = [];
|
2020-01-19 14:53:23 +01:00
|
|
|
if ($groupBy == 'month') {
|
|
|
|
$userRecipes = $userRecipes->groupBy(function ($d) {
|
|
|
|
return Carbon::parse($d->created_at)->format('Y-m');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$userRecipes = $userRecipes->groupBy(function ($d) {
|
|
|
|
return Carbon::parse($d->created_at)->format('Y-m-d');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($userRecipes as $date => $dateRecipes) {
|
|
|
|
$nutritionSum = [
|
|
|
|
'kcal' => 0,
|
|
|
|
'carbohydrates' => 0,
|
|
|
|
'protein' => 0,
|
|
|
|
'fat' => 0,
|
|
|
|
];
|
2020-01-19 14:02:14 +01:00
|
|
|
|
2020-01-19 14:53:23 +01:00
|
|
|
foreach($dateRecipes as $userRecipe){
|
|
|
|
$nutritionValues = $userRecipe->recipe->nutrition;
|
|
|
|
foreach($nutritionValues as $name => $value){
|
|
|
|
$nutritionSum[$name] += $value;
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 15:18:45 +01:00
|
|
|
|
|
|
|
foreach($nutritionSum as $k => $v){
|
|
|
|
$nutritionSum[$k] = round($v, 2);
|
|
|
|
}
|
|
|
|
|
2020-01-19 14:53:23 +01:00
|
|
|
$recipeHistory[] = array_merge(['date' => $date], $nutritionSum);
|
2020-01-19 14:02:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $recipeHistory;
|
|
|
|
}
|
2019-11-30 18:19:13 +01:00
|
|
|
}
|