|
|
@ -35,8 +35,10 @@ pub type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Resu
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct Note {
|
|
|
|
pub struct Note {
|
|
|
|
pub id: i32,
|
|
|
|
pub id: i32,
|
|
|
|
|
|
|
|
pub uuid: String,
|
|
|
|
pub content: String,
|
|
|
|
pub content: String,
|
|
|
|
pub list_id: i32,
|
|
|
|
pub list_id: i32,
|
|
|
|
|
|
|
|
pub writer_id: i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl ToString for Note {
|
|
|
|
impl ToString for Note {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
@ -101,6 +103,7 @@ impl<'a> FromRequest<'a> for User {
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct List {
|
|
|
|
pub struct List {
|
|
|
|
pub id: i32,
|
|
|
|
pub id: i32,
|
|
|
|
|
|
|
|
pub uuid: String,
|
|
|
|
pub name: String,
|
|
|
|
pub name: String,
|
|
|
|
pub owner_id: i32,
|
|
|
|
pub owner_id: i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -109,10 +112,44 @@ impl ToString for List {
|
|
|
|
format!("{}: {} (owned by {})", self.id, self.name, self.owner_id)
|
|
|
|
format!("{}: {} (owned by {})", self.id, self.name, self.owner_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl List {
|
|
|
|
|
|
|
|
pub async fn from_uuid(list_uuid: String, db: &mut PoolConnection<Postgres>) -> Result<Option<List>> {
|
|
|
|
|
|
|
|
match sqlx::query!("
|
|
|
|
|
|
|
|
SELECT * FROM list WHERE uuid = $1", list_uuid)
|
|
|
|
|
|
|
|
.fetch_optional(db)
|
|
|
|
|
|
|
|
.await? {
|
|
|
|
|
|
|
|
Some(list) => Ok(Some(List {
|
|
|
|
|
|
|
|
id: list.id,
|
|
|
|
|
|
|
|
uuid: list.uuid,
|
|
|
|
|
|
|
|
name: list.name.clone(),
|
|
|
|
|
|
|
|
owner_id: list.owner_id,
|
|
|
|
|
|
|
|
})),
|
|
|
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_notes_from_list(list_id: i32, db: &mut PoolConnection<Postgres>) -> Result<Vec<Note>> {
|
|
|
|
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
|
|
|
|
FROM note
|
|
|
|
|
|
|
|
WHERE list_id = $1", list_id)
|
|
|
|
|
|
|
|
.fetch_all(db)
|
|
|
|
|
|
|
|
.await?
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.map(|r| Note {
|
|
|
|
|
|
|
|
id: r.id,
|
|
|
|
|
|
|
|
uuid: r.uuid.clone(),
|
|
|
|
|
|
|
|
list_id: r.list_id,
|
|
|
|
|
|
|
|
content: r.content.clone(),
|
|
|
|
|
|
|
|
writer_id: r.writer_id,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.collect())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_list_notes(db: &mut PoolConnection<Postgres>, lid: i32) -> Result<Vec<Note>> {
|
|
|
|
pub async fn get_list_notes(db: &mut PoolConnection<Postgres>, lid: i32) -> Result<Vec<Note>> {
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
SELECT id,content,list_id
|
|
|
|
SELECT id,content,list_id,uuid,writer_id
|
|
|
|
FROM note
|
|
|
|
FROM note
|
|
|
|
WHERE list_id = $1", lid)
|
|
|
|
WHERE list_id = $1", lid)
|
|
|
|
.fetch_all(db)
|
|
|
|
.fetch_all(db)
|
|
|
@ -120,31 +157,34 @@ pub async fn get_list_notes(db: &mut PoolConnection<Postgres>, lid: i32) -> Resu
|
|
|
|
.iter()
|
|
|
|
.iter()
|
|
|
|
.map(|r| Note {
|
|
|
|
.map(|r| Note {
|
|
|
|
id: r.id,
|
|
|
|
id: r.id,
|
|
|
|
|
|
|
|
uuid: r.uuid.clone(),
|
|
|
|
content: r.content.clone(),
|
|
|
|
content: r.content.clone(),
|
|
|
|
list_id: r.list_id
|
|
|
|
list_id: r.list_id,
|
|
|
|
|
|
|
|
writer_id: r.writer_id,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect())
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_user_lists(db: &mut PoolConnection<Postgres>, uid: i32) -> Result<Vec<List>> {
|
|
|
|
pub async fn get_user_lists(db: &mut PoolConnection<Postgres>, uid: i32) -> Result<Vec<List>> {
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
SELECT id,name,owner_id
|
|
|
|
SELECT id,name,owner_id,uuid
|
|
|
|
FROM list
|
|
|
|
FROM list
|
|
|
|
WHERE owner_id = $1", uid)
|
|
|
|
WHERE owner_id = $1", uid)
|
|
|
|
.fetch_all(db)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await?
|
|
|
|
.await?
|
|
|
|
.iter()
|
|
|
|
.iter()
|
|
|
|
.map(|r| List {
|
|
|
|
.map(|r| List {
|
|
|
|
name: r.name.clone(),
|
|
|
|
|
|
|
|
id: r.id,
|
|
|
|
id: r.id,
|
|
|
|
owner_id: r.owner_id
|
|
|
|
uuid: r.uuid.clone(),
|
|
|
|
|
|
|
|
owner_id: r.owner_id,
|
|
|
|
|
|
|
|
name: r.name.clone(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect())
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_user_lists_from_perms(db: &mut PoolConnection<Postgres>, uid: i32) -> Result<Vec<List>> {
|
|
|
|
pub async fn get_user_lists_from_perms(db: &mut PoolConnection<Postgres>, uid: i32) -> Result<Vec<List>> {
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
Ok(sqlx::query!("
|
|
|
|
SELECT list.id,list.name,list.owner_id
|
|
|
|
SELECT list.id,list.name,list.owner_id,uuid
|
|
|
|
FROM list
|
|
|
|
FROM list
|
|
|
|
JOIN perms ON list.id=perms.list_id
|
|
|
|
JOIN perms ON list.id=perms.list_id
|
|
|
|
WHERE perms.user_id = $1
|
|
|
|
WHERE perms.user_id = $1
|
|
|
@ -155,11 +195,22 @@ pub async fn get_user_lists_from_perms(db: &mut PoolConnection<Postgres>, uid: i
|
|
|
|
.map(|r| List {
|
|
|
|
.map(|r| List {
|
|
|
|
name: r.name.clone(),
|
|
|
|
name: r.name.clone(),
|
|
|
|
id: r.id,
|
|
|
|
id: r.id,
|
|
|
|
|
|
|
|
uuid: r.uuid.clone(),
|
|
|
|
owner_id: r.owner_id
|
|
|
|
owner_id: r.owner_id
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect())
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn add_note_to_list(db: &mut PoolConnection<Postgres>, list_id: i32, content: String, writer_id: i32) -> Result<bool> {
|
|
|
|
|
|
|
|
match sqlx::query!(
|
|
|
|
|
|
|
|
"INSERT INTO note (list_id, writer_id, content) VALUES ($1, $2, $3)", list_id, writer_id, content)
|
|
|
|
|
|
|
|
.execute(db)
|
|
|
|
|
|
|
|
.await {
|
|
|
|
|
|
|
|
Ok(_) => Ok(true),
|
|
|
|
|
|
|
|
Err(e) => Ok(false),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_user_from_email(db: &mut PoolConnection<Postgres>, email: String) -> Result<Option<User>> {
|
|
|
|
pub async fn get_user_from_email(db: &mut PoolConnection<Postgres>, email: String) -> Result<Option<User>> {
|
|
|
|
match sqlx::query!(
|
|
|
|
match sqlx::query!(
|
|
|
|
"SELECT * FROM users WHERE email = $1",
|
|
|
|
"SELECT * FROM users WHERE email = $1",
|
|
|
|