Skip to content

Commit 5a01915

Browse files
committed
Move migration into one file, allow many to many with group and add user to calendar items and event
1 parent 14a0afa commit 5a01915

File tree

29 files changed

+412
-221
lines changed

29 files changed

+412
-221
lines changed

api/lib/models/event/database.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class EventDatabaseService extends EventService with TableService {
2020
CREATE TABLE IF NOT EXISTS $name (
2121
id BLOB(16) PRIMARY KEY,
2222
parentId BLOB(16),
23-
groupId BLOB(16),
2423
blocked INTEGER NOT NULL DEFAULT 1,
2524
name VARCHAR(100) NOT NULL DEFAULT '',
2625
description TEXT NOT NULL DEFAULT '',

api/lib/models/event/group.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:flow_api/models/event/model.dart';
4+
import 'package:flow_api/models/group/model.dart';
5+
import 'package:flow_api/services/database.dart';
6+
7+
class EventGroupDatabaseConnector extends DatabaseModelConnector<Group, Event> {
8+
@override
9+
String get connectedIdName => "eventId";
10+
11+
@override
12+
String get connectedTableName => "events";
13+
14+
@override
15+
String get tableName => "eventGroups";
16+
17+
@override
18+
Future<List<Event>> getConnected(Uint8List itemId,
19+
{int offset = 0, int limit = 50}) async {
20+
final result = await db?.query(
21+
'$tableName JOIN events ON eventId = events.id',
22+
limit: limit,
23+
offset: offset,
24+
where: '$connectedIdName = ?',
25+
whereArgs: [itemId],
26+
);
27+
return result?.map((e) => Event.fromDatabase(e)).toList() ?? [];
28+
}
29+
30+
@override
31+
Future<List<Group>> getItems(Uint8List connectId,
32+
{int offset = 0, int limit = 50}) async {
33+
final result = await db?.query(
34+
'$tableName JOIN groups ON groupId = groups.id',
35+
limit: limit,
36+
offset: offset,
37+
where: '$itemIdName = ?',
38+
whereArgs: [connectId],
39+
);
40+
return result?.map((e) => Group.fromDatabase(e)).toList() ?? [];
41+
}
42+
43+
@override
44+
String get itemIdName => 'groupId';
45+
46+
@override
47+
String get itemTableName => 'groups';
48+
}

api/lib/models/event/item/database.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class CalendarItemDatabaseService extends CalendarItemService
1515
CalendarItemDatabaseService();
1616

1717
@override
18-
Future<void> create(Database db) async {
18+
Future<void> create(DatabaseExecutor db,
19+
[String name = 'calendarItems']) async {
1920
await db.execute("""
20-
CREATE TABLE IF NOT EXISTS calendarItems (
21+
CREATE TABLE IF NOT EXISTS $name (
2122
runtimeType VARCHAR(20) NOT NULL DEFAULT 'fixed',
2223
id BLOB(16) PRIMARY KEY,
2324
name VARCHAR(100) NOT NULL DEFAULT '',
@@ -42,16 +43,6 @@ class CalendarItemDatabaseService extends CalendarItemService
4243
""");
4344
}
4445

45-
@override
46-
Future<void> migrate(Database db, int version) async {
47-
if (version < 3) {
48-
await db.transaction((txn) async {
49-
await txn.execute("ALTER TABLE calendarItems ADD groupId BLOB(16)");
50-
await txn.execute("ALTER TABLE calendarItems ADD placeId BLOB(16)");
51-
});
52-
}
53-
}
54-
5546
@override
5647
Future<List<ConnectedModel<CalendarItem, Event?>>> getCalendarItems(
5748
{List<EventStatus>? status,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:flow_api/models/event/item/model.dart';
4+
import 'package:flow_api/models/group/model.dart';
5+
import 'package:flow_api/services/database.dart';
6+
7+
class CalendarItemGroupDatabaseConnector
8+
extends DatabaseModelConnector<Group, CalendarItem> {
9+
@override
10+
String get connectedIdName => "itemId";
11+
12+
@override
13+
String get connectedTableName => "calendarItems";
14+
15+
@override
16+
String get tableName => "calendaritemGroups";
17+
18+
@override
19+
Future<List<CalendarItem>> getConnected(Uint8List itemId,
20+
{int offset = 0, int limit = 50}) async {
21+
final result = await db?.query(
22+
'$tableName JOIN calendarItems ON itemId = calendarItems.id',
23+
limit: limit,
24+
offset: offset,
25+
where: '$connectedIdName = ?',
26+
whereArgs: [itemId],
27+
);
28+
return result?.map((e) => CalendarItem.fromDatabase(e)).toList() ?? [];
29+
}
30+
31+
@override
32+
Future<List<Group>> getItems(Uint8List connectId,
33+
{int offset = 0, int limit = 50}) async {
34+
final result = await db?.query(
35+
'$tableName JOIN groups ON groupId = groups.id',
36+
limit: limit,
37+
offset: offset,
38+
where: '$itemIdName = ?',
39+
whereArgs: [connectId],
40+
);
41+
return result?.map((e) => Group.fromDatabase(e)).toList() ?? [];
42+
}
43+
44+
@override
45+
String get itemIdName => 'groupId';
46+
47+
@override
48+
String get itemTableName => 'groups';
49+
}

api/lib/models/event/item/model.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ sealed class CalendarItem
1818
@override
1919
final String name, description;
2020
final String location;
21-
final Uint8List? groupId, eventId;
21+
final Uint8List? eventId;
2222
final DateTime? start, end;
2323
final EventStatus status;
2424

@@ -27,7 +27,6 @@ sealed class CalendarItem
2727
this.name = '',
2828
this.description = '',
2929
this.location = '',
30-
this.groupId,
3130
this.eventId,
3231
this.start,
3332
this.end,
@@ -65,7 +64,6 @@ final class FixedCalendarItem extends CalendarItem
6564
super.name,
6665
super.description,
6766
super.location,
68-
super.groupId,
6967
super.eventId,
7068
super.start,
7169
super.end,
@@ -86,7 +84,6 @@ final class RepeatingCalendarItem extends CalendarItem
8684
super.name,
8785
super.description,
8886
super.location,
89-
super.groupId,
9087
super.eventId,
9188
super.start,
9289
super.end,
@@ -116,7 +113,6 @@ final class AutoCalendarItem extends CalendarItem
116113
super.name,
117114
super.description,
118115
super.location,
119-
super.groupId,
120116
super.eventId,
121117
super.status,
122118
super.start,

api/lib/models/event/item/model.mapper.dart

Lines changed: 0 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/lib/models/event/item/service.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,3 @@ abstract class CalendarItemService extends ModelService {
2828

2929
FutureOr<bool> deleteCalendarItem(Uint8List id);
3030
}
31-
32-
abstract class CalendarItemResourceConnector<T> extends ModelService {
33-
FutureOr<void> connect(Uint8List calendarItemId, Uint8List resourceId);
34-
FutureOr<void> disconnect(Uint8List calendarItemId, Uint8List resourceId);
35-
FutureOr<bool> isConnected(Uint8List calendarItemId, Uint8List resourceId);
36-
}

0 commit comments

Comments
 (0)