/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,
)
| 127 | /// |
| 128 | /// TODO: don't panic if db connection fails, just return an error |
| 129 | pub 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 | /// |
no test coverage detected