|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +namespace HotChocolate.Benchmark.Tests.Execution |
| 5 | +{ |
| 6 | + public class CharacterRepository |
| 7 | + { |
| 8 | + private Dictionary<string, ICharacter> _characters; |
| 9 | + private Dictionary<string, Starship> _starships; |
| 10 | + |
| 11 | + public CharacterRepository() |
| 12 | + { |
| 13 | + _characters = CreateCharacters().ToDictionary(t => t.Id); |
| 14 | + _starships = CreateStarships().ToDictionary(t => t.Id); |
| 15 | + } |
| 16 | + |
| 17 | + public ICharacter GetHero(Episode episode) |
| 18 | + { |
| 19 | + if (episode == Episode.Empire) |
| 20 | + { |
| 21 | + return _characters["1000"]; |
| 22 | + } |
| 23 | + return _characters["2001"]; |
| 24 | + } |
| 25 | + |
| 26 | + public ICharacter GetCharacter(string id) |
| 27 | + { |
| 28 | + if (_characters.TryGetValue(id, out ICharacter c)) |
| 29 | + { |
| 30 | + return c; |
| 31 | + } |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + public Human GetHuman(string id) |
| 36 | + { |
| 37 | + if (_characters.TryGetValue(id, out ICharacter c) |
| 38 | + && c is Human h) |
| 39 | + { |
| 40 | + return h; |
| 41 | + } |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + public Droid GetDroid(string id) |
| 46 | + { |
| 47 | + if (_characters.TryGetValue(id, out ICharacter c) |
| 48 | + && c is Droid d) |
| 49 | + { |
| 50 | + return d; |
| 51 | + } |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + public IEnumerable<object> Search(string text) |
| 56 | + { |
| 57 | + foreach(ICharacter character in _characters.Values |
| 58 | + .Where(t => t.Name.Contains(text))) |
| 59 | + { |
| 60 | + yield return character; |
| 61 | + } |
| 62 | + |
| 63 | + foreach(Starship starship in _starships.Values |
| 64 | + .Where(t => t.Name.Contains(text))) |
| 65 | + { |
| 66 | + yield return starship; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static IEnumerable<ICharacter> CreateCharacters() |
| 71 | + { |
| 72 | + yield return new Human |
| 73 | + { |
| 74 | + Id = "1000", |
| 75 | + Name = "Luke Skywalker", |
| 76 | + Friends = new[] { "1002", "1003", "2000", "2001" }, |
| 77 | + AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }, |
| 78 | + HomePlanet = "Tatooine" |
| 79 | + }; |
| 80 | + |
| 81 | + yield return new Human |
| 82 | + { |
| 83 | + Id = "1001", |
| 84 | + Name = "Darth Vader", |
| 85 | + Friends = new[] { "1004" }, |
| 86 | + AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }, |
| 87 | + HomePlanet = "Tatooine" |
| 88 | + }; |
| 89 | + |
| 90 | + yield return new Human |
| 91 | + { |
| 92 | + Id = "1002", |
| 93 | + Name = "Han Solo", |
| 94 | + Friends = new[] { "1000", "1003", "2001" }, |
| 95 | + AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi } |
| 96 | + }; |
| 97 | + |
| 98 | + yield return new Human |
| 99 | + { |
| 100 | + Id = "1003", |
| 101 | + Name = "Leia Organa", |
| 102 | + Friends = new[] { "1000", "1002", "2000", "2001" }, |
| 103 | + AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }, |
| 104 | + HomePlanet = "Alderaan" |
| 105 | + }; |
| 106 | + |
| 107 | + yield return new Human |
| 108 | + { |
| 109 | + Id = "1004", |
| 110 | + Name = "Wilhuff Tarkin", |
| 111 | + Friends = new[] { "1001" }, |
| 112 | + AppearsIn = new[] { Episode.NewHope } |
| 113 | + }; |
| 114 | + |
| 115 | + yield return new Droid |
| 116 | + { |
| 117 | + Id = "2000", |
| 118 | + Name = "C-3PO", |
| 119 | + Friends = new[] { "1000", "1002", "1003", "2001" }, |
| 120 | + AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }, |
| 121 | + PrimaryFunction = "Protocol" |
| 122 | + }; |
| 123 | + |
| 124 | + yield return new Droid |
| 125 | + { |
| 126 | + Id = "2001", |
| 127 | + Name = "R2-D2", |
| 128 | + Friends = new[] { "1000", "1002", "1003" }, |
| 129 | + AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }, |
| 130 | + PrimaryFunction = "Astromech" |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + private static IEnumerable<Starship> CreateStarships() |
| 135 | + { |
| 136 | + yield return new Starship |
| 137 | + { |
| 138 | + Id = "3000", |
| 139 | + Name = "TIE Advanced x1", |
| 140 | + Length = 9.2 |
| 141 | + }; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments