feat: add documentation crate and integrate OpenAPI specifications

- Added a new crate `doc` for API documentation.
- Integrated `utoipa` for OpenAPI support in the presentation layer.
- Updated routes to include social features (follow, unfollow, etc.) and diary export.
- Enhanced API request and response structures with new DTOs for social interactions.
- Updated `Cargo.toml` files to include new dependencies and features.
- Modified Dockerfile to copy the new documentation crate.
- Refactored existing handlers and routes to accommodate new API endpoints.
- Updated tests to cover new functionality and ensure proper API behavior.
This commit is contained in:
2026-05-09 21:29:20 +02:00
parent f66f8745c7
commit fa501706cd
16 changed files with 777 additions and 38 deletions

View File

@@ -266,6 +266,8 @@ pub enum Command {
ClearToken,
}
// Matches the export CSV column order:
// title,year,director,rating,comment,watched_at,external_metadata_id
pub fn parse_csv(content: &str) -> Vec<ParsedRow> {
let mut rdr = csv::Reader::from_reader(content.as_bytes());
let mut rows = Vec::new();
@@ -285,10 +287,11 @@ pub fn parse_csv(content: &str) -> Vec<ParsedRow> {
let title = record.get(0).unwrap_or("").trim().to_string();
let year_str = record.get(1).unwrap_or("").trim().to_string();
let external_id = record.get(2).unwrap_or("").trim().to_string();
let director = record.get(2).unwrap_or("").trim().to_string();
let rating_str = record.get(3).unwrap_or("").trim().to_string();
let watched_at = record.get(4).unwrap_or("").trim().to_string();
let comment = record.get(5).unwrap_or("").trim().to_string();
let comment = record.get(4).unwrap_or("").trim().to_string();
let watched_at = record.get(5).unwrap_or("").trim().to_string();
let external_id = record.get(6).unwrap_or("").trim().to_string();
if title.is_empty() && external_id.is_empty() {
rows.push(ParsedRow {
@@ -349,12 +352,9 @@ pub fn parse_csv(content: &str) -> Vec<ParsedRow> {
},
manual_title: if title.is_empty() { None } else { Some(title) },
manual_release_year,
manual_director: if director.is_empty() { None } else { Some(director) },
rating,
comment: if comment.is_empty() {
None
} else {
Some(comment)
},
comment: if comment.is_empty() { None } else { Some(comment) },
watched_at,
}),
});
@@ -843,6 +843,7 @@ pub fn update(app: &mut App, action: Action) -> Vec<Command> {
external_metadata_id: ext_id,
manual_title: title,
manual_release_year: year,
manual_director: None,
rating,
comment,
watched_at,
@@ -1366,6 +1367,7 @@ mod tests {
external_metadata_id: None,
manual_title: Some("The Matrix".into()),
manual_release_year: None,
manual_director: None,
rating: 5,
comment: None,
watched_at: "1999-03-31T00:00:00".into(),
@@ -1387,6 +1389,7 @@ mod tests {
external_metadata_id: None,
manual_title: Some("A".into()),
manual_release_year: None,
manual_director: None,
rating: 5,
comment: None,
watched_at: "2024-01-01T00:00:00".into(),
@@ -1395,6 +1398,7 @@ mod tests {
external_metadata_id: None,
manual_title: Some("B".into()),
manual_release_year: None,
manual_director: None,
rating: 4,
comment: None,
watched_at: "2024-01-02T00:00:00".into(),
@@ -1422,6 +1426,7 @@ mod tests {
external_metadata_id: None,
manual_title: Some("A".into()),
manual_release_year: None,
manual_director: None,
rating: 5,
comment: None,
watched_at: "2024-01-01T00:00:00".into(),
@@ -1481,20 +1486,24 @@ mod tests {
// ── parse_csv ─────────────────────────────────────────────────────────────
// CSV column order matches the export format:
// title,year,director,rating,comment,watched_at,external_metadata_id
#[test]
fn parse_csv_valid_row_with_title() {
let csv = "title,year,external_id,rating,watched_at,comment\nThe Matrix,1999,,5,1999-03-31T00:00:00,\n";
let csv = "title,year,director,rating,comment,watched_at,external_metadata_id\nThe Matrix,1999,Wachowski,5,,1999-03-31T00:00:00,\n";
let rows = parse_csv(csv);
assert_eq!(rows.len(), 1);
assert!(rows[0].result.is_ok());
let req = rows[0].result.as_ref().unwrap();
assert_eq!(req.manual_title.as_deref(), Some("The Matrix"));
assert_eq!(req.manual_director.as_deref(), Some("Wachowski"));
assert_eq!(req.rating, 5);
}
#[test]
fn parse_csv_row_missing_title_and_id_is_error() {
let csv = "title,year,external_id,rating,watched_at,comment\n,,,5,2024-01-01T00:00:00,\n";
let csv = "title,year,director,rating,comment,watched_at,external_metadata_id\n,,,5,,2024-01-01T00:00:00,\n";
let rows = parse_csv(csv);
assert_eq!(rows.len(), 1);
assert!(rows[0].result.is_err());
@@ -1502,14 +1511,14 @@ mod tests {
#[test]
fn parse_csv_invalid_rating_is_error() {
let csv = "title,year,external_id,rating,watched_at,comment\nThe Matrix,,,9,2024-01-01T00:00:00,\n";
let csv = "title,year,director,rating,comment,watched_at,external_metadata_id\nThe Matrix,,,9,,2024-01-01T00:00:00,\n";
let rows = parse_csv(csv);
assert!(rows[0].result.is_err());
}
#[test]
fn parse_csv_with_external_id_only() {
let csv = "title,year,external_id,rating,watched_at,comment\n,,tt0133093,5,1999-03-31T00:00:00,\n";
let csv = "title,year,director,rating,comment,watched_at,external_metadata_id\n,,,5,,1999-03-31T00:00:00,tt0133093\n";
let rows = parse_csv(csv);
assert!(rows[0].result.is_ok());
let req = rows[0].result.as_ref().unwrap();
@@ -1519,7 +1528,7 @@ mod tests {
#[test]
fn parse_csv_rating_zero_is_valid() {
let csv = "title,year,external_id,rating,watched_at,comment\nThe Matrix,,,0,2024-01-01T00:00:00,\n";
let csv = "title,year,director,rating,comment,watched_at,external_metadata_id\nThe Matrix,,,0,,2024-01-01T00:00:00,\n";
let rows = parse_csv(csv);
assert_eq!(rows.len(), 1);
assert!(rows[0].result.is_ok());

View File

@@ -22,6 +22,8 @@ pub struct LogReviewRequest {
pub manual_title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub manual_release_year: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
pub manual_director: Option<String>,
pub rating: u8,
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<String>,
@@ -63,6 +65,28 @@ pub struct ReviewHistoryResponse {
pub trend: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct FollowRequest {
pub handle: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct ActorUrlRequest {
pub actor_url: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct RemoteActorDto {
pub handle: String,
pub display_name: Option<String>,
pub url: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ActorListResponse {
pub actors: Vec<RemoteActorDto>,
}
// ── Error ─────────────────────────────────────────────────────────────────────
#[derive(Debug, thiserror::Error)]
@@ -119,10 +143,16 @@ impl ApiClient {
self.base_url.read().unwrap().clone()
}
fn api(&self, path: &str) -> String {
format!("{}/api/v1{}", self.url(), path)
}
// ── Auth ──────────────────────────────────────────────────────────────────
pub async fn login(&self, email: &str, password: &str) -> Result<LoginResponse, ApiError> {
let resp = self
.http
.post(format!("{}/api/auth/login", self.url()))
.post(self.api("/auth/login"))
.json(&LoginRequest {
email: email.into(),
password: password.into(),
@@ -132,6 +162,8 @@ impl ApiClient {
Ok(check_status(resp).await?.json().await?)
}
// ── Diary ─────────────────────────────────────────────────────────────────
pub async fn get_diary(
&self,
token: &str,
@@ -140,7 +172,7 @@ impl ApiClient {
) -> Result<DiaryResponse, ApiError> {
let resp = self
.http
.get(format!("{}/api/diary", self.url()))
.get(self.api("/diary"))
.query(&[("offset", offset), ("limit", limit)])
.bearer_auth(token)
.send()
@@ -148,6 +180,23 @@ impl ApiClient {
Ok(check_status(resp).await?.json().await?)
}
pub async fn export_diary(
&self,
token: &str,
format: &str,
) -> Result<Vec<u8>, ApiError> {
let resp = self
.http
.get(self.api("/diary/export"))
.query(&[("format", format)])
.bearer_auth(token)
.send()
.await?;
Ok(check_status(resp).await?.bytes().await?.to_vec())
}
// ── Reviews ───────────────────────────────────────────────────────────────
pub async fn get_movie_history(
&self,
token: &str,
@@ -155,7 +204,7 @@ impl ApiClient {
) -> Result<ReviewHistoryResponse, ApiError> {
let resp = self
.http
.get(format!("{}/api/movies/{}/history", self.url(), movie_id))
.get(self.api(&format!("/movies/{movie_id}/history")))
.bearer_auth(token)
.send()
.await?;
@@ -165,7 +214,7 @@ impl ApiClient {
pub async fn create_review(&self, token: &str, req: &LogReviewRequest) -> Result<(), ApiError> {
let resp = self
.http
.post(format!("{}/api/reviews", self.url()))
.post(self.api("/reviews"))
.bearer_auth(token)
.json(req)
.send()
@@ -177,13 +226,95 @@ impl ApiClient {
pub async fn delete_review(&self, token: &str, review_id: Uuid) -> Result<(), ApiError> {
let resp = self
.http
.delete(format!("{}/api/reviews/{}", self.url(), review_id))
.delete(self.api(&format!("/reviews/{review_id}")))
.bearer_auth(token)
.send()
.await?;
check_status(resp).await?;
Ok(())
}
// ── Social (ActivityPub) ──────────────────────────────────────────────────
pub async fn get_following(&self, token: &str) -> Result<ActorListResponse, ApiError> {
let resp = self
.http
.get(self.api("/social/following"))
.bearer_auth(token)
.send()
.await?;
Ok(check_status(resp).await?.json().await?)
}
pub async fn get_followers(&self, token: &str) -> Result<ActorListResponse, ApiError> {
let resp = self
.http
.get(self.api("/social/followers"))
.bearer_auth(token)
.send()
.await?;
Ok(check_status(resp).await?.json().await?)
}
pub async fn follow(&self, token: &str, handle: &str) -> Result<(), ApiError> {
let resp = self
.http
.post(self.api("/social/follow"))
.bearer_auth(token)
.json(&FollowRequest { handle: handle.into() })
.send()
.await?;
check_status(resp).await?;
Ok(())
}
pub async fn unfollow(&self, token: &str, actor_url: &str) -> Result<(), ApiError> {
let resp = self
.http
.post(self.api("/social/unfollow"))
.bearer_auth(token)
.json(&ActorUrlRequest { actor_url: actor_url.into() })
.send()
.await?;
check_status(resp).await?;
Ok(())
}
pub async fn accept_follower(&self, token: &str, actor_url: &str) -> Result<(), ApiError> {
let resp = self
.http
.post(self.api("/social/followers/accept"))
.bearer_auth(token)
.json(&ActorUrlRequest { actor_url: actor_url.into() })
.send()
.await?;
check_status(resp).await?;
Ok(())
}
pub async fn reject_follower(&self, token: &str, actor_url: &str) -> Result<(), ApiError> {
let resp = self
.http
.post(self.api("/social/followers/reject"))
.bearer_auth(token)
.json(&ActorUrlRequest { actor_url: actor_url.into() })
.send()
.await?;
check_status(resp).await?;
Ok(())
}
pub async fn remove_follower(&self, token: &str, actor_url: &str) -> Result<(), ApiError> {
let resp = self
.http
.post(self.api("/social/followers/remove"))
.bearer_auth(token)
.json(&ActorUrlRequest { actor_url: actor_url.into() })
.send()
.await?;
check_status(resp).await?;
Ok(())
}
}
#[cfg(test)]
@@ -209,6 +340,7 @@ mod tests {
external_metadata_id: None,
manual_title: Some("The Matrix".into()),
manual_release_year: None,
manual_director: None,
rating: 5,
comment: None,
watched_at: "2024-01-15T20:00:00".into(),
@@ -216,15 +348,40 @@ mod tests {
let json = serde_json::to_string(&req).unwrap();
assert!(!json.contains("external_metadata_id"));
assert!(!json.contains("manual_release_year"));
assert!(!json.contains("manual_director"));
assert!(json.contains("\"manual_title\":\"The Matrix\""));
assert!(json.contains("\"rating\":5"));
}
#[test]
fn log_review_request_includes_director_when_set() {
let req = LogReviewRequest {
external_metadata_id: None,
manual_title: Some("Dune".into()),
manual_release_year: Some(2021),
manual_director: Some("Denis Villeneuve".into()),
rating: 5,
comment: None,
watched_at: "2024-01-15T20:00:00".into(),
};
let json = serde_json::to_string(&req).unwrap();
assert!(json.contains("\"manual_director\":\"Denis Villeneuve\""));
}
#[test]
fn api_client_builds_versioned_urls() {
let client = ApiClient::new("http://localhost:3000");
assert_eq!(client.api("/diary"), "http://localhost:3000/api/v1/diary");
assert_eq!(client.api("/auth/login"), "http://localhost:3000/api/v1/auth/login");
assert_eq!(client.api("/social/follow"), "http://localhost:3000/api/v1/social/follow");
}
#[test]
fn api_client_update_url() {
let client = ApiClient::new("http://localhost:3000");
assert!(client.url().contains("3000"));
client.update_url("http://localhost:8080");
assert!(client.url().contains("8080"));
assert_eq!(client.api("/diary"), "http://localhost:8080/api/v1/diary");
}
}