AngularDart:具有多个集合的内存中数据服务
我一直在遵循,并且有一个in_memory_data_service.dart文件,其中有一个名为_initialHeroes的“集合/表”。但目前的一切都是为了支持这个集合/表格。如何最好地重构in_memory_data_service.dart以支持多个集合/表?我就是这样解决的。只是根据url的路径段(即“位置”或“帖子”)来决定,我应该连接到内存列表中的位置或帖子。在我的例子中是位置或帖子。编码不是很好,但它只是临时的,而且可以工作AngularDart:具有多个集合的内存中数据服务,dart,angular-dart,Dart,Angular Dart,我一直在遵循,并且有一个in_memory_data_service.dart文件,其中有一个名为_initialHeroes的“集合/表”。但目前的一切都是为了支持这个集合/表格。如何最好地重构in_memory_data_service.dart以支持多个集合/表?我就是这样解决的。只是根据url的路径段(即“位置”或“帖子”)来决定,我应该连接到内存列表中的位置或帖子。在我的例子中是位置或帖子。编码不是很好,但它只是临时的,而且可以工作 import 'dart:async'; impo
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:angular/angular.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'src/place.dart';
import 'src/post.dart';
@Injectable()
class InMemoryDataService extends MockClient {
static final _initialPlaces = [
{'id': 11, 'name': 'Place 1'},
{'id': 12, 'name': 'Place 2'},
{'id': 13, 'name': 'Place 3'},
{'id': 14, 'name': 'Place 4'},
{'id': 15, 'name': 'Place 5'},
{'id': 16, 'name': 'Place 6'},
{'id': 17, 'name': 'Place 7'},
{'id': 18, 'name': 'Place 8'},
{'id': 19, 'name': 'Place 9'},
{'id': 20, 'name': 'Place 10'}
];
static final _initialPosts = [
{'id': 23, 'place_id': 11, 'text': 'My first post'},
{'id': 24, 'place_id': 11, 'text': 'My second post'},
{'id': 25, 'place_id': 11, 'text': 'My third post'},
{'id': 26, 'place_id': 11, 'text': 'My fourth post'},
{'id': 27, 'place_id': 11, 'text': 'My fifth post'},
{'id': 28, 'place_id': 11, 'text': 'My sixth post'},
{'id': 29, 'place_id': 11, 'text': 'My seventh post'},
{'id': 30, 'place_id': 11, 'text': 'My eights post'},
{'id': 31, 'place_id': 11, 'text': 'My ninth post'},
{'id': 32, 'place_id': 11, 'text': 'My tenth post'},
{'id': 33, 'place_id': 11, 'text': 'My elevents post'},
];
static List _placesTbl;
static List _postsTbl;
static int _nextPlacesId;
static int _nextPostsId;
static Future _handler(Request request) async {
if (_placesTbl == null) resetDb();
var data;
switch (request.method) {
case 'GET':
final id = int.tryParse(request.url.pathSegments.last);
switch(request.url.pathSegments[1]) {
case 'places':
if (id != null) {
data = _placesTbl
.firstWhere((place) => place.id == id); // throws if no match
} else {
String prefix = request.url.queryParameters['name'] ?? '';
final regExp = new RegExp(prefix, caseSensitive: false);
data = _placesTbl.where((place) => place.name.contains(regExp)).toList();
}
break;
case 'posts':
if (id != null) {
data = _postsTbl
.firstWhere((post) => post.id == id); // throws if no match
} else {
String prefix = request.url.queryParameters['text'] ?? '';
final regExp = new RegExp(prefix, caseSensitive: false);
data = _postsTbl.where((post) => post.text.contains(regExp)).toList();
}
break;
default:
throw 'Unimplemented HTTP method ${request.method}';
}
break;
case 'POST':
switch(request.url.pathSegments[1]) {
case 'places':
var name = json.decode(request.body)['name'];
var newPlace = new Place(_nextPlacesId++, name);
_placesTbl.add(newPlace);
data = newPlace;
break;
case 'posts':
var text = json.decode(request.body)['text'];
var place_id = json.decode(request.body)['place_id'];
var newPost = new Post(_nextPostsId++, place_id, text);
_postsTbl.add(newPost);
data = newPost;
break;
}
break;
case 'PUT':
switch(request.url.pathSegments[1]) {
case 'places':
var placeChanges = new Place.fromJson(json.decode(request.body));
var targetPlace = _placesTbl.firstWhere((h) => h.id == placeChanges.id);
targetPlace.name = placeChanges.name;
data = targetPlace;
break;
case 'posts':
var postChanges = new Post.fromJson(json.decode(request.body));
var targetPost = _postsTbl.firstWhere((h) => h.id == postChanges.id);
targetPost.place_id = postChanges.place_id;
targetPost.text = postChanges.text;
data = targetPost;
break;
}
break;
case 'DELETE':
switch(request.url.pathSegments[1]) {
case 'places':
var id = int.parse(request.url.pathSegments.last);
_placesTbl.removeWhere((place) => place.id == id);
break;
case 'posts':
var id = int.parse(request.url.pathSegments.last);
_postsTbl.removeWhere((post) => post.id == id);
break;
}
// No data, so leave it as null.
break;
default:
throw 'Unimplemented HTTP method ${request.method}';
}
return new Response(json.encode({'data': data}), 200,
headers: {'content-type': 'application/json'});
}
static resetDb() {
_placesTbl = _initialPlaces.map((json) => new Place.fromJson(json)).toList();
_postsTbl = _initialPosts.map((json) => new Post.fromJson(json)).toList();
_nextPlacesId = _placesTbl.map((place) => place.id).fold(0, max) + 1;
_nextPostsId = _postsTbl.map((post) => post.id).fold(0, max) + 1;
}
static String lookUpPlacesName(int id) =>
_placesTbl.firstWhere((place) => place.id == id, orElse: null)?.name;
static String lookUpPostsText(int id) =>
_postsTbl.firstWhere((post) => post.id == id, orElse: null)?.text;
InMemoryDataService() : super(_handler);
}
不确定您试图实现什么。也许是更好的选择?我想为我的应用程序构建一个带有json数据结构的模拟后端。一旦前端达到预期效果,我想使用Aqueduct&Postgresql构建后端,并删除模拟后端。我正在通过redux进行阅读,但不知道它是否真的有帮助。这似乎是个问题帮助分析状态,而不是创建模拟json rest后端。这是gor内存状态,但可能不适合您的目的。无论如何,谢谢。Redux看起来很有趣,但很难理解。我想是时候激活自己的大脑,修改内存中的data service.dart代码,直到它工作为止。