api/services/queries/
get_available_games.rs1use chrono::{DateTime, Utc};
2#[cfg(feature = "server")]
3use dioxus::fullstack::extract::State;
4use dioxus::prelude::*;
5use serde::{Deserialize, Serialize};
6
7#[cfg(feature = "server")]
8use crate::ServerStateExtractor;
9use crate::dto::{AvailableGameDTO, UserIdDTO};
10
11pub type Since = Option<DateTime<Utc>>;
16
17#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
21pub struct AvailableGamesResponse {
22 games: Vec<AvailableGameDTO>,
23 has_more: bool,
24 since: Since,
25}
26
27impl AvailableGamesResponse {
28 pub fn games(&self) -> &[AvailableGameDTO] {
30 &self.games
31 }
32
33 pub fn has_more(&self) -> bool {
35 self.has_more
36 }
37
38 pub fn since(&self) -> &Since {
40 &self.since
41 }
42}
43
44#[get("/api/{user_id}/available_games?filter&since", State(server_state): State<ServerStateExtractor>)]
60pub async fn get_available_games(
61 user_id: UserIdDTO,
62 filter: Option<String>,
63 since: Since,
64) -> Result<AvailableGamesResponse> {
65 use server::{
66 domain::{Availability, UserId},
67 queries::get_available_games,
68 };
69
70 use crate::dto::{AvailabilityDTO, GameIdDTO};
71
72 let user_id = UserId::from(user_id.value());
73 let filter = filter.unwrap_or_default();
74
75 let (games, has_more, since) =
76 get_available_games(server_state.0, user_id, filter, since).await?;
77
78 let games = games
79 .into_iter()
80 .map(|game| {
81 let game_id = GameIdDTO::from(game.id().value());
82 let name = String::from(game.name());
83 let availability = match game.availability() {
84 Availability::Private => AvailabilityDTO::Private,
85 Availability::Public => AvailabilityDTO::Public,
86 };
87 AvailableGameDTO {
88 game_id,
89 name,
90 availability,
91 }
92 })
93 .collect::<Vec<_>>();
94
95 let response = AvailableGamesResponse {
96 games,
97 has_more,
98 since,
99 };
100
101 Ok(response)
102}