MCPcopy Create free account
hub / github.com/Wulf/create-rust-app / get_sessions

Function get_sessions

crates/create-rust-app/src/auth/controller.rs:129–163  ·  view source on GitHub ↗

/sessions queries [`db`](`Database`) for all sessions owned by the User associated with [`auth`](`Auth`) breaks up the results of that query as defined by [`info`](`PaginationParams`) # Returns [`Result`] - Ok([`UserSessionResponse`]) - the results of the query paginated according to [`info`](`PaginationParams`) - Err([`StatusCode`], [`Message`]) # Errors - 500: Could not fetch sessions # Pan

(
    db: &Database,
    auth: &Auth,
    info: &PaginationParams,
)

Source from the content-addressed store, hash-verified

127///
128/// TODO: don't panic if db connection fails, just return an error
129pub fn get_sessions(
130 db: &Database,
131 auth: &Auth,
132 info: &PaginationParams,
133) -> Result<UserSessionResponse, (StatusCode, Message)> {
134 let mut db = db.get_connection().unwrap();
135
136 let Ok(sessions) = UserSession::read_all(&mut db, info, auth.user_id) else {
137 return Err((500, "Could not fetch sessions."));
138 };
139
140 let sessions_json: Vec<UserSessionJson> = sessions
141 .iter()
142 .map(|s| UserSessionJson {
143 id: s.id,
144 device: s.device.clone(),
145 created_at: s.created_at,
146 #[cfg(not(feature = "database_sqlite"))]
147 updated_at: s.updated_at,
148 })
149 .collect();
150
151 let Ok(num_sessions) = UserSession::count_all(&mut db, auth.user_id) else {
152 return Err((500, "Could not fetch sessions."));
153 };
154
155 let num_pages = (num_sessions / info.page_size) + i64::from(num_sessions % info.page_size != 0);
156
157 let resp = UserSessionResponse {
158 sessions: sessions_json,
159 num_pages,
160 };
161
162 Ok(resp)
163}
164
165/// /sessions/{id}
166///

Callers 2

sessionsFunction · 0.85
sessionsFunction · 0.85

Calls 1

get_connectionMethod · 0.80

Tested by

no test coverage detected