You can now add notes to a list

master
Tait Hoyem 2 years ago
parent 935ffc0a1b
commit 2142a85dc7

@ -35,8 +35,10 @@ pub type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Resu
#[derive(Serialize)]
pub struct Note {
pub id: i32,
pub uuid: String,
pub content: String,
pub list_id: i32,
pub writer_id: i32,
}
impl ToString for Note {
fn to_string(&self) -> String {
@ -101,6 +103,7 @@ impl<'a> FromRequest<'a> for User {
#[derive(Serialize)]
pub struct List {
pub id: i32,
pub uuid: String,
pub name: String,
pub owner_id: i32,
}
@ -109,10 +112,44 @@ impl ToString for List {
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>> {
Ok(sqlx::query!("
SELECT id,content,list_id
SELECT id,content,list_id,uuid,writer_id
FROM note
WHERE list_id = $1", lid)
.fetch_all(db)
@ -120,31 +157,34 @@ pub async fn get_list_notes(db: &mut PoolConnection<Postgres>, lid: i32) -> Resu
.iter()
.map(|r| Note {
id: r.id,
uuid: r.uuid.clone(),
content: r.content.clone(),
list_id: r.list_id
list_id: r.list_id,
writer_id: r.writer_id,
})
.collect())
}
pub async fn get_user_lists(db: &mut PoolConnection<Postgres>, uid: i32) -> Result<Vec<List>> {
Ok(sqlx::query!("
SELECT id,name,owner_id
SELECT id,name,owner_id,uuid
FROM list
WHERE owner_id = $1", uid)
.fetch_all(db)
.await?
.iter()
.map(|r| List {
name: r.name.clone(),
id: r.id,
owner_id: r.owner_id
uuid: r.uuid.clone(),
owner_id: r.owner_id,
name: r.name.clone(),
})
.collect())
}
pub async fn get_user_lists_from_perms(db: &mut PoolConnection<Postgres>, uid: i32) -> Result<Vec<List>> {
Ok(sqlx::query!("
SELECT list.id,list.name,list.owner_id
SELECT list.id,list.name,list.owner_id,uuid
FROM list
JOIN perms ON list.id=perms.list_id
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 {
name: r.name.clone(),
id: r.id,
uuid: r.uuid.clone(),
owner_id: r.owner_id
})
.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>> {
match sqlx::query!(
"SELECT * FROM users WHERE email = $1",

@ -25,3 +25,9 @@ pub struct PermsForm<'a> {
pub perm: i32,
}
#[derive(FromForm)]
pub struct NewNoteForm<'a> {
pub content: &'a str,
pub list_uuid: &'a str,
}

@ -16,15 +16,19 @@ use forms::{
NewUserForm,
NewListForm,
PermsForm,
NewNoteForm,
};
use db::{
List,
Notes,
Note,
Result,
add_permission,
get_user_from_email,
get_user_lists,
get_user_lists_from_perms,
get_notes_from_list,
add_note_to_list,
User,
};
@ -45,6 +49,7 @@ use rocket::{
http::{
Cookie,
CookieJar,
RawStr,
},
};
@ -133,6 +138,23 @@ async fn new_list_form(_user: User) -> Template {
async fn new_list_form_not_logged_in() -> Redirect {
Redirect::to(uri!(home))
}
#[get("/new/note/<list_uuid>")]
async fn new_note_form(mut db: Connection<Notes>, user: User, list_uuid: String) -> Result<Template> {
Ok(Template::render("new_note_form", context!{
list_uuid: list_uuid
}))
}
#[post("/note", data="<note>")]
async fn new_note(mut db: Connection<Notes>, user: User, note: Form<NewNoteForm<'_>>) -> Result<String> {
let list = match List::from_uuid(note.list_uuid.to_string(), &mut *db).await? {
Some(l) => l,
None => return Ok(format!("List not found!")),
};
match add_note_to_list(&mut *db, list.id, note.content.to_string(), user.id).await {
Ok(_) => Ok(format!("Added note!")),
Err(_) => Ok(format!("Error!"))
}
}
#[post("/perms", data="<perms>")]
async fn new_perms(perms: Form<PermsForm<'_>>, user: User, mut db: Connection<Notes>) -> Result<String> {
@ -181,6 +203,17 @@ async fn logout(mut db: Connection<Notes>, cookies: &CookieJar<'_>) -> Result<St
}
}
#[get("/notes/<list_uuid>")]
async fn show_notes(user: User, mut db: Connection<Notes>, list_uuid: String) -> Result<Template> {
let list = match List::from_uuid(list_uuid, &mut *db).await? {
Some(list) => list,
None => panic!("LOL!"),
};
Ok(Template::render("show_notes", context!{
notes: get_notes_from_list(list.id, &mut *db).await?
}))
}
#[launch]
fn rocket() -> _ {
rocket::build()
@ -197,12 +230,15 @@ fn rocket() -> _ {
.mount("/new", routes![
new_list, new_list_not_logged_in,
new_perms, new_perms_not_logged_in,
new_note, //new_note_not_logged_in,
])
.mount("/show", routes![
show_list, show_list_not_logged_in
show_list, show_list_not_logged_in,
show_notes,
])
.mount("/forms", routes![
add_perms_form, add_perms_form_not_logged_in,
new_list_form, new_list_form_not_logged_in
new_list_form, new_list_form_not_logged_in,
new_note_form,
])
}

Loading…
Cancel
Save