1use anyhow::Error;
2
3use crate::{
4 db_client::DbClient, pagination::PaginationParams, response::DeleteResult, DeleteParams, Doc,
5 PaginationResult, PatchParams, RestModel, UpdateResult, UpsertResult,
6};
7
8pub trait Init<T, Db>
9where
10 T: RestModel,
11 Db: DbClient<T>,
12{
13 fn init(client: &Db) -> impl std::future::Future<Output = Result<(), Error>> {
14 async { client.init(T::get_db_name(), T::get_table_name()).await }
15 }
16}
17
18pub trait GetWithId<T, Db>
19where
20 T: RestModel,
21 Db: DbClient<T>,
22{
23 fn get_with_id(
24 client: &Db,
25 id: &str,
26 ) -> impl std::future::Future<Output = Result<Doc<T>, Error>> {
27 async {
28 client
29 .select_by_id(T::get_db_name(), T::get_table_name(), id)
30 .await
31 }
32 }
33}
34
35pub trait Get<T, Db>
36where
37 T: RestModel,
38 Db: DbClient<T>,
39{
40 fn get(
41 client: &Db,
42 pagination_params: &PaginationParams,
43 ) -> impl std::future::Future<Output = Result<PaginationResult<T>, Error>> {
44 async {
45 client
46 .paginate(T::get_db_name(), T::get_table_name(), pagination_params)
47 .await
48 }
49 }
50}
51
52pub trait Put<T, Db>
53where
54 T: RestModel,
55 Db: DbClient<T>,
56{
57 fn put(
58 client: &Db,
59 items: &[Doc<T>],
60 ) -> impl std::future::Future<Output = Result<UpsertResult, Error>> {
61 async {
62 client
63 .upsert(T::get_db_name(), T::get_table_name(), items)
64 .await
65 }
66 }
67}
68
69pub trait Patch<T, Db>
70where
71 T: RestModel,
72 Db: DbClient<T>,
73{
74 fn patch(
75 client: &Db,
76 params: &PatchParams,
77 ) -> impl std::future::Future<Output = Result<UpdateResult, Error>> {
78 async move {
79 client
80 .update(T::get_db_name(), T::get_table_name(), ¶ms)
81 .await
82 }
83 }
84}
85
86pub trait Delete<T, Db>
87where
88 T: RestModel,
89 Db: DbClient<T>,
90{
91 fn delete(
92 client: &Db,
93 params: &DeleteParams,
94 ) -> impl std::future::Future<Output = Result<DeleteResult, Error>> {
95 async move {
96 client
97 .delete(T::get_db_name(), T::get_table_name(), ¶ms)
98 .await
99 }
100 }
101}