69 lines
1.5 KiB
Rust
69 lines
1.5 KiB
Rust
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<T>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
total_number_of_page: usize,
|
|
current_page_number: usize,
|
|
content: Vec<T>,
|
|
}
|
|
|
|
impl<T> Page<T>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
pub fn all_of(data: Vec<T>) -> Page<T> {
|
|
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<Option<UserDTO>> {
|
|
let ret = get_user_by_name(self.conn, username)?;
|
|
Ok(ret.as_ref().map(UserDTO::from_entity))
|
|
}
|
|
|
|
pub fn list_users(self) -> CustomResult<Page<UserDTO>> {
|
|
let users = list_users(self.conn)?
|
|
.iter()
|
|
.map(UserDTO::from_entity)
|
|
.collect();
|
|
let ret = Page::all_of(users);
|
|
return Ok(ret);
|
|
}
|
|
}
|