Make replies and sharing work
This commit is contained in:
parent
42d980f4cb
commit
30ffd5ad6f
4 changed files with 85 additions and 20 deletions
|
@ -32,7 +32,7 @@ impl Database {
|
|||
.query_map([], |row| {
|
||||
let id = row.get::<usize, Option<i64>>(0)?.unwrap();
|
||||
let replies = self.conn
|
||||
.prepare(&format!("SELECT id, author, email, text, timestamp FROM comment WHERE parent={id} ORDER BY timestamp DESC"))?
|
||||
.prepare(&format!("SELECT id, author, email, text, timestamp FROM comment WHERE parent={id}"))?
|
||||
.query_map([], |row| {
|
||||
Ok(Comment {
|
||||
id: row.get(0)?,
|
||||
|
@ -62,12 +62,13 @@ impl Database {
|
|||
|
||||
pub fn create_comment(&self, comment: &Comment) -> Result<()> {
|
||||
self.conn.execute(
|
||||
"INSERT INTO comment (author, email, text, content_id) VALUES (?1, ?2, ?3, ?4)",
|
||||
"INSERT INTO comment (author, email, text, content_id, parent) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![
|
||||
&comment.author,
|
||||
&comment.email,
|
||||
&comment.text,
|
||||
&comment.content_id
|
||||
&comment.content_id,
|
||||
&comment.parent,
|
||||
],
|
||||
)?;
|
||||
Ok(())
|
||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -123,7 +123,25 @@ async fn post_comment(
|
|||
Ok(database) => database,
|
||||
Err(response) => return response,
|
||||
};
|
||||
database.create_comment(&comment).unwrap();
|
||||
if let Some(parent) = comment.parent {
|
||||
'outer2: loop {
|
||||
match database.get_comments(&comment.content_id) {
|
||||
Ok(comments) => for other_comment in comments.iter() {
|
||||
if other_comment.id.unwrap() == parent {
|
||||
if other_comment.parent.is_none() {
|
||||
break 'outer2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(_) => return HttpResponse::InternalServerError().reason("failed to get comments").finish(),
|
||||
}
|
||||
return HttpResponse::BadRequest().reason("invalid comment parent").finish();
|
||||
}
|
||||
}
|
||||
if let Err(_) = database.create_comment(&comment) {
|
||||
return HttpResponse::InternalServerError().reason("failed to create comment").finish();
|
||||
}
|
||||
HttpResponse::Ok().into()
|
||||
}
|
||||
Err(_) => HttpResponse::BadRequest().reason("failed to parse request body").finish(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue