mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
Add basic http server
This commit is contained in:
parent
a43bd91922
commit
d77f49a21b
17 changed files with 340 additions and 0 deletions
2
server/lib/musicus_server.dart
Normal file
2
server/lib/musicus_server.dart
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export 'src/configuration.dart';
|
||||
export 'src/server.dart';
|
||||
14
server/lib/src/compositions.dart
Normal file
14
server/lib/src/compositions.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class CompositionsController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
CompositionsController(this.db);
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getWorks(@Bind.path('id') int id) async {
|
||||
final works = db.worksByComposer(id).get();
|
||||
return Response.ok(works);
|
||||
}
|
||||
}
|
||||
13
server/lib/src/configuration.dart
Normal file
13
server/lib/src/configuration.dart
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:aqueduct/aqueduct.dart';
|
||||
|
||||
class MusicusServerConfiguration extends Configuration {
|
||||
MusicusServerConfiguration(String fileName) : super.fromFile(File(fileName));
|
||||
|
||||
String host;
|
||||
int port;
|
||||
|
||||
@optionalConfiguration
|
||||
String dbPath;
|
||||
}
|
||||
36
server/lib/src/ensembles.dart
Normal file
36
server/lib/src/ensembles.dart
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class EnsemblesController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
EnsemblesController(this.db);
|
||||
|
||||
@Operation.get()
|
||||
Future<Response> getEnsembles() async {
|
||||
final ensembles = await db.allEnsembles().get();
|
||||
return Response.ok(ensembles);
|
||||
}
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getEnsemble(@Bind.path('id') int id) async {
|
||||
final ensemble = await db.ensembleById(id).getSingle();
|
||||
if (ensemble != null) {
|
||||
return Response.ok(ensemble);
|
||||
} else {
|
||||
return Response.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation.put('id')
|
||||
Future<Response> putEnsemble(
|
||||
@Bind.path('id') int id, @Bind.body() Map<String, dynamic> json) async {
|
||||
final ensemble = Ensemble.fromJson(json).copyWith(
|
||||
id: id,
|
||||
);
|
||||
|
||||
await db.updateEnsemble(ensemble);
|
||||
|
||||
return Response.ok(null);
|
||||
}
|
||||
}
|
||||
36
server/lib/src/instruments.dart
Normal file
36
server/lib/src/instruments.dart
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class InstrumentsController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
InstrumentsController(this.db);
|
||||
|
||||
@Operation.get()
|
||||
Future<Response> getInstruments() async {
|
||||
final instruments = await db.allInstruments().get();
|
||||
return Response.ok(instruments);
|
||||
}
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getInstrument(@Bind.path('id') int id) async {
|
||||
final instrument = await db.instrumentById(id).getSingle();
|
||||
if (instrument != null) {
|
||||
return Response.ok(instrument);
|
||||
} else {
|
||||
return Response.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation.put('id')
|
||||
Future<Response> putInstrument(
|
||||
@Bind.path('id') int id, @Bind.body() Map<String, dynamic> json) async {
|
||||
final instrument = Instrument.fromJson(json).copyWith(
|
||||
id: id,
|
||||
);
|
||||
|
||||
await db.updateInstrument(instrument);
|
||||
|
||||
return Response.ok(null);
|
||||
}
|
||||
}
|
||||
14
server/lib/src/performances.dart
Normal file
14
server/lib/src/performances.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class PerformancesController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
PerformancesController(this.db);
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getPerformances(@Bind.path('id') int id) async {
|
||||
final performances = await db.performancesByRecording(id).get();
|
||||
return Response.ok(performances);
|
||||
}
|
||||
}
|
||||
36
server/lib/src/persons.dart
Normal file
36
server/lib/src/persons.dart
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class PersonsController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
PersonsController(this.db);
|
||||
|
||||
@Operation.get()
|
||||
Future<Response> getPersons() async {
|
||||
final persons = await db.allPersons().get();
|
||||
return Response.ok(persons);
|
||||
}
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getPerson(@Bind.path('id') int id) async {
|
||||
final person = await db.personById(id).getSingle();
|
||||
if (person != null) {
|
||||
return Response.ok(person);
|
||||
} else {
|
||||
return Response.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation.put('id')
|
||||
Future<Response> putPerson(
|
||||
@Bind.path('id') int id, @Bind.body() Map<String, dynamic> json) async {
|
||||
final person = Person.fromJson(json).copyWith(
|
||||
id: id,
|
||||
);
|
||||
|
||||
await db.updatePerson(person);
|
||||
|
||||
return Response.ok(null);
|
||||
}
|
||||
}
|
||||
26
server/lib/src/recordings.dart
Normal file
26
server/lib/src/recordings.dart
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class RecordingsController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
RecordingsController(this.db);
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getRecording(@Bind.path('id') int id) async {
|
||||
final recording = await db.recordingById(id).getSingle();
|
||||
if (recording != null) {
|
||||
return Response.ok(recording);
|
||||
} else {
|
||||
return Response.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation.put('id')
|
||||
Future<Response> putRecording(
|
||||
@Bind.path('id') int id, @Bind.body() Map<String, dynamic> json) async {
|
||||
final data = RecordingData.fromJson(json);
|
||||
await db.updateRecording(data);
|
||||
return Response.ok(null);
|
||||
}
|
||||
}
|
||||
44
server/lib/src/server.dart
Normal file
44
server/lib/src/server.dart
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:moor_ffi/moor_ffi.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
import 'package:musicus_server/src/work_recordings.dart';
|
||||
|
||||
import 'compositions.dart';
|
||||
import 'configuration.dart';
|
||||
import 'ensembles.dart';
|
||||
import 'instruments.dart';
|
||||
import 'performances.dart';
|
||||
import 'persons.dart';
|
||||
import 'recordings.dart';
|
||||
import 'work_parts.dart';
|
||||
import 'works.dart';
|
||||
|
||||
class MusicusServer extends ApplicationChannel {
|
||||
Database db;
|
||||
|
||||
@override
|
||||
Future<void> prepare() async {
|
||||
final config = MusicusServerConfiguration(options.configurationFilePath);
|
||||
|
||||
if (config.dbPath != null) {
|
||||
db = Database(VmDatabase(File(config.dbPath)));
|
||||
} else {
|
||||
db = Database(VmDatabase.memory());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Controller get entryPoint => Router()
|
||||
..route('/persons/[:id]').link(() => PersonsController(db))
|
||||
..route('/persons/:id/works').link(() => CompositionsController(db))
|
||||
..route('/instruments/[:id]').link(() => InstrumentsController(db))
|
||||
..route('/works/:id').link(() => WorksController(db))
|
||||
..route('/works/:id/parts').link(() => WorkPartsController(db))
|
||||
..route('/works/:id/recordings').link(() => WorkRecordingsController(db))
|
||||
..route('/ensembles/[:id]').link(() => EnsemblesController(db))
|
||||
..route('/recordings/:id').link(() => RecordingsController(db))
|
||||
..route('/recordings/:id/performances')
|
||||
.link(() => PerformancesController(db));
|
||||
}
|
||||
14
server/lib/src/work_parts.dart
Normal file
14
server/lib/src/work_parts.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class WorkPartsController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
WorkPartsController(this.db);
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getParts(@Bind.path('id') int id) async {
|
||||
final parts = await db.workParts(id).get();
|
||||
return Response.ok(parts);
|
||||
}
|
||||
}
|
||||
14
server/lib/src/work_recordings.dart
Normal file
14
server/lib/src/work_recordings.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class WorkRecordingsController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
WorkRecordingsController(this.db);
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getRecordings(@Bind.path('id') int id) async {
|
||||
final recordings = await db.recordingsByWork(id).get();
|
||||
return Response.ok(recordings);
|
||||
}
|
||||
}
|
||||
26
server/lib/src/works.dart
Normal file
26
server/lib/src/works.dart
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import 'package:aqueduct/aqueduct.dart';
|
||||
import 'package:musicus_database/musicus_database.dart';
|
||||
|
||||
class WorksController extends ResourceController {
|
||||
final Database db;
|
||||
|
||||
WorksController(this.db);
|
||||
|
||||
@Operation.get('id')
|
||||
Future<Response> getWork(@Bind.path('id') int id) async {
|
||||
final work = await db.workById(id).getSingle();
|
||||
if (work != null) {
|
||||
return Response.ok(work);
|
||||
} else {
|
||||
return Response.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation.put('id')
|
||||
Future<Response> putWork(
|
||||
@Bind.path('id') int id, @Bind.body() Map<String, dynamic> json) async {
|
||||
final data = WorkData.fromJson(json);
|
||||
await db.updateWork(data);
|
||||
return Response.ok(null);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue