forked from eatvio/eatvio-infra
added tests for diets
This commit is contained in:
@@ -4,11 +4,11 @@ namespace App\Models;
|
||||
|
||||
use CyrildeWit\EloquentViewable\Contracts\Viewable;
|
||||
use CyrildeWit\EloquentViewable\InteractsWithViews;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* App\Models\Diet
|
||||
@@ -64,10 +64,20 @@ class Diet extends Model implements Viewable
|
||||
{
|
||||
use HasFactory, InteractsWithViews;
|
||||
|
||||
protected $withCount = [
|
||||
'likes',
|
||||
'comments',
|
||||
'choses',
|
||||
// protected $withCount = [
|
||||
// 'likes',
|
||||
// 'comments',
|
||||
// 'choses',
|
||||
// ];
|
||||
|
||||
public static function getWithLikesCommentsChosesCount(): Builder
|
||||
{
|
||||
return self::withCount(['likes', 'comments', 'choses']);
|
||||
}
|
||||
|
||||
protected $casts = [
|
||||
'type' => 'array',
|
||||
'daily_schedule' => 'array',
|
||||
];
|
||||
|
||||
private $allowedDietTypes = [
|
||||
@@ -120,19 +130,19 @@ class Diet extends Model implements Viewable
|
||||
|
||||
public function types(): Attribute
|
||||
{
|
||||
$data = $this->unserializeOptionsArr($this->type);
|
||||
$data = $this->type;
|
||||
|
||||
return new Attribute(
|
||||
get: fn () => array_keys($data),
|
||||
get: fn() => array_keys($data),
|
||||
);
|
||||
}
|
||||
|
||||
public function schedules(): Attribute
|
||||
{
|
||||
$data = $this->unserializeOptionsArr($this->daily_schedule);
|
||||
$data = $this->daily_schedule;
|
||||
|
||||
return new Attribute(
|
||||
get: fn () => $data,
|
||||
get: fn() => $data,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -168,8 +178,8 @@ class Diet extends Model implements Viewable
|
||||
foreach ($types as $type) {
|
||||
if (in_array($type, $this->allowedDietTypes)) {
|
||||
$query = $i === 0
|
||||
? $query->whereJsonContainsKey('type->'.$type)
|
||||
: $query->orWhereJsonContainsKey('type->'.$type);
|
||||
? $query->whereJsonContainsKey('type->' . $type)
|
||||
: $query->orWhereJsonContainsKey('type->' . $type);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
@@ -177,12 +187,12 @@ class Diet extends Model implements Viewable
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function unserializeOptionsArr($str)
|
||||
{
|
||||
if (preg_match('/^".*"$/', $str)) {
|
||||
return json_decode(stripslashes(mb_substr($str, 1, -1)), true);
|
||||
}
|
||||
// private function unserializeOptionsArr($str)
|
||||
// {
|
||||
// if (preg_match('/^".*"$/', $str)) {
|
||||
// return stripslashes(mb_substr($str, 1, -1)), true;
|
||||
// }
|
||||
|
||||
return json_decode(stripslashes($str), true);
|
||||
}
|
||||
// return json_decode(stripslashes($str), true);
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user