initial release

This commit is contained in:
Chris Smith
2025-02-19 22:41:18 +01:00
parent 61e85ae9a2
commit 9e9e989830
15 changed files with 96 additions and 59 deletions

View File

@@ -7,11 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
* Initial release
## [0.1.0] - 2025-02-10 ## [0.1.0] - 2025-02-10
* First release This marks the first release!
### Added
* User is able to signup, reset password, and also receives a welcome email
* Able to upload from phone with the rear camera initially
* Confetti on success :)
[Unreleased]: https://github.com/cgsmith/calorie/compare/0.0.1...HEAD [Unreleased]: https://github.com/cgsmith/calorie/compare/0.0.1...HEAD
[0.1.0]: https://github.com/cgsmith/calorie/releases/tag/0.1.0 [0.1.0]: https://github.com/cgsmith/calorie/releases/tag/0.1.0

View File

@@ -113,7 +113,6 @@ class GeminiApiComponent extends \yii\base\Component
$meal = new Meal(); $meal = new Meal();
$gemini = json_decode($response->getContent(), true); $gemini = json_decode($response->getContent(), true);
Yii::debug($gemini);
$geminiMeal = json_decode($gemini['candidates'][0]['content']['parts'][0]['text'], true); $geminiMeal = json_decode($gemini['candidates'][0]['content']['parts'][0]['text'], true);
$meal->protein = $geminiMeal['protein']; $meal->protein = $geminiMeal['protein'];
$meal->calories = $geminiMeal['calories']; $meal->calories = $geminiMeal['calories'];
@@ -121,8 +120,13 @@ class GeminiApiComponent extends \yii\base\Component
$meal->fat = $geminiMeal['fat']; $meal->fat = $geminiMeal['fat'];
$meal->fiber = $geminiMeal['fiber']; $meal->fiber = $geminiMeal['fiber'];
$meal->food_name = $geminiMeal['food_name']; $meal->food_name = $geminiMeal['food_name'];
$meal->user_id = Yii::$app->user->id;
$meal->file_name = $filePath;
Yii::debug($meal);
$meal->save();
// @TODO catch unidentified pictures?
return $meal; return $meal->id;
} }
} }

View File

@@ -2,6 +2,9 @@
namespace common\models; namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/** /**
* This is the model class for table "meal". * This is the model class for table "meal".
* *
@@ -13,12 +16,11 @@ namespace common\models;
* @property int $fat * @property int $fat
* @property int $carbohydrates * @property int $carbohydrates
* @property int $fiber * @property int $fiber
* @property int $meal
* @property int $user_id * @property int $user_id
* @property int $created_at * @property int $created_at
* @property int $updated_at * @property int $updated_at
*/ */
class Meal extends \yii\db\ActiveRecord class Meal extends ActiveRecord
{ {
public $base64File; public $base64File;
@@ -31,14 +33,28 @@ class Meal extends \yii\db\ActiveRecord
return 'meal'; return 'meal';
} }
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rules() public function rules()
{ {
return [ return [
[['food_name', 'file_name', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber', 'meal', 'created_at', 'updated_at'], 'required'], [['food_name', 'file_name', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber'], 'required'],
[['user_id', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber', 'meal', 'created_at', 'updated_at'], 'integer'], [['user_id', 'calories', 'protein', 'fat', 'carbohydrates', 'fiber', 'created_at', 'updated_at'], 'integer'],
[['file_name'], 'string', 'max' => 255], [['file_name'], 'string', 'max' => 255],
]; ];
} }
@@ -56,10 +72,8 @@ class Meal extends \yii\db\ActiveRecord
'fat' => 'Fat', 'fat' => 'Fat',
'carbohydrates' => 'Carbohydrates', 'carbohydrates' => 'Carbohydrates',
'fiber' => 'Fiber', 'fiber' => 'Fiber',
'meal' => 'Meal',
'created_at' => 'Created At', 'created_at' => 'Created At',
'updated_at' => 'Updated At', 'updated_at' => 'Updated At',
]; ];
} }
} }

View File

