Adding API

This commit is contained in:
Chris Smith
2025-02-20 20:09:01 +01:00
parent 0c639ce11e
commit 5f3d1359cf
15 changed files with 292 additions and 133 deletions

View File

@@ -2,8 +2,10 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\UnauthorizedHttpException;
/**
* This is the model class for table "meal".
@@ -53,7 +55,7 @@ class Meal extends ActiveRecord
public function rules()
{
return [
[['food_name', 'file_name', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber'], 'required'],
[['food_name', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber'], 'required'],
[['user_id', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber', 'created_at', 'updated_at'], 'integer'],
[['file_name'], 'string', 'max' => 255],
];
@@ -76,4 +78,30 @@ class Meal extends ActiveRecord
'updated_at' => 'Updated At',
];
}
// Automatically set the user_id before saving a new meal
/**
* @throws UnauthorizedHttpException
*/
public function beforeSave($insert)
{
if ($this->isNewRecord) {
// Get the currently authenticated user's ID
if (Yii::$app->user->isGuest) {
throw new \yii\web\UnauthorizedHttpException('User not authenticated.');
}
// Set the user_id to the current authenticated user
$this->user_id = Yii::$app->user->identity->id;
}
return parent::beforeSave($insert);
}
// Define relationships (if any)
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
}