Support work sections

This commit is contained in:
Elias Projahn 2020-05-13 20:52:25 +02:00
parent 813fa2e47a
commit 93a5a06b55
6 changed files with 134 additions and 51 deletions

View file

@ -112,11 +112,17 @@ class Database extends _$Database {
));
}
final List<WorkSection> sections = [];
for (final section in await sectionsByWork(id).get()) {
sections.add(section);
}
return WorkInfo(
work: work,
instruments: instruments,
composers: composers,
parts: parts,
sections: sections,
);
}
@ -160,8 +166,8 @@ class Database extends _$Database {
await transaction(() async {
final workId = workInfo.work.id;
// Delete old work data first. The parts and instrumentations will be
// deleted automatically due to their foreign key constraints.
// Delete old work data first. The parts, sections and instrumentations
// will be deleted automatically due to their foreign key constraints.
await deleteWork(workId);
// This will also include the composers of the work's parts.
@ -194,6 +200,10 @@ class Database extends _$Database {
));
}
}
for (final section in workInfo.sections) {
await into(workSections).insert(section);
}
});
}

View file

@ -35,6 +35,13 @@ CREATE TABLE part_instrumentations (
instrument INTEGER NOT NULL REFERENCES instruments(id) ON DELETE CASCADE
);
CREATE TABLE work_sections (
id INTEGER NOT NULL PRIMARY KEY,
work INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE,
title TEXT NOT NULL,
before_part_index INTEGER NOT NULL
);
CREATE TABLE ensembles (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL
@ -80,6 +87,9 @@ SELECT * FROM works WHERE id = :id LIMIT 1;
partsByWork:
SELECT * FROM work_parts WHERE part_of = :id ORDER BY part_index;
sectionsByWork:
SELECT * FROM work_sections WHERE work = :id ORDER BY before_part_index;
worksByComposer:
SELECT DISTINCT works.* FROM works
JOIN work_parts ON work_parts.part_of = works.id

View file

@ -11,7 +11,7 @@ class PartInfo {
final List<Instrument> instruments;
/// The composer of this part.
///
///
/// This is null, if this part doesn't have a specific composer.
final Person composer;
@ -45,7 +45,7 @@ class WorkInfo {
final Work work;
/// A list of instruments.
///
///
/// This will not the include the instruments, that are specific to the work
/// parts.
final List<Instrument> instruments;
@ -56,11 +56,15 @@ class WorkInfo {
/// All available information on the work parts.
final List<PartInfo> parts;
/// The sections of this work.
final List<WorkSection> sections;
WorkInfo({
this.work,
this.instruments,
this.composers,
this.parts,
this.sections,
});
factory WorkInfo.fromJson(Map<String, dynamic> json) => WorkInfo(
@ -72,6 +76,9 @@ class WorkInfo {
json['composers'].map<Person>((j) => Person.fromJson(j)).toList(),
parts:
json['parts'].map<PartInfo>((j) => PartInfo.fromJson(j)).toList(),
sections: json['sections']
.map<WorkSection>((j) => WorkSection.fromJson(j))
.toList(),
);
Map<String, dynamic> toJson() => {
@ -79,6 +86,7 @@ class WorkInfo {
'instruments': instruments.map((i) => i.toJson()).toList(),
'composers': composers.map((c) => c.toJson()).toList(),
'parts': parts.map((c) => c.toJson()).toList(),
'sections': sections.map((s) => s.toJson()).toList(),
};
}