1 module graphql.starwars.data;
2 
3 import std.typecons : Nullable, nullable;
4 
5 import graphql.starwars.types;
6 
7 @safe:
8 
9 Human[string] humanData() {
10 	static Human[string] ret;
11 	static bool created;
12 	if(!created) {
13 		ret["1000"] =new Human("1000", "Luke Skywalker",
14 			["1002", "1003", "2000", "2001"], [4, 5, 6], "Tatooine"
15 		);
16 		ret["1001"] = new Human("1001", "Darth Vader", ["1004"], [4, 5, 6],
17 			"Tatooine"
18 		);
19 		ret["1002"] = new Human("1002", "Han Solo", ["1000", "1003", "2001"],
20 			[4, 5, 6], ""
21 		);
22 		ret["1003"] = new Human("1003", "Leia Organa",
23 			["1000", "1002", "2000", "2001"], [4, 5, 6], "Alderaan"
24 		);
25 		ret["1004"] = new Human("1004", "Wilhuff Tarkin", ["1001"], [4], "");
26 		created = true;
27 	}
28 	return ret;
29 }
30 
31 Droid[string] droidData() {
32 	static Droid[string] ret;
33 	static bool created;
34 	if(!created) {
35 		ret["2000"] = new Droid("2000", "C-3PO",
36 			["1000", "1002", "1003", "2001"], [4, 5, 6], "Protocol"
37 		);
38 		ret["2001"] = new Droid("2001", "R2-D2", ["1000", "1002", "1003"],
39 			[4, 5, 6], "Astromech"
40 		);
41 		created = true;
42 	}
43 	return ret;
44 }
45 
46 Character getCharacter(string id) {
47 	auto h = id in humanData();
48 	if(h) {
49 		return *h;
50 	} else {
51 		auto d = id in droidData();
52 		if(d) {
53 			return *d;
54 		}
55 	}
56 	return null;
57 }
58 
59 Character[] getFriends(Character c) {
60 	import std.array : array;
61 	import std.algorithm.iteration : map;
62 	return c.friendsId.map!(id => getCharacter(id)).array;
63 }
64 
65 Character getHero(Nullable!Episode episode) {
66 	return !episode.isNull() && episode.get() == 5
67 		? humanData()["1000"]
68 		: droidData()["2001"];
69 }
70 
71 Human getHuman(string id) {
72 	auto h = id in humanData();
73 	return h ? *h : null;
74 }
75 
76 Droid getDroid(string id) {
77 	auto d = id in droidData();
78 	return d ? *d : null;
79 }