Skip to content

Commit

Permalink
Fix error message from checklist and size of the button
Browse files Browse the repository at this point in the history
  • Loading branch information
TheanYeeSin committed Mar 17, 2024
1 parent df4f66d commit 117b42e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 46 deletions.
146 changes: 101 additions & 45 deletions lib/database/database_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,22 @@ class DatabaseService {

static Future<int> addCategory(Category category) async {
final db = await _getDatabase();
return await db.insert(categoryTableName, category.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace);
return await db.insert(
categoryTableName,
category.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

static Future<int> updateCategory(Category category) async {
final db = await _getDatabase();
return await db.update(categoryTableName, category.toJson(),
where: 'id = ?',
whereArgs: [category.id],
conflictAlgorithm: ConflictAlgorithm.replace);
return await db.update(
categoryTableName,
category.toJson(),
where: 'id = ?',
whereArgs: [category.id],
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

static Future<int> deleteCategory(Category category) async {
Expand All @@ -93,7 +99,9 @@ class DatabaseService {
await db.query(categoryTableName, orderBy: orderBy);
if (maps.isNotEmpty) {
return List.generate(
maps.length, (index) => Category.fromJson(maps[index]));
maps.length,
(index) => Category.fromJson(maps[index]),
);
} else {
return null;
}
Expand All @@ -105,16 +113,22 @@ class DatabaseService {

static Future<int> addReview(Review review) async {
final db = await _getDatabase();
return await db.insert(reviewTableName, review.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace);
return await db.insert(
reviewTableName,
review.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

static Future<int> updateReview(Review review) async {
final db = await _getDatabase();
return await db.update(reviewTableName, review.toJson(),
where: 'id = ?',
whereArgs: [review.id],
conflictAlgorithm: ConflictAlgorithm.replace);
return await db.update(
reviewTableName,
review.toJson(),
where: 'id = ?',
whereArgs: [review.id],
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

static Future<int> deleteReview(Review review) async {
Expand All @@ -123,14 +137,17 @@ class DatabaseService {
.delete(reviewTableName, where: 'id = ?', whereArgs: [review.id]);
}

static Future<List<Review>?> getAllReviews(
{String orderBy = 'restaurantName ASC'}) async {
static Future<List<Review>?> getAllReviews({
String orderBy = 'restaurantName ASC',
}) async {
final db = await _getDatabase();
final List<Map<String, dynamic>> maps =
await db.query(reviewTableName, orderBy: orderBy);
if (maps.isNotEmpty) {
return List.generate(
maps.length, (index) => Review.fromJson(maps[index]));
maps.length,
(index) => Review.fromJson(maps[index]),
);
} else {
return null;
}
Expand All @@ -148,26 +165,38 @@ class DatabaseService {
}

static Future<List<Review>?> getReviewsByColumn(
String columnName, String columnValue,
{String orderBy = 'restaurantName ASC'}) async {
String columnName,
String columnValue, {
String orderBy = 'restaurantName ASC',
}) async {
final db = await _getDatabase();
final List<Map<String, dynamic>> maps = await db.query(reviewTableName,
orderBy: orderBy,
where: '$columnName LIKE ? OR $columnName LIKE ? OR $columnName LIKE ?',
whereArgs: ['%$columnValue', '%$columnValue,%', '%, $columnValue, %']);
final List<Map<String, dynamic>> maps = await db.query(
reviewTableName,
orderBy: orderBy,
where: '$columnName LIKE ? OR $columnName LIKE ? OR $columnName LIKE ?',
whereArgs: ['%$columnValue', '%$columnValue,%', '%, $columnValue, %'],
);
if (maps.isNotEmpty) {
return List.generate(
maps.length, (index) => Review.fromJson(maps[index]));
maps.length,
(index) => Review.fromJson(maps[index]),
);
} else {
return null;
}
}

static Future<int> updateReviewFavourite(
int reviewId, int isFavourite) async {
int reviewId,
int isFavourite,
) async {
final db = await _getDatabase();
return await db.update(reviewTableName, {'isFavourite': isFavourite},
where: 'id = ?', whereArgs: [reviewId]);
return await db.update(
reviewTableName,
{'isFavourite': isFavourite},
where: 'id = ?',
whereArgs: [reviewId],
);
}

//=============================
Expand All @@ -176,16 +205,22 @@ class DatabaseService {

static Future<int> addChecklistItem(ChecklistItem item) async {
final db = await _getDatabase();
return await db.insert(checklistItemTableName, item.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace);
return await db.insert(
checklistItemTableName,
item.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

static Future<int> updateChecklistItem(ChecklistItem item) async {
final db = await _getDatabase();
return await db.update(checklistItemTableName, item.toJson(),
where: 'id = ?',
whereArgs: [item.id],
conflictAlgorithm: ConflictAlgorithm.replace);
return await db.update(
checklistItemTableName,
item.toJson(),
where: 'id = ?',
whereArgs: [item.id],
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

static Future<int> deleteChecklistItem(ChecklistItem item) async {
Expand All @@ -201,7 +236,9 @@ class DatabaseService {
await db.query(checklistItemTableName, orderBy: orderBy);
if (maps.isNotEmpty) {
return List.generate(
maps.length, (index) => ChecklistItem.fromJson(maps[index]));
maps.length,
(index) => ChecklistItem.fromJson(maps[index]),
);
} else {
return null;
}
Expand All @@ -213,16 +250,22 @@ class DatabaseService {
.query(checklistItemTableName, where: 'isChecked = ?', whereArgs: [0]);
if (maps.isNotEmpty) {
return List.generate(
maps.length, (index) => ChecklistItem.fromJson(maps[index]));
maps.length,
(index) => ChecklistItem.fromJson(maps[index]),
);
} else {
throw Exception('Checklist not found');
return null;
}
}

static Future<int> updateChecklistItemChecked(int id, int isChecked) async {
final db = await _getDatabase();
return await db.update(checklistItemTableName, {'isChecked': isChecked},
where: 'id = ?', whereArgs: [id]);
return await db.update(
checklistItemTableName,
{'isChecked': isChecked},
where: 'id = ?',
whereArgs: [id],
);
}

static backupDatabase() async {
Expand All @@ -242,8 +285,12 @@ class DatabaseService {
File source = File('$dbFolder/$databaseName');
Directory copyTo = Directory('/storage/emulated/0/Tabemashou Backup');
await copyTo.create();
await source.copy(join(copyTo.path,
'FoodReviews_${now.day}_${now.month}_${now.year}-${now.hour}_${now.minute}.db'));
await source.copy(
join(
copyTo.path,
'FoodReviews_${now.day}_${now.month}_${now.year}-${now.hour}_${now.minute}.db',
),
);
} catch (e) {
print(e);
}
Expand Down Expand Up @@ -294,8 +341,11 @@ class DatabaseService {
// Check if the "reviews" table exists
bool reviewTableExists = false;
try {
var result = await database.query('sqlite_master',
where: 'name = ?', whereArgs: [reviewTableName]);
var result = await database.query(
'sqlite_master',
where: 'name = ?',
whereArgs: [reviewTableName],
);
reviewTableExists = result.isNotEmpty;
} catch (e) {
print("Error checking 'reviews' table: $e");
Expand All @@ -304,8 +354,11 @@ class DatabaseService {
// Check if the "categories" table exists
bool categoryTableExists = false;
try {
var result = await database.query('sqlite_master',
where: 'name = ?', whereArgs: [categoryTableName]);
var result = await database.query(
'sqlite_master',
where: 'name = ?',
whereArgs: [categoryTableName],
);
categoryTableExists = result.isNotEmpty;
} catch (e) {
print("Error checking 'categories' table: $e");
Expand All @@ -314,8 +367,11 @@ class DatabaseService {
// Check if the "checklist items" table exists
bool checklistItemTableExists = false;
try {
var result = await database.query('sqlite_master',
where: 'name = ?', whereArgs: [checklistItemTableName]);
var result = await database.query(
'sqlite_master',
where: 'name = ?',
whereArgs: [checklistItemTableName],
);
checklistItemTableExists = result.isNotEmpty;
} catch (e) {
print("Error checking 'checklist items' table: $e");
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/form/review_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class _ReviewFormState extends State<ReviewForm> {
//-----Select Categories Button-----
SizedBox(
width: 110,
height: 60,
child: ElevatedButton(
onPressed: _selectCategories,
child: Text(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.0
version: 1.1.1

environment:
sdk: '>=3.0.6 <4.0.0'
Expand Down

0 comments on commit 117b42e

Please sign in to comment.