@@ -89,7 +89,7 @@ class User extends ActiveRecord implements IdentityInterface
public function rules() public function rules()
{ {
return [ return [
['status', 'default', 'value' => self::STATUS_VERIFIED], ['status', 'default', 'value' => self::STATUS_ACTIVE],
[['email'], 'email'], [['email'], 'email'],
[['email'], 'unique'], [['email'], 'unique'],
[['sales_agent_id', 'created_at', 'updated_at'], 'integer'], [['sales_agent_id', 'created_at', 'updated_at'], 'integer'],

View File

@@ -0,0 +1,39 @@
<?php
namespace common\models\search;
use common\models\Meal;
use Yii;
use yii\data\ActiveDataProvider;
class MealSearch extends Meal
{
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Meal::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
// ALWAYS filter by user_id that is signed in
$query->andFilterWhere(['user_id' => Yii::$app->user->id]);
if (!$this->validate()) {
return $dataProvider;
}
return $dataProvider;
}
}

View File

@@ -27,7 +27,7 @@ class m221203_160610_create_user_table extends Migration
'verification_token' => $this->string()->defaultValue(null), 'verification_token' => $this->string()->defaultValue(null),
'first_name' => $this->string(64), 'first_name' => $this->string(64),
'email' => $this->string()->notNull()->unique(), 'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(User::STATUS_UNVERIFIED), 'status' => $this->smallInteger()->notNull()->defaultValue(User::STATUS_ACTIVE),
'welcome_email_sent' => $this->boolean()->defaultValue(false), 'welcome_email_sent' => $this->boolean()->defaultValue(false),
'created_at' => $this->integer()->notNull(), 'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(), 'updated_at' => $this->integer()->notNull(),

View File

@@ -21,7 +21,6 @@ class m250219_133939_create_meal_table extends Migration
'fat' => $this->integer()->notNull(), 'fat' => $this->integer()->notNull(),
'carbohydrates' => $this->integer()->notNull(), 'carbohydrates' => $this->integer()->notNull(),
'fiber' => $this->integer()->notNull(), 'fiber' => $this->integer()->notNull(),
'meal' => $this->integer()->notNull(),
'user_id' => $this->integer()->notNull(), 'user_id' => $this->integer()->notNull(),
'created_at' => $this->integer()->notNull(), 'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(), 'updated_at' => $this->integer()->notNull(),

View File

@@ -3,6 +3,7 @@
namespace frontend\controllers; namespace frontend\controllers;
use common\models\Meal; use common\models\Meal;
use common\models\search\MealSearch;
use frontend\models\MealForm; use frontend\models\MealForm;
use Yii; use Yii;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
@@ -51,22 +52,10 @@ class MealController extends Controller
*/ */
public function actionIndex() public function actionIndex()
{ {
$dataProvider = new ActiveDataProvider([ $searchModel = new MealSearch();
'query' => Meal::find(),
/*
'pagination' => [
'pageSize' => 50
],
'sort' => [
'defaultOrder' => [
'id' => SORT_DESC,
]
],
*/
]);
return $this->render('index', [ return $this->render('index', [
'dataProvider' => $dataProvider, 'dataProvider' => $searchModel->search($this->request->queryParams),
]); ]);
} }
@@ -77,8 +66,8 @@ class MealController extends Controller
if (Yii::$app->request->isPost) { if (Yii::$app->request->isPost) {
$model->picture = UploadedFile::getInstance($model, 'picture'); $model->picture = UploadedFile::getInstance($model, 'picture');
if ($model->upload()) { if ($model->upload()) {
$meal = \Yii::$app->gemini->mealInquiry($model->filepath); $id = \Yii::$app->gemini->mealInquiry($model->filepath);
return $this->render('success', ['model' => $meal]); return Yii::$app->response->redirect(['meal/success', 'id' => $id]);
} }
} }
@@ -87,9 +76,11 @@ class MealController extends Controller
]); ]);
} }
public function actionSuccess() public function actionSuccess($id)
{ {
return $this->render('success'); $model = $this->findModel($id);
return $this->render('success', ['model' => $model]);
} }
/** /**
@@ -170,7 +161,7 @@ class MealController extends Controller
*/ */
protected function findModel($id) protected function findModel($id)
{ {
if (($model = Meal::findOne(['id' => $id])) !== null) { if (($model = MealSearch::findOne(['id' => $id, 'user_id' => Yii::$app->user->id])) !== null) {
return $model; return $model;
} }

View File

@@ -144,8 +144,8 @@ class SiteController extends Controller
{ {
$model = new SignupForm(); $model = new SignupForm();
if ($model->load(Yii::$app->request->post()) && $model->signup()) { if ($model->load(Yii::$app->request->post()) && $model->signup()) {
Yii::$app->session->setFlash('success', 'Thank you for registration! Snap your first meal.'); Yii::$app->session->setFlash('success', 'Thank you for registering! Sign in and snap your first meal!');
return $this->response->redirect(['meal/create']); return $this->response->redirect(['meal/upload']);
} }
return $this->render('signup', [ return $this->render('signup', [
@@ -170,11 +170,6 @@ class SiteController extends Controller
} }
// @todo
// fix deployment script
// save local .env variables for deployment
// verify email is working
// fix user sales agent issue
/** /**
* Requests password reset. * Requests password reset.
* *

View File

@@ -55,8 +55,8 @@ class SignupForm extends Model
// the following three lines were added: // the following three lines were added:
$auth = \Yii::$app->authManager; $auth = \Yii::$app->authManager;
$salesAgentRole = $auth->getRole('user'); $userRole = $auth->getRole('user');
$auth->assign($salesAgentRole, $user->getId()); $auth->assign($userRole, $user->getId());
return $this->sendEmail($user); return $this->sendEmail($user);
} }

View File

@@ -48,13 +48,9 @@ $this->beginPage() ?>
'label' => 'Capture Meal', 'label' => 'Capture Meal',
'url' => [Url::to(['meal/upload'])], 'url' => [Url::to(['meal/upload'])],
]; ];
$menuItems[] = [
'label' => 'List Meals',
'url' => [Url::to(['meal/index'])],
];
$menuItems[] = [ $menuItems[] = [
'label' => 'Summary', 'label' => 'Summary',
'url' => [Url::to(['summary'])], 'url' => [Url::to(['meal/index'])],
]; ];
} }

View File

@@ -25,19 +25,15 @@ $this->params['breadcrumbs'][] = $this->title;
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'columns' => [ 'columns' => [
['class' => 'yii\grid\SerialColumn'], ['class' => 'yii\grid\SerialColumn'],
'food_name',
'id',
'file_name',
'calories', 'calories',
'protein', 'protein',
'fat', 'fat',
//'carbohydrates', 'carbohydrates',
//'fiber', 'fiber',
//'meal', 'created_at:datetime',
//'created_at',
//'updated_at',
[ [
'class' => ActionColumn::className(), 'class' => ActionColumn::class,
'urlCreator' => function ($action, Meal $model, $key, $index, $column) { 'urlCreator' => function ($action, Meal $model, $key, $index, $column) {
return Url::toRoute([$action, 'id' => $model->id]); return Url::toRoute([$action, 'id' => $model->id]);
} }

View File

@@ -51,7 +51,7 @@ $this->registerJS("
<?= $form->field($model, 'picture') <?= $form->field($model, 'picture')
->fileInput([ ->fileInput([
'class' => 'form-control', 'class' => 'form-control',
//'capture' => 'environment', 'capture' => 'environment',
]); ?> ]); ?>
</div> </div>
<div class="form-group"> <div class="form-group">

View File

@@ -16,7 +16,7 @@ $this->title = 'Calorie Thingy';
<p class="fs-5 fw-light">Track your food with a picture!</p> <p class="fs-5 fw-light">Track your food with a picture!</p>
<p> <p>
<a class="btn btn-lg btn-success" href="<?= Yii::$app->getUrlManager()->createUrl(['meal/upload']) ?>">Capture a meal</a> <a class="btn btn-lg btn-success" href="<?= Yii::$app->getUrlManager()->createUrl(['meal/upload']) ?>">Capture a meal</a>
<a class="btn btn-lg btn-primary" href="<?= Yii::$app->getUrlManager()->createUrl(['summary']) ?>">View Summary</a></p> <a class="btn btn-lg btn-primary" href="<?= Yii::$app->getUrlManager()->createUrl(['meal/index']) ?>">View Summary</a></p>
</div> </div>
</div> </div>
<div class="body-content"> <div class="body-content">

View File

@@ -41,8 +41,6 @@ $this->params['breadcrumbs'][] = $this->title;
<div class="my-1 mx-0" style="color:#999;"> <div class="my-1 mx-0" style="color:#999;">
If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>. If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
<br>
Need new verification email? <?= Html::a('Resend', ['site/resend-verification-email']) ?>
</div> </div>
<div class="form-group"> <div class="form-group">