|
@@ -0,0 +1,52 @@
|
|
|
+use std::vec::Vec;
|
|
|
+
|
|
|
+use crate::boot::db;
|
|
|
+use crate::module::user::model::User;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试接口: 查 列表
|
|
|
+ */
|
|
|
+pub async fn list() -> Vec<User> {
|
|
|
+ let pool = db::get_pool().unwrap();
|
|
|
+ sqlx::query_as::<_, User>("select id, email, username, password, avatar, created_at from users")
|
|
|
+ .fetch_all(pool).await.unwrap()
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试接口: 增
|
|
|
+ */
|
|
|
+pub async fn create(user: User) -> u64 {
|
|
|
+ let pool = db::get_pool().unwrap();
|
|
|
+ sqlx::query("insert into users(email, username, password, avatar) values($1, $2, $3, $4)")
|
|
|
+ .bind(&user.email).bind(&user.username).bind(&user.password)
|
|
|
+ .bind(&user.avatar).execute(pool).await.unwrap().rows_affected()
|
|
|
+ // TODO: 1.最后插入的 主键 值; 2.最终使用 雪花 多数据中心算法
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试接口: 查
|
|
|
+ */
|
|
|
+pub async fn show(id: i32) -> User {
|
|
|
+ let pool = db::get_pool().unwrap();
|
|
|
+ sqlx::query_as::<_, User>("select * from users where id = $1")
|
|
|
+ .bind(id).fetch_one(pool).await.unwrap()
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试接口: 改
|
|
|
+ */
|
|
|
+pub async fn update(id: i32, user: User) -> u64 {
|
|
|
+ let pool = db::get_pool().unwrap();
|
|
|
+ let pg_done = sqlx::query("update users set password = $1 where id = $2")
|
|
|
+ .bind(&user.password).bind(id.clone()).execute(pool).await.unwrap();
|
|
|
+ println!("Hello {}! id: {}. result: {:?}", user.username, id, pg_done.rows_affected());
|
|
|
+ return pg_done.rows_affected();
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试接口: 删
|
|
|
+ */
|
|
|
+pub async fn delete(id: i32) -> u64 {
|
|
|
+ let pool = db::get_pool().unwrap();
|
|
|
+ sqlx::query("delete from users where id = $1").bind(id).execute(pool).await.unwrap().rows_affected()
|
|
|
+}
|