Skip to content

Commit a2812ba

Browse files
authored
Merge pull request #81 from Nesteo/inspection-preview-api
Inspection preview api
2 parents c57626b + 7109206 commit a2812ba

File tree

5 files changed

+116
-8
lines changed

5 files changed

+116
-8
lines changed

lib/backend/repositories/inspections_repository.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@ class InspectionsRepository {
3434
return null;
3535
}
3636
}
37+
38+
Future<Inspection> getInspectionPreviewById(int id) async {
39+
final response = await inspectionsApi.getInspectionPreviewById(id);
40+
if (response.statusCode == 200) {
41+
print(response.body);
42+
final Map result = json.decode(response.body);
43+
return Inspection.previewFromJson(result);
44+
} else {
45+
print('Request failed');
46+
return null;
47+
}
48+
}
49+
50+
Future<List<Inspection>> getAllInspectionPreviews() async {
51+
final response = await inspectionsApi.getAllInspections();
52+
if (response.statusCode == 200) {
53+
print(response.body);
54+
final List results = json.decode(response.body);
55+
return results
56+
.map((inspection) => Inspection.previewFromJson(inspection))
57+
.toList();
58+
} else {
59+
print('Request failed');
60+
return null;
61+
}
62+
}
3763
}

lib/backend/services/inspections/inspections_api_service.chopper.dart

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

lib/backend/services/inspections/inspections_api_service.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@ part 'inspections_api_service.chopper.dart';
77

88
@ChopperApi(baseUrl: '/inspections')
99
abstract class InspectionsApiService extends ChopperService {
10-
@Get(headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='})
10+
@Get(
11+
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
12+
)
1113
Future<Response> getAllInspections();
1214

13-
@Get(path: '/{id}', headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='})
15+
@Get(
16+
path: '/{id}',
17+
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
18+
)
1419
Future<Response> getInspectionById(@Path('id') int id);
1520

21+
@Get(
22+
path: '/previews',
23+
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
24+
)
25+
Future<Response> getAllInspectionPreviews();
26+
27+
@Get(
28+
path: '/previews/{id}',
29+
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
30+
)
31+
Future<Response> getInspectionPreviewById(@Path('id') int id);
32+
1633
static InspectionsApiService create() {
1734
final client = ChopperClient(
1835
baseUrl: 'http://${Dev.host}/api/v1',

lib/model/inspection.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Inspection extends Equatable {
2323
final String imageFileName;
2424
final String comment;
2525
final String lastUpdated;
26+
final String nestingBoxId;
27+
final bool isPreview;
2628

2729
Inspection({
2830
this.id,
@@ -44,6 +46,8 @@ class Inspection extends Equatable {
4446
this.imageFileName,
4547
this.comment,
4648
this.lastUpdated,
49+
this.nestingBoxId,
50+
this.isPreview,
4751
});
4852

4953
@override
@@ -67,6 +71,8 @@ class Inspection extends Equatable {
6771
imageFileName,
6872
comment,
6973
lastUpdated,
74+
nestingBoxId,
75+
isPreview,
7076
];
7177

7278
@override
@@ -97,6 +103,21 @@ class Inspection extends Equatable {
97103
imageFileName: json['imageFileName'],
98104
comment: json['comment'],
99105
lastUpdated: json['lastUpdated'],
106+
nestingBoxId: json['nestingBox']['id'],
107+
isPreview: false,
108+
);
109+
}
110+
111+
factory Inspection.previewFromJson(Map<String, dynamic> json) {
112+
return new Inspection(
113+
id: json['id'],
114+
nestingBoxId: json['nestingBoxId'],
115+
inspectionDate: json['inspectionDate'],
116+
condition: json['condition'],
117+
ringedChickCount: json['ringedChickCount'],
118+
femaleParentBirdDiscovery: json['femaleParentBirdDiscovery'],
119+
maleParentBirdDiscovery: json['maleParentBirdDiscovery'],
120+
isPreview: true,
100121
);
101122
}
102123
}

test/api_test.dart

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,23 @@ void main() {
9595
});
9696
group('Species API tests', () {
9797
SpeciesRepository speciesRepo;
98+
int speciesId;
9899

99100
setUp(() {
100101
speciesRepo = SpeciesRepository();
101102
});
102103

103104
test('Test /species', () async {
104105
List<Species> species = await speciesRepo.getAllSpecies();
106+
if (species[0] != null) {
107+
speciesId = species[0].id;
108+
}
105109
print(species.toString());
106110
expect(species.length > 0, true);
107111
});
108112

109113
test('Test /species/{id}', () async {
110-
Species species = await speciesRepo.getSpeciesById(0);
114+
Species species = await speciesRepo.getSpeciesById(speciesId);
111115
if (species != null) {
112116
print(species.toString());
113117
}
@@ -166,23 +170,49 @@ void main() {
166170
});
167171
group('Inspection API tests', () {
168172
InspectionsRepository inspectionRepo;
173+
int inspectionId;
169174

170175
setUp(() {
171176
inspectionRepo = InspectionsRepository();
172177
});
173178

174179
test('Test /inspections', () async {
175-
List<Inspection> inspectiones =
176-
await inspectionRepo.getAllInspections();
177-
print(inspectiones.toString());
178-
expect(inspectiones.length > 0, true);
180+
List<Inspection> inspections = await inspectionRepo.getAllInspections();
181+
print(inspections.toString());
182+
if (inspections[0] != null) {
183+
inspectionId = inspections[0].id;
184+
expect(inspections[0].isPreview, false);
185+
}
186+
expect(inspections.length > 0, true);
179187
});
180188

181189
test('Test /inspections/{id}', () async {
182-
Inspection inspection = await inspectionRepo.getInspectionById(0);
190+
Inspection inspection =
191+
await inspectionRepo.getInspectionById(inspectionId);
192+
if (inspection != null) {
193+
print(inspection.toString());
194+
expect(inspection.isPreview, false);
195+
}
196+
});
197+
198+
test('Test /inspections/previews', () async {
199+
List<Inspection> inspections =
200+
await inspectionRepo.getAllInspectionPreviews();
201+
print(inspections.toString());
202+
if (inspections[0] != null) {
203+
inspectionId = inspections[0].id;
204+
}
205+
expect(inspections.length > 0, true);
206+
expect(inspections[0].isPreview, true);
207+
});
208+
209+
test('Test /inspections/previews/{id}', () async {
210+
Inspection inspection =
211+
await inspectionRepo.getInspectionPreviewById(inspectionId);
183212
if (inspection != null) {
184213
print(inspection.toString());
185214
}
215+
expect(inspection.isPreview, true);
186216
});
187217
});
188218
});

0 commit comments

Comments
 (0)