feature/share-libs
rs 285 lines 8.41 KB
Raw
1 use crate::{
2 command::constants::{
3 base_url_builder, PATH_CAROUSEL, PATH_READY_TO_PLAY, PATH_SONG_BRIDGE_DETAIL,
4 PATH_SONG_BRIDGE_SPLIT, PATH_SONG_BRIDGE_VOTE, PATH_TOP_VOTES,
5 },
6 models::{
7 content::{
8 CarouselResponse, ContentCarouselResponse, ContentSongProviderResponse,
9 ContentSplitResponse, SongProviderResponse, SplitResponse, VoteType,
10 },
11 player::TauriResponse,
12 },
13 };
14 use log::{debug, error};
15 use reqwest::Client;
16 use serde_json::json;
17
18 #[tauri::command]
19 pub async fn content_carousel() -> ContentCarouselResponse {
20 let response = Client::new()
21 .get(content_url_builder(PATH_CAROUSEL))
22 .send()
23 .await;
24
25 let response = match response {
26 Ok(response) => response,
27 Err(e) => {
28 error!("Failed to get response: {:?}", e);
29 return ContentCarouselResponse {
30 status: TauriResponse::Error,
31 message: e.to_string(),
32 audio_files: vec![],
33 };
34 }
35 };
36 let res: CarouselResponse = match response.json().await {
37 Ok(json) => json,
38 Err(e) => {
39 error!("Failed to parse response: {:?}", e);
40 return ContentCarouselResponse {
41 status: TauriResponse::Error,
42 message: e.to_string(),
43 audio_files: vec![],
44 };
45 }
46 };
47 debug!("Carousel response: {:?}", res);
48 ContentCarouselResponse {
49 status: TauriResponse::Success,
50 message: res.message,
51 audio_files: res.audio_files,
52 }
53 }
54
55 #[tauri::command]
56 pub async fn content_ready_to_play() -> ContentCarouselResponse {
57 let response = Client::new()
58 .get(content_url_builder(PATH_READY_TO_PLAY))
59 .send()
60 .await;
61
62 let response = match response {
63 Ok(response) => response,
64 Err(e) => {
65 debug!("Failed to get response: {:?}", e);
66 return ContentCarouselResponse {
67 status: TauriResponse::Error,
68 message: e.to_string(),
69 audio_files: vec![],
70 };
71 }
72 };
73 let res: CarouselResponse = match response.json().await {
74 Ok(json) => json,
75 Err(e) => {
76 debug!("Failed to parse response: {:?}", e);
77 return ContentCarouselResponse {
78 status: TauriResponse::Error,
79 message: e.to_string(),
80 audio_files: vec![],
81 };
82 }
83 };
84
85 debug!("Ready to play: {:?}", res);
86 ContentCarouselResponse {
87 status: TauriResponse::Success,
88 message: res.message,
89 audio_files: res.audio_files,
90 }
91 }
92
93 #[tauri::command]
94 pub async fn content_song_bridge_detail(song_provider_id: String) -> ContentSongProviderResponse {
95 // song_provider_id is a string because it is comes from a nextjs query parameter.
96 // When we get it from the server, it actually is a number.
97 // It is Rails id convention.
98 debug!("Song provider id: {:?}", song_provider_id);
99 let url =
100 content_url_builder(PATH_SONG_BRIDGE_DETAIL).replace("{providerId}", &song_provider_id);
101 let response: Result<reqwest::Response, reqwest::Error> = Client::new().get(url).send().await;
102
103 let response = match response {
104 Ok(response) => response,
105 Err(e) => {
106 error!("Failed to get response: {:?}", e);
107 return ContentSongProviderResponse {
108 status: TauriResponse::Error,
109 message: e.to_string(),
110 song_provider: None,
111 votes: vec![],
112 };
113 }
114 };
115
116 let res: SongProviderResponse = match response.json().await {
117 Ok(json) => json,
118 Err(e) => {
119 error!("Failed to parse response: {:?}", e);
120 return ContentSongProviderResponse {
121 status: TauriResponse::Error,
122 message: e.to_string(),
123 song_provider: None,
124 votes: vec![],
125 };
126 }
127 };
128
129 debug!("Song bridge detail: {:?}", res);
130
131 let votes = match res.votes {
132 Some(votes) => votes,
133 None => vec![],
134 };
135
136 ContentSongProviderResponse {
137 status: TauriResponse::Success,
138 message: res.message,
139 song_provider: res.song_provider,
140 votes,
141 }
142 }
143
144 #[tauri::command]
145 pub async fn content_top_voted() -> ContentCarouselResponse {
146 let response = Client::new()
147 .get(content_url_builder(PATH_TOP_VOTES))
148 .send()
149 .await;
150
151 let response = match response {
152 Ok(response) => response,
153 Err(e) => {
154 error!("Failed to get response: {:?}", e);
155 return ContentCarouselResponse {
156 status: TauriResponse::Error,
157 message: e.to_string(),
158 audio_files: vec![],
159 };
160 }
161 };
162 let res: CarouselResponse = match response.json().await {
163 Ok(json) => json,
164 Err(e) => {
165 error!("Failed to parse response: {:?}", e);
166 return ContentCarouselResponse {
167 status: TauriResponse::Error,
168 message: e.to_string(),
169 audio_files: vec![],
170 };
171 }
172 };
173 debug!("Top voted response: {:?}", res);
174 ContentCarouselResponse {
175 status: TauriResponse::Success,
176 message: res.message,
177 audio_files: res.audio_files,
178 }
179 }
180
181 #[tauri::command]
182 pub async fn content_song_bridge_vote(
183 song_provider_id: i32,
184 vote_type: VoteType,
185 access_token: String,
186 ) -> ContentSongProviderResponse {
187 debug!("Song provider id: {:?}", song_provider_id);
188 let url = content_url_builder(PATH_SONG_BRIDGE_VOTE)
189 .replace("{providerId}", &song_provider_id.to_string());
190 let body = json!({
191 "vote_type": vote_type,
192 "provider_id": song_provider_id
193 });
194 let response: Result<reqwest::Response, reqwest::Error> = Client::new()
195 .post(url)
196 .json(&body)
197 .header("Authorization", format!("Bearer {}", access_token))
198 .send()
199 .await;
200
201 let response = match response {
202 Ok(response) => response,
203 Err(e) => {
204 error!("Failed to get response: {:?}", e);
205 return ContentSongProviderResponse {
206 status: TauriResponse::Error,
207 message: e.to_string(),
208 song_provider: None,
209 votes: vec![],
210 };
211 }
212 };
213
214 let res: SongProviderResponse = match response.json().await {
215 Ok(json) => json,
216 Err(e) => {
217 error!("Failed to parse response: {:?}", e);
218 return ContentSongProviderResponse {
219 status: TauriResponse::Error,
220 message: e.to_string(),
221 song_provider: None,
222 votes: vec![],
223 };
224 }
225 };
226
227 debug!("Song bridge detail: {:?}", res);
228
229 let votes = match res.votes {
230 Some(votes) => votes,
231 None => vec![],
232 };
233
234 ContentSongProviderResponse {
235 status: TauriResponse::Success,
236 message: res.message,
237 song_provider: res.song_provider,
238 votes,
239 }
240 }
241
242 #[tauri::command]
243 pub async fn content_song_bridge_split(song_provider_id: i32) -> ContentSplitResponse {
244 let url = content_url_builder(PATH_SONG_BRIDGE_SPLIT)
245 .replace("{providerId}", &song_provider_id.to_string());
246 let response: Result<reqwest::Response, reqwest::Error> = Client::new().get(url).send().await;
247
248 let response = match response {
249 Ok(response) => response,
250 Err(e) => {
251 error!("Failed to get response: {:?}", e);
252 return ContentSplitResponse {
253 status: TauriResponse::Error,
254 message: e.to_string(),
255 audio_file: None,
256 };
257 }
258 };
259
260 let res: SplitResponse = match response.json().await {
261 Ok(json) => json,
262 Err(e) => {
263 error!("Failed to parse response: {:?}", e);
264 return ContentSplitResponse {
265 status: TauriResponse::Error,
266 message: e.to_string(),
267 audio_file: None,
268 };
269 }
270 };
271
272 debug!("Song bridge split: {:?}", res);
273
274 ContentSplitResponse {
275 status: TauriResponse::Success,
276 message: res.message,
277 audio_file: res.audio_file,
278 }
279 }
280
281 fn content_url_builder(path: &str) -> String {
282 let mut url_builder = base_url_builder();
283 url_builder.add_route(path);
284 url_builder.build()
285 }