11import '../external_projects/tiger_in_car/models/participating_projects.dart' ;
2+ import '../external_projects/tiger_in_car/models/project_list.dart' ;
23
34import 'package:path/path.dart' ;
45import 'package:sqflite/sqflite.dart' ;
@@ -22,7 +23,8 @@ class ProjectDatabase {
2223 final dbPath = await getDatabasesPath ();
2324 final path = join (dbPath, filePath);
2425
25- return await openDatabase (path, version: 1 , onCreate: _createDB);
26+ return await openDatabase (path, version: 2 , onCreate: _createDB, onUpgrade: _onUpgrade);
27+ //return await openDatabase(path, version: 1, onCreate: _createDB);
2628 }
2729
2830 Future _createDB (Database db, int version) async {
@@ -47,9 +49,44 @@ class ProjectDatabase {
4749 ${ProjectFields .locationSharingMethod } $intType ,
4850 ${ProjectFields .surveyElementCode } $stringType
4951 )
52+ ''' );
53+
54+ await db.execute ('''
55+ CREATE TABLE $tableProjectList (
56+ ${ProjectListFields .projectId } $idType ,
57+ ${ProjectListFields .projectName } $stringType ,
58+ ${ProjectListFields .projectDescription } $stringType ,
59+ ${ProjectListFields .externalLink } $NullstringType ,
60+ ${ProjectListFields .internalLink } $NullstringType ,
61+ ${ProjectListFields .projectImageLocation } $stringType ,
62+ ${ProjectListFields .locationSharingMethod } INTEGER NOT NULL,
63+ ${ProjectListFields .surveyElementCode } $stringType ,
64+ ${ProjectListFields .projectURL } $stringType
65+ )
5066 ''' );
5167 }
5268
69+ Future _onUpgrade (Database db, int oldVersion, int newVersion) async {
70+ if (oldVersion < 2 ) {
71+
72+ final NullstringType = 'STRING' ;
73+ await db.execute ('''
74+ CREATE TABLE $tableProjectList (
75+ ${ProjectListFields .projectId } INTEGER PRIMARY KEY AUTOINCREMENT,
76+ ${ProjectListFields .projectName } STRING NOT NULL,
77+ ${ProjectListFields .projectDescription } STRING NOT NULL,
78+ ${ProjectListFields .externalLink } $NullstringType ,
79+ ${ProjectListFields .internalLink } $NullstringType ,
80+ ${ProjectListFields .projectImageLocation } STRING NOT NULL,
81+ ${ProjectListFields .locationSharingMethod } INTEGER NOT NULL,
82+ ${ProjectListFields .surveyElementCode } STRING NOT NULL,
83+ ${ProjectListFields .projectURL } STRING NOT NULL
84+ )
85+ ''' );
86+ }
87+
88+ }
89+ //Procedures for projects user has joined
5390 Future <Particpating_Project > createProject (Particpating_Project project) async {
5491 final db = await instance.database;
5592
@@ -87,6 +124,8 @@ class ProjectDatabase {
87124 }
88125 }
89126
127+
128+
90129 Future <List <Particpating_Project >> getOngoingProjects () async {
91130 final db = await instance.database;
92131
@@ -177,5 +216,83 @@ final db = await instance.database;
177216 whereArgs: [id]);
178217 }
179218
219+
220+ //List of projects user has loaded onto their phone
221+
222+ Future <ProjectList > createNewProject (ProjectList project) async {
223+ final db = await instance.database;
224+ print ("It made it into createProject" );
225+ final id = await db.insert (tableProjectList, project.toJson ());
226+ return project.copy (projectId: id);
227+ }
228+
229+ Future <ProjectList > RetrieveProject (int id) async {
230+ final db = await instance.database;
231+ final maps = await db.query (
232+ tableProjectList,
233+ columns: ProjectListFields .values,
234+ where: '${ProjectListFields .projectId } = ?' ,
235+ whereArgs: [id],
236+ );
237+
238+ if (maps.isNotEmpty) {
239+ return ProjectList .fromJson (maps.first);
240+ } else {
241+ throw Exception ('Project ID $id not found' );
242+ }
243+ }
244+
245+ Future <ProjectList > RetrieveProjectbyURL (String URL ) async {
246+ final db = await instance.database;
247+ final maps = await db.query (
248+ tableProjectList,
249+ columns: ProjectListFields .values,
250+ where: '${ProjectListFields .projectURL } = ?' ,
251+ whereArgs: [URL ],
252+ );
253+
254+ if (maps.isNotEmpty) {
255+ return ProjectList .fromJson (maps.first);
256+ } else {
257+ return ProjectList (
258+ projectId: - 1 ,
259+ projectName: '-1' ,
260+ projectDescription: '-1' ,
261+ externalLink: '-1' ,
262+ internalLink: '-1' ,
263+ projectImageLocation: '-1' ,
264+ locationSharingMethod: - 1 ,
265+ surveyElementCode: '-1' ,
266+ );
267+ }
268+ }
269+
270+ Future <List <ProjectList >> FetchAllProjects () async {
271+ final db = await instance.database;
272+ final result = await db.query (tableProjectList);
273+ return result.map ((json) => ProjectList .fromJson (json)).toList ();
274+ }
275+
276+ Future <int > updateProjectList (ProjectList project) async {
277+ final db = await instance.database;
278+ return db.update (
279+ tableProjectList,
280+ project.toJson (),
281+ where: '${ProjectListFields .projectId } = ?' ,
282+ whereArgs: [project.projectId],
283+ );
284+ }
285+
286+ Future <int > deleteProjectList (int id) async {
287+ final db = await instance.database;
288+ return await db.delete (
289+ tableProjectList,
290+ where: '${ProjectListFields .projectId } = ?' ,
291+ whereArgs: [id],
292+ );
293+ }
294+
295+
296+
180297}
181298
0 commit comments