diff --git a/lib/models/nutrition/log.dart b/lib/models/nutrition/log.dart index ac4aa845..8799d6ce 100644 --- a/lib/models/nutrition/log.dart +++ b/lib/models/nutrition/log.dart @@ -17,11 +17,14 @@ */ import 'package:json_annotation/json_annotation.dart'; +import 'package:powersync/sqlite3.dart' as sqlite; import 'package:wger/helpers/json.dart'; import 'package:wger/models/nutrition/ingredient.dart'; import 'package:wger/models/nutrition/ingredient_weight_unit.dart'; import 'package:wger/models/nutrition/meal_item.dart'; import 'package:wger/models/nutrition/nutritional_values.dart'; +import 'package:wger/models/schema.dart'; +import 'package:wger/powersync.dart'; part 'log.g.dart'; @@ -75,6 +78,19 @@ class Log { amount = mealItem.amount; } + factory Log.fromRow(sqlite.Row row) { + return Log( + id: row['id'], + mealId: row['meal_id'], + ingredientId: row['ingredient_id'], + weightUnitId: row['weight_unit_id'], + amount: row['amount'], + planId: row['plan_id'], + datetime: row['datetime'], + comment: row['comment'], + ); + } + // Boilerplate factory Log.fromJson(Map json) => _$LogFromJson(json); @@ -89,4 +105,31 @@ class Log { return ingredient.nutritionalValues / (100 / weight); } +/* + Future delete() async { + await db.execute('DELETE FROM $logItemsTable WHERE id = ?', [id]); + } + + static Future addPhoto(String photoId, String id) async { + await db.execute('UPDATE $logItemsTable SET photo_id = ? WHERE id = ?', [photoId, id]); + } +} + + static Stream> watchLists() { + // This query is automatically re-run when data in "lists" or "todos" is modified. + return db.watch('SELECT * FROM lists ORDER BY created_at, id').map((results) { + return results.map(TodoList.fromRow).toList(growable: false); + }); + } + + static Future create(String name) async { + final results = await db.execute(''' + INSERT INTO + lists(id, created_at, name, owner_id) + VALUES(uuid(), datetime(), ?, ?) + RETURNING * + ''', [name, await getUserId()]); + return TodoList.fromRow(results.first); + } + */ } diff --git a/lib/models/nutrition/log_powersync.dart b/lib/models/nutrition/log_powersync.dart deleted file mode 100644 index 25819152..00000000 --- a/lib/models/nutrition/log_powersync.dart +++ /dev/null @@ -1,68 +0,0 @@ -import 'package:powersync/sqlite3.dart' as sqlite; -import 'package:wger/models/schema.dart'; - -import '../../powersync.dart'; - -/// TodoItem represents a result row of a query on "todos". -/// -/// This class is immutable - methods on this class do not modify the instance -/// directly. Instead, watch or re-query the data to get the updated item. -/// confirm how the watch works. this seems like a weird pattern -class TodoItem { - final String id; - final String description; - final String? photoId; - final bool completed; - - TodoItem( - {required this.id, - required this.description, - required this.completed, - required this.photoId}); - - factory TodoItem.fromRow(sqlite.Row row) { - return TodoItem( - id: row['id'], - description: row['description'], - photoId: row['photo_id'], - completed: row['completed'] == 1); - } - - Future toggle() async { - if (completed) { - await db.execute( - 'UPDATE $logItemsTable SET completed = FALSE, completed_by = NULL, completed_at = NULL WHERE id = ?', - [id]); - } else { - await db.execute( - 'UPDATE $logItemsTable SET completed = TRUE, completed_by = ?, completed_at = datetime() WHERE id = ?', - [await getUserId(), id]); - } - } - - Future delete() async { - await db.execute('DELETE FROM $logItemsTable WHERE id = ?', [id]); - } - - static Future addPhoto(String photoId, String id) async { - await db.execute('UPDATE $logItemsTable SET photo_id = ? WHERE id = ?', [photoId, id]); - } -} -/* - static Stream> watchLists() { - // This query is automatically re-run when data in "lists" or "todos" is modified. - return db.watch('SELECT * FROM lists ORDER BY created_at, id').map((results) { - return results.map(TodoList.fromRow).toList(growable: false); - }); - } - - static Future create(String name) async { - final results = await db.execute(''' - INSERT INTO - lists(id, created_at, name, owner_id) - VALUES(uuid(), datetime(), ?, ?) - RETURNING * - ''', [name, await getUserId()]); - return TodoList.fromRow(results.first); - } - */ \ No newline at end of file