use rusqlite::Connection; use serde::Deserialize; use serde::Serialize; use crate::db::users::{get_user_by_name, list_users, User}; use crate::util::CustomResult; #[derive(Serialize, Debug)] pub struct Page where T: Serialize, { total_number_of_page: usize, current_page_number: usize, content: Vec, } impl Page where T: Serialize, { pub fn all_of(data: Vec) -> Page { Page { total_number_of_page: 0, current_page_number: 10, content: data, } } } #[derive(Deserialize, Serialize, Debug, Clone)] pub struct UserDTO { pub name: String, pub id: String, // todo: should be of type UserId } impl UserDTO { fn from_entity(user: &User) -> UserDTO { return UserDTO { name: user.get_name(), id: user.id().to_string(), }; } } pub struct UserService<'a> { conn: &'a Connection, } impl UserService<'_> { pub fn new(conn: &Connection) -> UserService { UserService { conn } } pub fn get_user_for_authentication(&self, username: &String, _credentials: &String) -> CustomResult> { let ret = get_user_by_name(self.conn, username)?; Ok(ret.as_ref().map(UserDTO::from_entity)) } pub fn list_users(self) -> CustomResult> { let users = list_users(self.conn)? .iter() .map(UserDTO::from_entity) .collect(); let ret = Page::all_of(users); return Ok(ret); } }