Create basic working structure

Signed-off-by: Christian Wolf <github@christianwolf.email>
This commit is contained in:
Christian Wolf 2022-07-06 08:28:58 +02:00
Родитель b615ebfd6b
Коммит a43dd01976
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 9FC3120E932F73F1
6 изменённых файлов: 83 добавлений и 20 удалений

Просмотреть файл

@ -40,7 +40,7 @@
"**/vendor/**/{Tests,tests}/**",
"**/.history/**",
"**/vendor/**/vendor/**",
"3rdparty/**"
// "3rdparty/**"
],
"cSpell.words": [
"Nextcloud"
@ -52,6 +52,9 @@
"other": "on",
"comments": "on",
"strings": "on"
}
},
"intelephense.environment.includePaths": [
"${workspaceFolder:base}/3rdparty/doctrine/dbal/src"
]
}
}

Просмотреть файл

@ -14,6 +14,9 @@ class DbTypesPolyfillHelper {
*/
private $string;
/** @var string */
private $date;
public function __construct(Util $util) {
switch ($util->getVersion()[0]) {
case 18:
@ -21,11 +24,13 @@ class DbTypesPolyfillHelper {
case 20:
$this->int = \Doctrine\DBAL\Types\Type::INTEGER;
$this->string = \Doctrine\DBAL\Types\Type::STRING;
$this->date = \Doctrine\DBAL\Types\Type::DATE;
break;
default:
$this->int = \OCP\DB\Types::INTEGER;
$this->string = \OCP\DB\Types::STRING;
$this->date = \OCP\DB\Types::DATE;
break;
}
}
@ -43,4 +48,11 @@ class DbTypesPolyfillHelper {
final public function STRING() {
return $this->string;
}
/**
* @return string
*/
final public function DATE() {
return $this->date;
}
}

Просмотреть файл

@ -2,6 +2,7 @@
namespace OCA\Cookbook\Db;
use DateTimeImmutable;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\AppFramework\Db\DoesNotExistException;
@ -91,7 +92,7 @@ class RecipeDb {
public function findAllRecipes(string $user_id) {
$qb = $this->db->getQueryBuilder();
$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.dateCreated', 'r.dateModified', 'k.name AS keywords'])
->from(self::DB_TABLE_RECIPES, 'r')
->where('r.user_id = :user')
->orderBy('r.name');
@ -229,7 +230,7 @@ class RecipeDb {
// for the recipe, but those don't seem to work:
// $qb->select(['r.recipe_id', 'r.name', 'GROUP_CONCAT(k.name) AS keywords']) // not working
// $qb->select(['r.recipe_id', 'r.name', DB::raw('GROUP_CONCAT(k.name) AS keywords')]) // not working
$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.dateCreated', 'r.dateModified', 'k.name AS keywords'])
->from(self::DB_TABLE_CATEGORIES, 'c')
->where('c.name = :category')
->andWhere('c.user_id = :user')
@ -242,7 +243,7 @@ class RecipeDb {
$qb->groupBy(['r.name', 'r.recipe_id', 'k.name']);
$qb->orderBy('r.name');
} else {
$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.dateCreated', 'r.dateModified', 'k.name AS keywords'])
->from(self::DB_TABLE_RECIPES, 'r')
->leftJoin('r', self::DB_TABLE_KEYWORDS, 'k', 'r.recipe_id = k.recipe_id')
->leftJoin(
@ -279,7 +280,7 @@ class RecipeDb {
$qb = $this->db->getQueryBuilder();
$qb->select(['r.recipe_id', 'r.name', 'kk.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.dateCreated', 'r.dateModified', 'kk.name AS keywords'])
->from(self::DB_TABLE_KEYWORDS, 'k')
->where('k.name IN (:keywords)')
->andWhere('k.user_id = :user')
@ -314,7 +315,7 @@ class RecipeDb {
$qb = $this->db->getQueryBuilder();
$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.dateCreated', 'r.dateModified', 'k.name AS keywords'])
->from(self::DB_TABLE_RECIPES, 'r');
$qb->leftJoin('r', self::DB_TABLE_KEYWORDS, 'k', 'k.recipe_id = r.recipe_id');
@ -454,7 +455,9 @@ class RecipeDb {
->values([
'recipe_id' => ':id',
'user_id' => ':userid',
'name' => ':name'
'name' => ':name',
'dateCreated' => ':dateCreated',
'dateModified' => ':dateModified',
]);
$qb->setParameter('userid', $userId);
@ -462,6 +465,15 @@ class RecipeDb {
foreach ($recipes as $recipe) {
$qb->setParameter('id', $recipe['id'], $this->types->INT());
$qb->setParameter('name', $recipe['name'], $this->types->STRING());
$dateCreated = $this->parseDate($recipe['dateCreated']);
if($dateCreated === null) {
$dateCreated = $this->parseDate('now');
}
$qb->setParameter('dateCreated', $dateCreated, $this->types->DATE());
$dateModified = $this->parseDate($recipe['dateModified']);
$qb->setParameter('dateModified', $dateModified, $this->types->DATE());
$qb->execute();
}
@ -472,20 +484,30 @@ class RecipeDb {
return;
}
$qb = $this->db->getQueryBuilder();
foreach ($recipes as $recipe) {
$qb = $this->db->getQueryBuilder();
$qb->update(self::DB_TABLE_RECIPES)
->where('recipe_id = :id', 'user_id = :uid');
$literal = [];
$literal['name'] = $qb->expr()->literal($recipe['name'], IQueryBuilder::PARAM_STR);
$qb->set('name', $literal['name']);
// $literal = [];
// $literal['name'] = $qb->expr()->literal($recipe['name'], IQueryBuilder::PARAM_STR);
// $qb->set('name', $literal['name']);
$qb->set('name', $qb->createNamedParameter($recipe['name'], IQueryBuilder::PARAM_STR));
$dateCreated = $this->parseDate($recipe['dateCreated'] ?? 'now');
$dateModified = $this->parseDate($recipe['dateModified']) ?? $dateCreated;
$qb->set('dateCreated', $qb->expr()->literal($dateCreated, IQueryBuilder::PARAM_DATE));
$qb->set('dateModified', $qb->expr()->literal($dateModified, IQueryBuilder::PARAM_DATE));
$qb->setParameter('id', $recipe['id']);
$qb->setParameter('uid', $userId);
$qb->execute();
try {
$qb->execute();
} catch (\Exception $ex) {
throw $ex;
}
}
}
@ -617,4 +639,16 @@ class RecipeDb {
$qb->execute();
}
private function parseDate(?string $date) {
if (is_null($date)) {
return null;
}
try{
return new DateTimeImmutable($date);
} catch (\Exception $ex) {
return new DateTimeImmutable();
}
}
}

Просмотреть файл

@ -35,13 +35,13 @@ class Version000000Date20220703174647 extends SimpleMigrationStep {
$table = $schema->getTable('cookbook_names');
if(! $table->hasColumn('dateCreated')) {
$table->addColumn('dateCreated', 'date', [
$table->addColumn('dateCreated', 'datetime_immutable', [
'notnull' => false,
]);
}
if(!$table->hasColumn('dateModified')) {
$table->addColumn('dateModified', 'date', [
$table->addColumn('dateModified', 'datetime_immutable', [
'notnull' => false,
]);
}

Просмотреть файл

@ -142,6 +142,10 @@ class DbCacheService {
$id = (int) $jsonFile->getParent()->getId();
$json['id'] = $id;
if(!isset($json['dateModified'])) {
$json['dateModified'] = null;
}
return $json;
}
@ -157,6 +161,8 @@ class DbCacheService {
$obj = [];
$obj['name'] = $row['name'];
$obj['id'] = $id;
$obj['dateCreated'] = $row['dateCreated'];
$obj['dateModified'] = $row['dateModified'];
$ret[$id] = $obj;
}
@ -220,6 +226,13 @@ class DbCacheService {
if ($dbEntry['name'] !== $fileEntry['name']) {
return false;
}
if($dbEntry['dateCreated'] !== $fileEntry['dateCreated']) {
return false;
}
if($dbEntry['dateModified'] !== $fileEntry['dateModified']) {
return false;
}
return true;
}

Просмотреть файл

@ -942,10 +942,11 @@ class RecipeService {
*/
private function addDatesToRecipes(array &$recipes) {
foreach ($recipes as $i => $recipe) {
// TODO Add data to database instead of reading from files
$r = $this->getRecipeById($recipe['recipe_id']);
$recipes[$i]['dateCreated'] = $r['dateCreated'];
$recipes[$i]['dateModified'] = $r['dateModified'];
if(! array_key_exists('dateCreated', $recipe) || ! array_key_exists('dateModified', $recipe)) {
$r = $this->getRecipeById($recipe['recipe_id']);
$recipes[$i]['dateCreated'] = $r['dateCreated'];
$recipes[$i]['dateModified'] = $r['dateModified'];
}
}
}