Going somewhere... but no idea where.

This commit is contained in:
didier amyot committed 2023-02-25 17:03:52 -05:00
1 parent 0bbe716d8d
commit 89597dcac2
7 files changed
+110 -10

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
use rusqlite::Connection;
use serde::Deserialize;
use serde::Serialize;
use crate::db::external_id::UserId;
use crate::db::users::{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)]
pub struct UserDTO {
name: String,
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 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)
}
}