Skip to content

Commit

Permalink
update database for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 27, 2024
1 parent ff939f4 commit 73e9083
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
17 changes: 17 additions & 0 deletions lib/models/tag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Tag {
int? id;
String? name;

Tag({this.id, this.name});

Map<String, Object?> toMap() {
return {'id': id, 'name': name};
}

static Tag fromMap(Map<String, dynamic> map) {
return Tag(
id: map['id'],
name: map['name'],
);
}
}
17 changes: 17 additions & 0 deletions lib/models/tagLink.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class TagLink {
int? recipeId;
int? tagId;

TagLink({this.recipeId, this.tagId});

Map<String, Object?> toMap() {
return {'recipeId': recipeId, 'tagId': tagId};
}

static TagLink fromMap(Map<String, dynamic> map) {
return TagLink(
recipeId: map['recipeId'],
tagId: map['tagId'],
);
}
}
43 changes: 40 additions & 3 deletions lib/utilities/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import 'dart:convert';

import 'package:reciper/models/tagLink.dart';
import 'package:reciper/models/tag.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import '../models/recipe.dart';
Expand Down Expand Up @@ -58,7 +60,7 @@ class DatabaseService {
""");

db.execute("""
CREATE TABLE RecipesTags(
CREATE TABLE TagsLinks(
recipeId INTEGER,
tagId INTEGER,
)
Expand Down Expand Up @@ -86,7 +88,7 @@ class DatabaseService {
)
""");
await database.execute("""
CREATE TABLE RecipesTags(
CREATE TABLE TagsLinks(
recipeId INTEGER,
tagId INTEGER,
)
Expand All @@ -110,15 +112,50 @@ class DatabaseService {
return id;
}

static Future<int> createTag(Tag tag) async {
print("CREATE");

final db = await DatabaseService.initializeDb();

final id = await db.insert('Tags', Tag(name: tag.name).toMap());
return id;
}

static Future<List<Recipe>> getRecipes({String searchQuery = ""}) async {
final db = await DatabaseService.initializeDb();

final List<Map<String, dynamic>> queryResult =
List<Map<String, dynamic>> queryResult =
await db.query('Recipes', where: "title LIKE '%$searchQuery%'");

return queryResult.map((e) => Recipe.fromMap(e)).toList();
}

static Future<List<Tag>> getTags(int? recipeId) async {
final db = await DatabaseService.initializeDb();

List<Map<String, dynamic>> queryResult = await db.query('Tags');

return queryResult.map((e) => Tag.fromMap(e)).toList();
}

static Future<List<Tag>> getTagsFromRecipe(int recipeId) async {
final db = await DatabaseService.initializeDb();

List<Map<String, dynamic>> recipeTagsLinks =
await db.query('TagsLinks', where: "recipeId = $recipeId");

List<Tag> recipeTags = [];

for (var tagLink in recipeTagsLinks) {
List<Map<String, dynamic>> tag =
await db.query('Tags', where: "id = ${tagLink['tagId']}");

recipeTags.add(Tag.fromMap(tag[0]));
}

return recipeTags;
}

static Future<Recipe> getRecipe(int id) async {
final db = await DatabaseService.initializeDb();

Expand Down

0 comments on commit 73e9083

Please sign in to comment.