From f1a631efff3e2a17423ae365ff45f0ea5b6c74e5 Mon Sep 17 00:00:00 2001 From: fef Date: Sun, 25 Dec 2022 18:11:45 +0100 Subject: [PATCH] data: remove unnecessary generics The repository layer is the central gateway to the data storage subsystem, so it should also be solely responsible for transforming data coming from the rest of the app into the appropriate format. --- src/data/account/pg.rs | 12 ++-------- src/data/follow/pg.rs | 43 +++++++++++------------------------- src/data/like/pg.rs | 27 +++++----------------- src/data/note/pg.rs | 18 +++------------ src/model/note.rs | 4 ++-- src/repo/account.rs | 7 +++--- src/repo/follow.rs | 9 ++++---- src/repo/like.rs | 7 +++--- src/repo/note.rs | 10 +++++---- src/route/api/v1/accounts.rs | 1 + 10 files changed, 44 insertions(+), 94 deletions(-) diff --git a/src/data/account/pg.rs b/src/data/account/pg.rs index ff9821a..9087e76 100644 --- a/src/data/account/pg.rs +++ b/src/data/account/pg.rs @@ -12,11 +12,7 @@ impl PgAccountDataSource { PgAccountDataSource { pool } } - pub async fn by_id(&self, id: I) -> Result - where - I: Into + Send, - { - let id: Id = id.into(); + pub async fn by_id(&self, id: Id) -> Result { let account: Account = sqlx::query_as("SELECT * FROM accounts WHERE id = $1") .bind(id) .fetch_one(&self.pool) @@ -24,11 +20,7 @@ impl PgAccountDataSource { Ok(account) } - pub async fn create(&self, new: T) -> Result - where - T: Into + Send, - { - let new = new.into(); + pub async fn create(&self, new: NewAccount) -> Result { let account: Account = sqlx::query_as( "INSERT INTO accounts (iri, name, domain, display_name) VALUES ($1, $2, $3, $4) diff --git a/src/data/follow/pg.rs b/src/data/follow/pg.rs index 2878188..24409aa 100644 --- a/src/data/follow/pg.rs +++ b/src/data/follow/pg.rs @@ -12,22 +12,15 @@ impl PgFollowDataSource { PgFollowDataSource { pool } } - pub async fn by_id(&self, id: I) -> Result - where - I: Into + Send, - { + pub async fn by_id(&self, id: Id) -> Result { let follow: Follow = sqlx::query_as("SELECT * FROM follows WHERE id = $1") - .bind(id.into()) + .bind(id) .fetch_one(&self.pool) .await?; Ok(follow) } - pub async fn create(&self, new: T) -> Result - where - T: Into + Send, - { - let new = new.into(); + pub async fn create(&self, new: NewFollow) -> Result { let follow: Follow = sqlx::query_as( "INSERT INTO follows (iri, follower_id, followee_id) VALUES ($1, $2, $3) @@ -41,41 +34,31 @@ impl PgFollowDataSource { Ok(follow) } - pub async fn by_follower_and_followee( + pub async fn by_follower_and_followee( &self, - follower_id: I, - followee_id: J, - ) -> Result - where - I: Into + Send, - J: Into + Send, - { + follower_id: Id, + followee_id: Id, + ) -> Result { let follow: Follow = sqlx::query_as("SELECT * FROM follows WHERE follower_id = $1 AND followee_id = $2") - .bind(follower_id.into()) - .bind(followee_id.into()) + .bind(follower_id) + .bind(followee_id) .fetch_one(&self.pool) .await?; Ok(follow) } - pub async fn followees_of(&self, account_id: I) -> Result> - where - I: Into + Send, - { + pub async fn followees_of(&self, account_id: Id) -> Result> { let followees: Vec = sqlx::query_as("SELECT * FROM follows WHERE follower_id = $1") - .bind(account_id.into()) + .bind(account_id) .fetch_all(&self.pool) .await?; Ok(followees) } - pub async fn followers_of(&self, account_id: I) -> Result> - where - I: Into + Send, - { + pub async fn followers_of(&self, account_id: Id) -> Result> { let followers: Vec = sqlx::query_as("SELECT * FROM follows WHERE followee_id = $1") - .bind(account_id.into()) + .bind(account_id) .fetch_all(&self.pool) .await?; Ok(followers) diff --git a/src/data/like/pg.rs b/src/data/like/pg.rs index d679944..5595603 100644 --- a/src/data/like/pg.rs +++ b/src/data/like/pg.rs @@ -13,23 +13,15 @@ impl PgLikeDataSource { PgLikeDataSource { pool } } - pub async fn by_id(&self, id: I) -> Result - where - I: Into + Send, - { + pub async fn by_id(&self, id: Id) -> Result { let like: Like = sqlx::query_as("SELECT * FROM likes WHERE id = $1") - .bind(id.into()) + .bind(id) .fetch_one(&self.pool) .await?; Ok(like) } - pub async fn create(&self, new: T) -> Result - where - T: TryInto, Error = E> + Send, - E: Into, - { - let new = new.try_into().map_err(|e| e.into())?.inner(); + pub async fn create(&self, new: NewLike) -> Result { let like: Like = sqlx::query_as( "INSERT INTO likes (iri, note_id, account_id) VALUES ($1, $2, $3) @@ -43,12 +35,7 @@ impl PgLikeDataSource { Ok(like) } - pub async fn delete(&self, note_id: N, account_id: A) -> Result<()> - where - N: Into, - A: Into, - { - let (note_id, account_id) = (note_id.into(), account_id.into()); + pub async fn delete(&self, note_id: Id, account_id: Id) -> Result<()> { sqlx::query("DELETE FROM likes WHERE note_id = $1 AND account_id = $2") .bind(note_id) .bind(account_id) @@ -57,11 +44,7 @@ impl PgLikeDataSource { Ok(()) } - pub async fn count_by_note(&self, note_id: I) -> Result - where - I: Into + Send, - { - let note_id = note_id.into(); + pub async fn count_by_note(&self, note_id: Id) -> Result { let result: Count = sqlx::query_as("SELECT COUNT(*) as count FROM likes WHERE note_id = $1") .bind(note_id) diff --git a/src/data/note/pg.rs b/src/data/note/pg.rs index 1f35011..d80a966 100644 --- a/src/data/note/pg.rs +++ b/src/data/note/pg.rs @@ -13,11 +13,7 @@ impl PgNoteDataSource { PgNoteDataSource { pool } } - pub async fn by_id(&self, id: I) -> Result - where - I: Into + Send, - { - let id: Id = id.into(); + pub async fn by_id(&self, id: Id) -> Result { let note: Note = sqlx::query_as("SELECT * FROM notes WHERE id = $1") .bind(id) .fetch_one(&self.pool) @@ -25,11 +21,7 @@ impl PgNoteDataSource { Ok(note) } - pub async fn by_account(&self, id: I, since: NaiveDateTime) -> Result> - where - I: Into + Send, - { - let id: Id = id.into(); + pub async fn by_account(&self, id: Id, since: NaiveDateTime) -> Result> { let notes: Vec = sqlx::query_as( "SELECT * FROM notes WHERE account_id = $1 AND created_at < $1 @@ -43,11 +35,7 @@ impl PgNoteDataSource { Ok(notes) } - pub async fn create(&self, new: T) -> Result - where - T: Into + Send, - { - let new = new.into(); + pub async fn create(&self, new: NewNote) -> Result { let note: Note = sqlx::query_as( "INSERT INTO notes ( account_id, diff --git a/src/model/note.rs b/src/model/note.rs index 73b4a5b..b463198 100644 --- a/src/model/note.rs +++ b/src/model/note.rs @@ -8,7 +8,7 @@ use crate::core::*; pub struct Note { pub id: Id, pub account_id: Id, - pub uri: Option, + pub iri: Option, pub content: String, pub source: Option, pub summary: Option, @@ -19,7 +19,7 @@ pub struct Note { pub struct NewNote { pub account_id: Id, - pub uri: String, + pub iri: Option, pub content: String, pub source: Option, pub summary: Option, diff --git a/src/repo/account.rs b/src/repo/account.rs index b3c70f0..9291fae 100644 --- a/src/repo/account.rs +++ b/src/repo/account.rs @@ -19,7 +19,7 @@ impl AccountRepo { where I: Into + Send, { - self.db.by_id(id).await + self.db.by_id(id.into()).await } pub async fn create(&self, new: T) -> Result @@ -27,8 +27,7 @@ impl AccountRepo { T: TryInto, Error = E> + Send, E: Into, { - self.db - .create(new.try_into().map_err(|e| e.into())?.inner()) - .await + let new = new.try_into().map_err(|e| e.into())?.inner(); + self.db.create(new).await } } diff --git a/src/repo/follow.rs b/src/repo/follow.rs index 7929235..dd12e5c 100644 --- a/src/repo/follow.rs +++ b/src/repo/follow.rs @@ -19,14 +19,14 @@ impl FollowRepo { where I: Into + Send, { - self.db.by_id(id).await + self.db.by_id(id.into()).await } pub async fn create(&self, new: T) -> Result where T: Into + Send, { - self.db.create(new).await + self.db.create(new.into()).await } pub async fn by_follower_and_followee( @@ -38,6 +38,7 @@ impl FollowRepo { I: Into + Send, J: Into + Send, { + let (follower_id, followee_id) = (follower_id.into(), followee_id.into()); self.db .by_follower_and_followee(follower_id, followee_id) .await @@ -62,13 +63,13 @@ impl FollowRepo { where I: Into + Send, { - self.db.followees_of(account_id).await + self.db.followees_of(account_id.into()).await } pub async fn followers_of(&self, account_id: I) -> Result> where I: Into + Send, { - self.db.followers_of(account_id).await + self.db.followers_of(account_id.into()).await } } diff --git a/src/repo/like.rs b/src/repo/like.rs index 39dbc75..3ea37d0 100644 --- a/src/repo/like.rs +++ b/src/repo/like.rs @@ -19,7 +19,7 @@ impl LikeRepo { where I: Into + Send, { - self.db.by_id(id).await + self.db.by_id(id.into()).await } pub async fn create(&self, new: T) -> Result @@ -27,6 +27,7 @@ impl LikeRepo { T: TryInto, Error = E> + Send, E: Into, { + let new = new.try_into().map_err(|e| e.into())?.inner(); self.db.create(new).await } @@ -35,13 +36,13 @@ impl LikeRepo { N: Into, A: Into, { - self.db.delete(note_id, account_id).await + self.db.delete(note_id.into(), account_id.into()).await } pub async fn count_by_note(&self, note_id: I) -> Result where I: Into + Send, { - self.db.count_by_note(note_id).await + self.db.count_by_note(note_id.into()).await } } diff --git a/src/repo/note.rs b/src/repo/note.rs index ca240e4..974c0c5 100644 --- a/src/repo/note.rs +++ b/src/repo/note.rs @@ -20,20 +20,22 @@ impl NoteRepo { where I: Into + Send, { - self.db.by_id(id).await + self.db.by_id(id.into()).await } pub async fn by_account(&self, account_id: I, since: NaiveDateTime) -> Result> where I: Into + Send, { - self.db.by_account(account_id, since).await + self.db.by_account(account_id.into(), since).await } - pub async fn create(&self, new: T) -> Result + pub async fn create(&self, new: T) -> Result where - T: Into + Send, + T: TryInto, Error = E> + Send, + E: Into, { + let new = new.try_into().map_err(|e| e.into())?.inner(); self.db.create(new).await } } diff --git a/src/route/api/v1/accounts.rs b/src/route/api/v1/accounts.rs index 2a62bfd..bdb0140 100644 --- a/src/route/api/v1/accounts.rs +++ b/src/route/api/v1/accounts.rs @@ -32,6 +32,7 @@ async fn signup(data: web::Json, state: AppState) -> Result