import 'package:aqueduct/aqueduct.dart';
import 'package:musicus_database/musicus_database.dart';

import 'auth.dart';

class InstrumentsController extends ResourceController {
  final Database db;

  InstrumentsController(this.db);

  @Operation.get()
  Future<Response> getInstruments(
      {@Bind.query('p') int page, @Bind.query('s') String search}) async {
    final instruments = await db.getInstruments(page, search);
    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 {
    if (await db.instrumentById(id).getSingle() != null) {
      if (!request.mayEdit) {
        return Response.forbidden();
      }
    } else {
      if (!request.mayUpload) {
        return Response.forbidden();
      }
    }
    
    final instrument = Instrument.fromJson(json).copyWith(
      id: id,
    );

    await db.updateInstrument(instrument);

    return Response.ok(null);
  }

  @Operation.delete('id')
  Future<Response> deleteInstrument(@Bind.path('id') int id) async {
    if (!request.mayDelete) {
      return Response.forbidden();
    }

    await db.deleteInstrument(id);
    return Response.ok(null);
  }
}
