1use crate::query_result::QueryResult;
2use paste::paste;
3use pobsd_parser::{Game, Store};
4
5use crate::database::GameDataBase;
6
7macro_rules! get_game_by {
8 (id) => {
9 pub fn get_game_by_id(&self, game_id: u32) -> Option<&Game> {
11 self.games.get(&game_id)
12 }
13 };
14 (ids) => {
15 pub fn get_game_by_ids(&self, game_ids: Vec<u32>) -> QueryResult<&Game> {
17 let mut games: Vec<&Game> = Vec::new();
18 for game_id in game_ids {
19 if let Some(game) = self.get_game_by_id(game_id) {
20 games.push(game);
21 }
22 }
23 games.sort();
24 QueryResult {
25 count: games.len(),
26 items: games,
27 }
28 }
29 };
30 (name) => {
31 pub fn get_game_by_name(&self, name: &str) -> Option<&Game> {
33 for game in self.games.values() {
34 if game.name.eq(name) {
35 return Some(game);
36 }
37 }
38 None
39 }
40 };
41 (steam_id) => {
42 pub fn get_game_by_steam_id(&self, steam_id: usize) -> Option<&Game> {
44 for game in self.games.values() {
45 if let Some(stores) = &game.stores {
46 for store in stores.inner_ref() {
47 if store.store.eq(&Store::Steam) && store.id.eq(&Some(steam_id)) {
48 return Some(game);
49 }
50 }
51 }
52 }
53 None
54 }
55 };
56 ($field:ident) => {
57 paste! {
58 pub fn [<get_game_by_ $field>](&self, field: &str) -> QueryResult<&Game> {
60 match self.[<$field s>].get(field) {
61 Some(game_ids) => {
62 let mut games: Vec<&Game> = Vec::new();
63 for game_id in game_ids {
64 if let Some(game) = self.games.get(game_id) {
65 games.push(game)
66 }
67 }
68 games.sort();
69 QueryResult{
70 count: games.len(),
71 items: games
72 }
73 }
74 None => QueryResult{
75 count: 0,
76 items : vec![]
77 },
78 }
79 }
80 }
81 };
82}
83
84macro_rules! search_game_by {
85 (name) => {
86 pub fn search_game_by_name(&self, name: &str) -> QueryResult<&Game> {
88 let mut games: Vec<&Game> = Vec::new();
89 for game in self.games.values() {
90 if game.name.to_lowercase().contains(&name.to_lowercase()) {
91 games.push(game)
92 }
93 }
94 QueryResult {
95 count: games.len(),
96 items: games,
97 }
98 }
99 };
100 ($field:ident) => {
101 paste! {
102 pub fn [<search_game_by_ $field>](&self, name: &str) -> QueryResult<&Game> {
104 let mut games: Vec<&Game> = Vec::new();
105 for game in self.games.values() {
106 if let Some(value) = &game.$field {
107 if value.to_lowercase().contains(&name.to_lowercase()) {
108 games.push(game);
109 }
110 }
111 }
112 QueryResult {
113 count: games.len(),
114 items: games
115 }
116 }
117 }
118 };
119 (array $field:ident) => {
120 paste! {
121 pub fn [<search_game_by_ $field>](&self, name: &str) -> QueryResult<&Game> {
123 let mut games: Vec<&Game> = Vec::new();
124 for game in self.games.values() {
125 if let Some(value) = &game.$field {
126 if value.join(" ").to_lowercase().contains(&name.to_lowercase()) {
127 games.push(game);
128 }
129 }
130 }
131 QueryResult {
132 count: games.len(),
133 items: games
134 }
135 }
136 }
137 };
138}
139
140macro_rules! get_all {
141 (games) => {
142 pub fn get_all_games(&self) -> QueryResult<&Game> {
144 let mut games: Vec<&Game> = self.games.values().collect();
145 games.sort();
146 QueryResult {
147 count: games.len(),
148 items: games,
149 }
150 }
151 };
152 ($field:ident) => {
153 paste! {
154 pub fn [<get_all_ $field>](&self) -> QueryResult<&String> {
156 let mut items: Vec<&String> = self.$field.keys().collect();
157 items.sort();
158 QueryResult{
159 count: items.len(),
160 items,
161 }
162 }
163 }
164 };
165}
166
167impl GameDataBase {
168 get_all!(games);
170 get_game_by!(id);
171 get_game_by!(ids);
172 get_game_by!(name);
173 get_game_by!(tag);
174 get_game_by!(year);
175 get_game_by!(engine);
176 get_game_by!(runtime);
177 get_game_by!(genre);
178 get_game_by!(dev);
179 get_game_by!(publi);
180 get_game_by!(steam_id);
181
182 search_game_by!(name);
183 search_game_by!(year);
184 search_game_by!(engine);
185 search_game_by!(runtime);
186 search_game_by!(dev);
187 search_game_by!(publi);
188 search_game_by!(array genres);
189 search_game_by!(array tags);
190 get_all!(tags);
192 get_all!(engines);
193 get_all!(runtimes);
194 get_all!(genres);
195 get_all!(years);
196 get_all!(devs);
197 get_all!(publis);
198}