quokka_admin/entity/
user.rs1use quokka::{
2 helper::database::{
3 execute, query_all, query_one, query_optional, BaseRepository, PaginationOrder,
4 },
5 state::{Database, FromState},
6};
7
8#[derive(Clone, sqlx::FromRow, serde::Deserialize, serde::Serialize)]
9pub struct User {
10 pub id: i32,
11 pub username: String,
12 pub password: String,
13 pub email: String,
14 pub created_at: time::OffsetDateTime,
15 pub updated_at: time::OffsetDateTime,
16}
17
18#[derive(Clone, FromState)]
19pub struct UserRepository {
20 database: Database,
21}
22
23impl UserRepository {
24 #[query_optional(query = "SELECT * FROM \"user\" WHERE username = {username}")]
25 pub async fn get_user_by_name(&self, username: &str) -> quokka::Result<Option<User>>;
26}
27
28impl BaseRepository for UserRepository {
29 type Entity = User;
30
31 type PkType = i32;
32
33 #[query_all(
34 query = "SELECT * FROM \"user\" ORDER BY {#order_by} {#direction.to_string()} LIMIT {#page_size} OFFSET {#page_size * page}"
35 )]
36 async fn get_entities(
37 &self,
38 page: i32,
39 page_size: i32,
40 order_by: &'static str,
41 direction: PaginationOrder,
42 ) -> quokka::Result<Vec<Self::Entity>>;
43
44 #[query_one(query = "SELECT * FROM \"user\" WHERE id = {pk}")]
45 fn get_entity(&self, pk: Self::PkType) -> quokka::Result<Self::Entity>;
46
47 #[execute(
48 query = "UPDATE \"user\" SET username = {entity.username}, email = {entity.email}, password = {entity.password}, updated_at = NOW() WHERE id = {entity.id}",
49 write
50 )]
51 fn update_entity(&self, entity: Self::Entity) -> quokka::Result<u64>;
52
53 #[query_one(
54 query = "INSERT INTO \"user\" (username, email, password) VALUES ({entity.username}, {entity.email}, {entity.password}) RETURNING *",
55 write
56 )]
57 fn create_entity(&self, entity: Self::Entity) -> quokka::Result<Self::Entity>;
58
59 #[execute(query = "DELETE FROM \"user\" WHERE id = {pk}", write)]
60 fn delete_entity(&self, pk: Self::PkType) -> quokka::Result<u64>;
61}