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

@@ -120,6 +120,7 @@ class GeminiApiComponent extends \yii\base\Component
$meal->fat = $geminiMeal['fat'];
$meal->fiber = $geminiMeal['fiber'];
$meal->food_name = $geminiMeal['food_name'];
// @TODO if moved a job queue then this must be an object otherwise the queue is NOT aware of the user
$meal->user_id = Yii::$app->user->id;
$meal->file_name = $filePath;
Yii::debug($meal);

View File

@@ -20,9 +20,7 @@ class Yii {
/**
* @property yii\rbac\DbManager $authManager
* @property \Da\User\Model\User $user
* @property \common\components\GeminiApiComponent $sonar
* @property \common\components\HubspotApiComponent $hubspot
* @property \common\components\GeminiApiComponent $gemini
* @property \common\components\PostmarkComponent $postmark
* @property \yii\queue\db\Queue $queue
*

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']);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace common\models;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
class MealForm extends Model
{
/**
* @var UploadedFile
*/
public $picture;
public string $filepath;
public function rules() {
return [
[['picture'], 'file', 'skipOnEmpty' => false],
[['picture'], 'required'],
];
}
public function newFileName()
{
$this->filepath = (string)'uploads/' . Yii::$app->user->id . '-' . Uuid::uuid4() . '.' . $this->picture->extension;
}
public function upload()
{
if ($this->validate()) {
$this->newFileName();
$this->picture->saveAs('@frontend/web/'.$this->filepath);
return true;
} else {
return false;
}
}
}

View File

@@ -182,7 +182,7 @@ class User extends ActiveRecord implements IdentityInterface
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
return static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]);
}
/**