OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pirate.server; | 5 library pirate.server; |
6 | 6 |
7 import 'dart:convert' show JSON; | |
8 import 'dart:io'; | |
9 import 'package:rpc/rpc.dart'; | 7 import 'package:rpc/rpc.dart'; |
10 | 8 |
11 import '../common/messages.dart'; | 9 import '../common/messages.dart'; |
12 import '../common/utils.dart'; | 10 import '../common/utils.dart'; |
13 | 11 |
14 @ApiClass(version: 'v1') | 12 @ApiClass(version: 'v1') |
15 class PiratesApi { | 13 class PiratesApi { |
16 final Map<int, Pirate> _alivePirates = {}; | 14 final Map<int, Pirate> _pirateCrew = {}; |
17 PirateShanghaier _shanghaier; | 15 final PirateShanghaier _shanghaier = new PirateShanghaier(properPirateNames); |
18 Map<String, List<String>> _properPirates; | |
19 | |
20 PiratesApi() { | |
21 var namesFile = new File( | |
22 Platform.script.resolve('../lib/server/piratenames.json').toFilePath()); | |
23 _properPirates = JSON.decode(namesFile.readAsStringSync()); | |
24 _shanghaier = new PirateShanghaier(_properPirates); | |
25 } | |
26 | 16 |
27 @ApiMethod(method: 'POST', path: 'pirate') | 17 @ApiMethod(method: 'POST', path: 'pirate') |
28 Pirate addPirate(Pirate newPirate) { | 18 Pirate hirePirate(Pirate newPirate) { |
29 // Make sure this is a real pirate... | 19 // Make sure this is a real pirate... |
30 if (!truePirate(newPirate)) { | 20 if (!truePirate(newPirate)) { |
31 throw new BadRequestError( | 21 throw new BadRequestError( |
32 '$newPirate cannot be a pirate. \'Tis not a pirate name!'); | 22 '$newPirate cannot be a pirate. \'Tis not a pirate name!'); |
33 } | 23 } |
34 if (_alivePirates.containsKey(newPirate.toString().hashCode)) { | 24 if (_pirateCrew.containsKey(newPirate.toString().hashCode)) { |
35 throw new BadRequestError( | 25 throw new BadRequestError( |
36 '$newPirate is already part of your crew!'); | 26 '$newPirate is already part of your crew!'); |
37 } | 27 } |
38 | 28 |
39 // Add pirate to store. | 29 // Add pirate to store. |
40 _alivePirates[newPirate.toString().hashCode] = newPirate; | 30 _pirateCrew[newPirate.toString().hashCode] = newPirate; |
41 return newPirate; | 31 return newPirate; |
42 } | 32 } |
43 | 33 |
44 @ApiMethod( | 34 @ApiMethod( |
45 method: 'DELETE', path: 'pirate/{name}/the/{appellation}') | 35 method: 'DELETE', path: 'pirate/{name}/the/{appellation}') |
46 Pirate killPirate(String name, String appellation) { | 36 Pirate firePirate(String name, String appellation) { |
47 var pirate = new Pirate() | 37 var pirate = new Pirate() |
48 ..name = Uri.decodeComponent(name) | 38 ..name = Uri.decodeComponent(name) |
49 ..appellation = Uri.decodeComponent(appellation); | 39 ..appellation = Uri.decodeComponent(appellation); |
50 if (!_alivePirates.containsKey(pirate.toString().hashCode)) { | 40 if (!_pirateCrew.containsKey(pirate.toString().hashCode)) { |
51 throw new NotFoundError( | 41 throw new NotFoundError( |
52 'Could not find pirate \'$pirate\'! Maybe they\'ve abandoned ship!'); | 42 'Could not find pirate \'$pirate\'! Maybe they\'ve abandoned ship!'); |
53 } | 43 } |
54 return _alivePirates.remove(pirate.toString().hashCode); | 44 return _pirateCrew.remove(pirate.toString().hashCode); |
55 } | 45 } |
56 | 46 |
57 @ApiMethod(method: 'GET', path: 'pirates') | 47 @ApiMethod(method: 'GET', path: 'pirates') |
58 List<Pirate> listPirates() { | 48 List<Pirate> listPirates() { |
59 return _alivePirates.values.toList(); | 49 return _pirateCrew.values.toList(); |
60 } | 50 } |
61 | 51 |
62 @ApiMethod(path: 'shanghai') // Default HTTP method is GET. | 52 @ApiMethod(path: 'shanghai') // Default HTTP method is GET. |
63 Pirate shanghaiAPirate() { | 53 Pirate shanghaiAPirate() { |
64 var pirate = _shanghaier.shanghaiAPirate(); | 54 var pirate = _shanghaier.shanghaiAPirate(); |
65 if (pirate == null) { | 55 if (pirate == null) { |
66 throw new InternalServerError('Ran out of pirates!'); | 56 throw new InternalServerError('Ran out of pirates!'); |
67 } | 57 } |
68 _alivePirates[pirate.toString().hashCode] = pirate; | 58 _pirateCrew[pirate.toString().hashCode] = pirate; |
69 return pirate; | 59 return pirate; |
70 } | 60 } |
71 | 61 |
72 @ApiMethod(path: 'proper/pirates') | 62 @ApiMethod(path: 'proper/pirates') |
73 Map<String, List<String>> properPirates() { | 63 Map<String, List<String>> properPirates() { |
74 return _properPirates; | 64 return properPirateNames; |
75 } | 65 } |
76 } | 66 } |
OLD | NEW |