quotes create&getbyid, migration renew, misc
This commit is contained in:
@@ -7,12 +7,14 @@ use axum::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
database::DatabaseError,
|
database::DatabaseError,
|
||||||
persons::PersonError,
|
persons::PersonError,
|
||||||
|
quotes::QuoteError,
|
||||||
tags::TagError,
|
tags::TagError,
|
||||||
users::{UserError, auth::AuthError, sessions::SessionError},
|
users::{UserError, auth::AuthError, sessions::SessionError},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
mod persons;
|
mod persons;
|
||||||
|
mod quotes;
|
||||||
mod sessions;
|
mod sessions;
|
||||||
mod tags;
|
mod tags;
|
||||||
mod users;
|
mod users;
|
||||||
@@ -20,10 +22,10 @@ mod users;
|
|||||||
pub fn api_router() -> Router {
|
pub fn api_router() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/api/live", get(async || "Mnemosyne lives"))
|
.route("/api/live", get(async || "Mnemosyne lives"))
|
||||||
//
|
// auth
|
||||||
.route("/api/auth/login", post(auth::login))
|
.route("/api/auth/login", post(auth::login))
|
||||||
.route("/api/auth/logout", post(auth::logout))
|
.route("/api/auth/logout", post(auth::logout))
|
||||||
//
|
// users
|
||||||
.route("/api/users", get(users::get_all))
|
.route("/api/users", get(users::get_all))
|
||||||
.route("/api/users", post(users::create))
|
.route("/api/users", post(users::create))
|
||||||
.route("/api/users/me", get(users::get_me))
|
.route("/api/users/me", get(users::get_me))
|
||||||
@@ -31,17 +33,17 @@ pub fn api_router() -> Router {
|
|||||||
.route("/api/users/@{handle}", get(users::get_by_handle))
|
.route("/api/users/@{handle}", get(users::get_by_handle))
|
||||||
.route("/api/users/{id}/setpassw", post(users::change_password))
|
.route("/api/users/{id}/setpassw", post(users::change_password))
|
||||||
.route("/api/users/{id}/sethandle", post(users::change_handle))
|
.route("/api/users/{id}/sethandle", post(users::change_handle))
|
||||||
//
|
// sessions
|
||||||
.route("/api/sessions/{id}", get(sessions::get_by_id))
|
.route("/api/sessions/{id}", get(sessions::get_by_id))
|
||||||
.route("/api/sessions/{id}/revoke", post(sessions::revoke_by_id))
|
.route("/api/sessions/{id}/revoke", post(sessions::revoke_by_id))
|
||||||
//
|
// tags
|
||||||
.route("/api/tags", get(tags::get_all))
|
.route("/api/tags", get(tags::get_all))
|
||||||
.route("/api/tags", post(tags::create))
|
.route("/api/tags", post(tags::create))
|
||||||
.route("/api/tags/{id}", get(tags::get_by_id))
|
.route("/api/tags/{id}", get(tags::get_by_id))
|
||||||
.route("/api/tags/{id}", patch(tags::rename))
|
.route("/api/tags/{id}", patch(tags::rename))
|
||||||
.route("/api/tags/{id}", delete(tags::delete))
|
.route("/api/tags/{id}", delete(tags::delete))
|
||||||
.route("/api/tags/#{name}", get(tags::get_by_name))
|
.route("/api/tags/#{name}", get(tags::get_by_name))
|
||||||
//
|
// persons & names
|
||||||
.route("/api/persons", get(persons::get_all))
|
.route("/api/persons", get(persons::get_all))
|
||||||
.route("/api/persons", post(persons::create))
|
.route("/api/persons", post(persons::create))
|
||||||
.route("/api/persons/{id}", get(persons::get_by_id))
|
.route("/api/persons/{id}", get(persons::get_by_id))
|
||||||
@@ -49,6 +51,9 @@ pub fn api_router() -> Router {
|
|||||||
.route("/api/persons/{id}/addname", post(persons::add_name))
|
.route("/api/persons/{id}/addname", post(persons::add_name))
|
||||||
.route("/api/names/{id}", get(persons::n_by_id))
|
.route("/api/names/{id}", get(persons::n_by_id))
|
||||||
.route("/api/names/{id}/setprimary", post(persons::n_setprimary))
|
.route("/api/names/{id}/setprimary", post(persons::n_setprimary))
|
||||||
|
// quotes
|
||||||
|
.route("/api/quotes", post(quotes::create))
|
||||||
|
.route("/api/quotes/{id}", get(quotes::get_by_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CompositeError(Response);
|
pub struct CompositeError(Response);
|
||||||
@@ -75,5 +80,6 @@ composite_from!(
|
|||||||
SessionError,
|
SessionError,
|
||||||
TagError,
|
TagError,
|
||||||
PersonError,
|
PersonError,
|
||||||
|
QuoteError,
|
||||||
DatabaseError
|
DatabaseError
|
||||||
);
|
);
|
||||||
|
|||||||
66
src/api/quotes.rs
Normal file
66
src/api/quotes.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
use axum::{
|
||||||
|
Json,
|
||||||
|
extract::Path,
|
||||||
|
http::{HeaderMap, StatusCode},
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use chrono::{DateTime, FixedOffset};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
api::CompositeError,
|
||||||
|
persons::Name,
|
||||||
|
quotes::Quote,
|
||||||
|
users::{
|
||||||
|
User,
|
||||||
|
auth::{UserAuthRequired, UserAuthenticate},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn get_by_id(
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
) -> Result<Response, CompositeError> {
|
||||||
|
User::authenticate(&headers)?.required()?;
|
||||||
|
Ok(Json(Quote::get_by_id(id)?).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct QuoteLineForm {
|
||||||
|
pub content: String,
|
||||||
|
pub name_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct QuoteCreateForm {
|
||||||
|
pub lines: Vec<QuoteLineForm>,
|
||||||
|
pub timestamp: DateTime<FixedOffset>,
|
||||||
|
pub context: Option<String>,
|
||||||
|
pub location: Option<String>,
|
||||||
|
pub public: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Json(form): Json<QuoteCreateForm>,
|
||||||
|
) -> Result<Response, CompositeError> {
|
||||||
|
let u = User::authenticate(&headers)?.required()?;
|
||||||
|
|
||||||
|
let lines = form
|
||||||
|
.lines
|
||||||
|
.into_iter()
|
||||||
|
.map(|l| Ok((l.content, Name::get_by_id(l.name_id)?)))
|
||||||
|
.collect::<Result<Vec<(String, Name)>, CompositeError>>()?;
|
||||||
|
|
||||||
|
let q = Quote::create(
|
||||||
|
lines,
|
||||||
|
form.timestamp,
|
||||||
|
form.context,
|
||||||
|
form.location,
|
||||||
|
u.id,
|
||||||
|
form.public,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((StatusCode::CREATED, Json(q)).into_response())
|
||||||
|
}
|
||||||
@@ -20,18 +20,9 @@ CREATE TABLE sessions (
|
|||||||
);
|
);
|
||||||
CREATE INDEX sessions_by_userid ON sessions(user_id);
|
CREATE INDEX sessions_by_userid ON sessions(user_id);
|
||||||
|
|
||||||
-- CREATE TABLE logs (
|
|
||||||
-- id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
|
||||||
-- actor BLOB NOT NULL REFERENCES users(id), -- UUIDv7 as bytes
|
|
||||||
-- -- (userID with special cases: UUID::nil if system, UUID::max if infradmin)
|
|
||||||
-- -- ((infradmin & system shall both be users))
|
|
||||||
-- target BLOB, -- Option<UUIDv7 as bytes (userID)>
|
|
||||||
-- change TEXT NOT NULL
|
|
||||||
-- );
|
|
||||||
|
|
||||||
CREATE TABLE quotes (
|
CREATE TABLE quotes (
|
||||||
id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
||||||
timestamp TEXT NOT NULL, -- RFC3339 into DateTime<Utc>
|
timestamp TEXT NOT NULL, -- RFC3339 into DateTime<FixedOffset>
|
||||||
location TEXT,
|
location TEXT,
|
||||||
context TEXT,
|
context TEXT,
|
||||||
created_by BLOB NOT NULL REFERENCES users(id), -- UUIDv7 as bytes
|
created_by BLOB NOT NULL REFERENCES users(id), -- UUIDv7 as bytes
|
||||||
@@ -85,6 +76,15 @@ CREATE TABLE quote_tags (
|
|||||||
) WITHOUT ROWID;
|
) WITHOUT ROWID;
|
||||||
CREATE INDEX quote_tags_reverse_index ON quote_tags(tag_id, quote_id);
|
CREATE INDEX quote_tags_reverse_index ON quote_tags(tag_id, quote_id);
|
||||||
|
|
||||||
|
-- CREATE TABLE logs (
|
||||||
|
-- id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
|
||||||
|
-- actor BLOB NOT NULL REFERENCES users(id), -- UUIDv7 as bytes
|
||||||
|
-- -- (userID with special cases: UUID::nil if system, UUID::max if infradmin)
|
||||||
|
-- -- ((infradmin & system shall both be users))
|
||||||
|
-- target BLOB, -- Option<UUIDv7 as bytes (userID)>
|
||||||
|
-- change TEXT NOT NULL
|
||||||
|
-- );
|
||||||
|
|
||||||
-- all this to be followed by:
|
-- all this to be followed by:
|
||||||
-- - a better access scoping mechanism (role-based like discord)
|
-- - a better access scoping mechanism (role-based like discord)
|
||||||
-- - photos just like quotes
|
-- - photos just like quotes
|
||||||
@@ -8,7 +8,7 @@ macro_rules! migration {
|
|||||||
($name, include_str!(concat!("./migrations/", $name, ".sql")))
|
($name, include_str!(concat!("./migrations/", $name, ".sql")))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const MIGRATIONS: &[(&str, &str)] = &[migration!("2026-02-20--01")];
|
const MIGRATIONS: &[(&str, &str)] = &[migration!("2026-03-07--01")];
|
||||||
|
|
||||||
pub static DB_URL: LazyLock<String> =
|
pub static DB_URL: LazyLock<String> =
|
||||||
LazyLock::new(|| env::var("DATABASE_URL").expect("DATABASE_URL is not set"));
|
LazyLock::new(|| env::var("DATABASE_URL").expect("DATABASE_URL is not set"));
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use crate::persons::{Name, Person};
|
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
pub struct QuoteLine {
|
|
||||||
pub id: Uuid,
|
|
||||||
pub attribution: (Name, Person),
|
|
||||||
pub content: String,
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,170 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use axum::{http::StatusCode, response::IntoResponse};
|
||||||
|
use chrono::{DateTime, FixedOffset};
|
||||||
|
use rusqlite::OptionalExtension;
|
||||||
|
use serde::Serialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::quotes::lines::QuoteLine;
|
use crate::{
|
||||||
|
database::{self, DatabaseError},
|
||||||
|
persons::Name,
|
||||||
|
};
|
||||||
|
|
||||||
pub mod lines;
|
#[derive(Serialize)]
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
pub struct Quote {
|
pub struct Quote {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub lines: Vec<QuoteLine>,
|
pub lines: Vec<QuoteLine>,
|
||||||
pub timestamp: DateTime<Utc>,
|
pub timestamp: DateTime<FixedOffset>,
|
||||||
pub location: Option<String>,
|
pub location: Option<String>,
|
||||||
pub context: Option<String>,
|
pub context: Option<String>,
|
||||||
pub created_by: Uuid,
|
pub created_by: Uuid,
|
||||||
pub public: bool,
|
pub public: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct QuoteLine {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub attribution: Name,
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum QuoteError {
|
||||||
|
#[error("No quote with ID {0}")]
|
||||||
|
NoQuoteWithId(Uuid),
|
||||||
|
#[error("A quote must have at least one line")]
|
||||||
|
EmptyQuote,
|
||||||
|
#[error("{0}")]
|
||||||
|
DatabaseError(#[from] DatabaseError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Quote {
|
||||||
|
pub fn get_by_id(id: Uuid) -> Result<Quote, QuoteError> {
|
||||||
|
let conn = database::conn()?;
|
||||||
|
|
||||||
|
let quotemain = conn
|
||||||
|
.prepare(
|
||||||
|
"SELECT timestamp, location, context, created_by, public FROM quotes WHERE id = ?1",
|
||||||
|
)?
|
||||||
|
.query_row((id,), |r| {
|
||||||
|
Ok((
|
||||||
|
r.get::<_, DateTime<FixedOffset>>(0)?,
|
||||||
|
r.get::<_, Option<String>>(1)?,
|
||||||
|
r.get::<_, Option<String>>(2)?,
|
||||||
|
r.get::<_, Uuid>(3)?,
|
||||||
|
r.get::<_, bool>(4)?,
|
||||||
|
))
|
||||||
|
})
|
||||||
|
.optional()?;
|
||||||
|
|
||||||
|
let (timestamp, location, context, created_by, public) = match quotemain {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return Err(QuoteError::NoQuoteWithId(id)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let lines = conn
|
||||||
|
.prepare(
|
||||||
|
r#"
|
||||||
|
SELECT l.id, l.content, n.id, n.is_primary, n.person_id, n.created_by, n.name
|
||||||
|
FROM lines AS l JOIN names AS n ON l.name_id = n.id
|
||||||
|
WHERE l.quote_id = ?1 ORDER BY l.ordering
|
||||||
|
"#,
|
||||||
|
)?
|
||||||
|
.query_map((id,), |r| {
|
||||||
|
Ok(QuoteLine {
|
||||||
|
id: r.get(0)?,
|
||||||
|
content: r.get(1)?,
|
||||||
|
attribution: Name {
|
||||||
|
id: r.get(2)?,
|
||||||
|
is_primary: r.get(3)?,
|
||||||
|
person_id: r.get(4)?,
|
||||||
|
created_by: r.get(5)?,
|
||||||
|
name: r.get(6)?,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})?
|
||||||
|
.collect::<Result<Vec<QuoteLine>, _>>()?;
|
||||||
|
|
||||||
|
Ok(Quote {
|
||||||
|
id,
|
||||||
|
lines,
|
||||||
|
timestamp,
|
||||||
|
location,
|
||||||
|
context,
|
||||||
|
created_by,
|
||||||
|
public,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn create(
|
||||||
|
lines: Vec<(String, Name)>,
|
||||||
|
timestamp: DateTime<FixedOffset>,
|
||||||
|
context: Option<String>,
|
||||||
|
location: Option<String>,
|
||||||
|
created_by: Uuid,
|
||||||
|
public: bool,
|
||||||
|
) -> Result<Quote, QuoteError> {
|
||||||
|
if lines.is_empty() {
|
||||||
|
return Err(QuoteError::EmptyQuote);
|
||||||
|
}
|
||||||
|
|
||||||
|
let conn = database::conn()?;
|
||||||
|
let quote_id = Uuid::now_v7();
|
||||||
|
let lines: Vec<(Uuid, String, Name)> = lines
|
||||||
|
.into_iter()
|
||||||
|
.map(|(c, a)| (Uuid::now_v7(), c, a))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
conn.execute("BEGIN TRANSACTION", ())?;
|
||||||
|
|
||||||
|
let mut quote_stmt = conn.prepare(
|
||||||
|
r#"
|
||||||
|
INSERT INTO quotes (id, timestamp, location, context, created_by, public)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
||||||
|
"#,
|
||||||
|
)?;
|
||||||
|
quote_stmt.execute((quote_id, timestamp, &location, &context, created_by, public))?;
|
||||||
|
|
||||||
|
let mut line_stmt = conn.prepare(
|
||||||
|
r#"
|
||||||
|
INSERT INTO lines (id, quote_id, content, name_id, ordering)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||||
|
"#,
|
||||||
|
)?;
|
||||||
|
for (ordering, (id, content, attr)) in lines.iter().enumerate() {
|
||||||
|
line_stmt.execute((id, quote_id, content, attr.id, ordering as i64))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.execute("COMMIT", ())?;
|
||||||
|
Ok(Quote {
|
||||||
|
id: quote_id,
|
||||||
|
lines: lines
|
||||||
|
.into_iter()
|
||||||
|
.map(|(id, content, attribution)| QuoteLine {
|
||||||
|
id,
|
||||||
|
content,
|
||||||
|
attribution,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
timestamp,
|
||||||
|
location,
|
||||||
|
context,
|
||||||
|
created_by,
|
||||||
|
public,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<rusqlite::Error> for QuoteError {
|
||||||
|
fn from(error: rusqlite::Error) -> Self {
|
||||||
|
QuoteError::DatabaseError(DatabaseError::from(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for QuoteError {
|
||||||
|
fn into_response(self) -> axum::response::Response {
|
||||||
|
match self {
|
||||||
|
Self::DatabaseError(e) => e.into_response(),
|
||||||
|
Self::NoQuoteWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||||
|
Self::EmptyQuote => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user