|
| 1 | +import 'package:gruene_app/features/campaigns/helper/campaign_action.dart'; |
| 2 | +import 'package:path/path.dart'; |
| 3 | +import 'package:sqflite/sqflite.dart'; |
| 4 | + |
| 5 | +class CampaignActionDatabase { |
| 6 | + static final CampaignActionDatabase instance = CampaignActionDatabase._internal(); |
| 7 | + |
| 8 | + static Database? _database; |
| 9 | + |
| 10 | + CampaignActionDatabase._internal(); |
| 11 | + |
| 12 | + Future<Database> get database async { |
| 13 | + return _database ??= await _initDatabase(); |
| 14 | + } |
| 15 | + |
| 16 | + Future<Database> _initDatabase() async { |
| 17 | + final databasePath = await getDatabasesPath(); |
| 18 | + final path = join(databasePath, 'campaign_action_db.db'); |
| 19 | + // await File(path).delete(); |
| 20 | + return await openDatabase( |
| 21 | + path, |
| 22 | + version: 1, |
| 23 | + onCreate: _createDatabase, |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + Future<void> _createDatabase(Database db, _) async { |
| 28 | + return await db.execute(''' |
| 29 | + CREATE TABLE ${CampaignActionFields.tableName} ( |
| 30 | + ${CampaignActionFields.id} ${CampaignActionFields.idType}, |
| 31 | + ${CampaignActionFields.poiId} ${CampaignActionFields.intTypeNullable}, |
| 32 | + ${CampaignActionFields.poiTempId} ${CampaignActionFields.intType}, |
| 33 | + ${CampaignActionFields.actionType} ${CampaignActionFields.intType}, |
| 34 | + ${CampaignActionFields.serialized} ${CampaignActionFields.textTypeNullable} |
| 35 | + ) |
| 36 | + '''); |
| 37 | + } |
| 38 | + |
| 39 | + Future<CampaignAction> create(CampaignAction campaignAction) async { |
| 40 | + final db = await instance.database; |
| 41 | + final id = await db.insert(CampaignActionFields.tableName, campaignAction.toMap()); |
| 42 | + return campaignAction.copyWith(id: id); |
| 43 | + } |
| 44 | + |
| 45 | + // Future<NoteModel> read(int id) async { |
| 46 | + // final db = await instance.database; |
| 47 | + // final maps = await db.query( |
| 48 | + // CampaignActionFields.tableName, |
| 49 | + // columns: CampaignActionFields.values, |
| 50 | + // where: '${CampaignActionFields.id} = ?', |
| 51 | + // whereArgs: [id], |
| 52 | + // ); |
| 53 | + |
| 54 | + // if (maps.isNotEmpty) { |
| 55 | + // return NoteModel.fromJson(maps.first); |
| 56 | + // } else { |
| 57 | + // throw Exception('ID $id not found'); |
| 58 | + // } |
| 59 | + // } |
| 60 | + |
| 61 | + Future<List<CampaignAction>> readAll() async { |
| 62 | + final db = await instance.database; |
| 63 | + const orderBy = '${CampaignActionFields.poiTempId} DESC'; |
| 64 | + final result = await db.query(CampaignActionFields.tableName, orderBy: orderBy); |
| 65 | + return result.map((json) => CampaignAction.fromMap(json)).toList(); |
| 66 | + } |
| 67 | + |
| 68 | + Future<List<CampaignAction>> readAllByActionType(List<int> posterActions) async { |
| 69 | + final db = await instance.database; |
| 70 | + const orderBy = '${CampaignActionFields.poiTempId} DESC'; |
| 71 | + final result = await db.query( |
| 72 | + CampaignActionFields.tableName, |
| 73 | + orderBy: orderBy, |
| 74 | + where: '${CampaignActionFields.actionType} IN (${List.filled(posterActions.length, '?').join(',')})', |
| 75 | + whereArgs: posterActions, |
| 76 | + ); |
| 77 | + return result.map((json) => CampaignAction.fromMap(json)).toList(); |
| 78 | + } |
| 79 | + |
| 80 | + Future<List<CampaignAction>> getActionsWithPoiId(String poiId) async { |
| 81 | + final db = await instance.database; |
| 82 | + const orderBy = '${CampaignActionFields.poiTempId} DESC'; |
| 83 | + final result = await db.query( |
| 84 | + CampaignActionFields.tableName, |
| 85 | + orderBy: orderBy, |
| 86 | + where: '${CampaignActionFields.poiId} = ? OR ${CampaignActionFields.poiTempId} = ?', |
| 87 | + whereArgs: [poiId, poiId], |
| 88 | + ); |
| 89 | + return result.map((json) => CampaignAction.fromMap(json)).toList(); |
| 90 | + } |
| 91 | + // Future<int> update(NoteModel note) async { |
| 92 | + // final db = await instance.database; |
| 93 | + // return db.update( |
| 94 | + // CampaignActionFields.tableName, |
| 95 | + // note.toJson(), |
| 96 | + // where: '${CampaignActionFields.id} = ?', |
| 97 | + // whereArgs: [note.id], |
| 98 | + // ); |
| 99 | + // } |
| 100 | + |
| 101 | + Future<int> delete(int id) async { |
| 102 | + final db = await instance.database; |
| 103 | + return await db.delete( |
| 104 | + CampaignActionFields.tableName, |
| 105 | + where: '${CampaignActionFields.id} = ?', |
| 106 | + whereArgs: [id], |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + Future<void> close() async { |
| 111 | + final db = await instance.database; |
| 112 | + db.close(); |
| 113 | + } |
| 114 | + |
| 115 | + Future<int> getCount() async { |
| 116 | + final db = await instance.database; |
| 117 | + int count = Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM ${CampaignActionFields.tableName}')) ?? 0; |
| 118 | + return count; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +class CampaignActionFields { |
| 123 | + static const String tableName = 'campaign_action'; |
| 124 | + static const String idType = 'INTEGER PRIMARY KEY AUTOINCREMENT'; |
| 125 | + static const String textTypeNullable = 'TEXT'; |
| 126 | + static const String intType = 'INTEGER NOT NULL'; |
| 127 | + static const String intTypeNullable = 'INTEGER'; |
| 128 | + static const String id = '_id'; |
| 129 | + static const String poiId = 'poiId'; |
| 130 | + static const String poiTempId = 'poiTempId'; |
| 131 | + static const String actionType = 'actionType'; |
| 132 | + static const String serialized = 'serialized'; |
| 133 | +} |
0 commit comments