vendor_controller.dart 4.24 KB
Newer Older
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
1
import 'package:aqueduct/aqueduct.dart';
Leon Tappe's avatar
Leon Tappe committed
2

Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
3
4
5
import '../coupons_backend.dart';
import '../model/metadata.dart';
import '../model/vendor.dart';
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
6
7
8
9

class VendorController extends ResourceController {
  final ManagedContext context;

10
11
  VendorController(this.context);

Leon Tappe's avatar
Leon Tappe committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  @Scope(['admin'])
  @Operation.post()
  Future<Response> createVendor() async {
    if (request.body.isEmpty) {
      return Response.badRequest();
    }
    final vendor = Vendor()..read(await request.body.decode(), ignore: ['id']);
    if (vendor.name == null) {
      return Response.badRequest();
    }

    final vendorQuery = Query<Vendor>(context)..values = vendor;

    final insertedVendor = await vendorQuery.insert();

    final now = DateTime.now().toUtc();
    final accessMetaDataVendorQuery = Query<VendorMetadata>(context)
      ..values.changedAt = now
      ..values.createdAt = now
      ..values.vendor = insertedVendor;

33
    final insertedAccessMetaDataVendorQuery = await accessMetaDataVendorQuery.insert();
Leon Tappe's avatar
Leon Tappe committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    if (insertedAccessMetaDataVendorQuery == null) {
      return Response.notFound();
    }
    return Response.ok(insertedVendor);
  }

  @Scope(['admin'])
  @Operation.delete('id')
  Future<Response> deleteVendor(@Bind.path('id') int id) async {
    final deleteQuery = Query<Vendor>(context)..where((v) => v.id).equalTo(id);

    final delete = await deleteQuery.delete();
    if (delete == null) {
      return Response.notFound();
    }

    return Response.ok(delete);
  }

  @override
55
  APIRequestBody documentOperationRequestBody(APIDocumentContext context, Operation operation) {
Leon Tappe's avatar
Leon Tappe committed
56
57
58
59
60
61
62
63
64
65
66
67
68
    if (operation.method == 'POST') {
      return APIRequestBody.schema(context.schema['Vendor']);
    } else if (operation.method == 'PUT') {
      return APIRequestBody.schema(context.schema['Vendor']);
    }
    return null;
  }

  @override
  Map<String, APIResponse> documentOperationResponses(
      APIDocumentContext context, Operation operation) {
    if (operation.method == 'GET') {
      if (operation.pathVariables.contains('id')) {
69
        return {'200': APIResponse.schema('Get a vendor by id', context.schema['Vendor'])};
Leon Tappe's avatar
Leon Tappe committed
70
      } else {
71
        return {'200': APIResponse.schema('All vendor', context.schema['Vendor'])};
Leon Tappe's avatar
Leon Tappe committed
72
73
      }
    } else if (operation.method == 'POST') {
74
      return {'200': APIResponse.schema('Add a vendor', context.schema['Vendor'])};
Leon Tappe's avatar
Leon Tappe committed
75
76
77
78
    }
    return {'400': APIResponse('Unkown error')};
  }

Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
79
  @Scope(['user'])
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
80
  @Operation.get()
Leon Tappe's avatar
Leon Tappe committed
81
82
  Future<Response> getAllVendor({
    @Bind.query('name') String name,
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
83
    @Bind.query('slim') String slim,
Leon Tappe's avatar
Leon Tappe committed
84
  }) async {
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
85
86
87
88
    final vendorQuery = Query<Vendor>(context);
    if (name != null) {
      vendorQuery.where((v) => v.name).contains(name, caseSensitive: false);
    }
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
89
    if (slim == 'true') {
90
      vendorQuery.returningProperties((Vendor v) => <dynamic>[v.id, v.name, v.properties]);
Leon Tappe's avatar
Leon Tappe committed
91
    }
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
92
93
    final vendors = await vendorQuery.fetch();

94
95
96
97
98
99
100
101
    if (slim == 'true') {
      vendors.forEach((Vendor v) {
        if (v.properties?.data != null) {
          (v.properties.data as Map<String, dynamic>).removeWhere(
              (String key, dynamic value) => key == 'image' || key == 'logo' || key == 'product');
        }
      });
    }
102

Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
103
104
105
    return Response.ok(vendors);
  }

Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
106
  @Scope(['user'])
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
107
  @Operation.get('id')
Leon Tappe's avatar
Leon Tappe committed
108
  Future<Response> getVendorByID(@Bind.path('id') int id) async {
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
109
    final vendorQuery = Query<Vendor>(context)..where((v) => v.id).equalTo(id);
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
110
    final vendor = await vendorQuery.fetch();
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
111
112
113
114
115
116
    if (vendor == null) {
      return Response.notFound();
    }
    return Response.ok(vendor);
  }

Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
117
118
  @Scope(['admin'])
  @Operation.put('id')
119
120
  Future<Response> updateVendor(
      @Bind.path('id') int id, @Bind.body(ignore: ['id', 'metadataVendor']) Vendor vendor) async {
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
121
122
123
124
125
126
127
128
129
130
131
132
    final updateQuery = Query<Vendor>(context)
      ..where((v) => v.id).equalTo(id)
      ..values = vendor;

    final update = await updateQuery.updateOne();

    if (update == null) {
      return Response.notFound();
    }

    final now = DateTime.now().toUtc();

Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
133
    final updateMetadataQuery = Query<VendorMetadata>(context)
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
134
135
136
137
138
139
140
141
142
      ..where((m) => m.vendor.id).equalTo(id)
      ..values.changedAt = now;

    final updateMetadata = updateMetadataQuery.updateOne();
    if (updateMetadata == null) {
      return Response.serverError();
    }
    return Response.ok(update);
  }
Daniel Ravi Negi's avatar
Daniel Ravi Negi committed
143
}