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.
This commit is contained in:
anna 2022-12-25 18:11:45 +01:00
parent 4f542571e0
commit f1a631efff
Signed by: fef
GPG key ID: EC22E476DC2D3D84
10 changed files with 44 additions and 94 deletions

View file

@ -12,11 +12,7 @@ impl PgAccountDataSource {
PgAccountDataSource { pool }
}
pub async fn by_id<I>(&self, id: I) -> Result<Account>
where
I: Into<Id> + Send,
{
let id: Id = id.into();
pub async fn by_id(&self, id: Id) -> Result<Account> {
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<T>(&self, new: T) -> Result<Account>
where
T: Into<NewAccount> + Send,
{
let new = new.into();
pub async fn create(&self, new: NewAccount) -> Result<Account> {
let account: Account = sqlx::query_as(
"INSERT INTO accounts (iri, name, domain, display_name)
VALUES ($1, $2, $3, $4)

View file

@ -12,22 +12,15 @@ impl PgFollowDataSource {
PgFollowDataSource { pool }
}
pub async fn by_id<I>(&self, id: I) -> Result<Follow>
where
I: Into<Id> + Send,
{
pub async fn by_id(&self, id: Id) -> Result<Follow> {
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<T>(&self, new: T) -> Result<Follow>
where
T: Into<NewFollow> + Send,
{
let new = new.into();
pub async fn create(&self, new: NewFollow) -> Result<Follow> {
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<I, J>(
pub async fn by_follower_and_followee(
&self,
follower_id: I,
followee_id: J,
) -> Result<Follow>
where
I: Into<Id> + Send,
J: Into<Id> + Send,
{
follower_id: Id,
followee_id: Id,
) -> Result<Follow> {
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<I>(&self, account_id: I) -> Result<Vec<Follow>>
where
I: Into<Id> + Send,
{
pub async fn followees_of(&self, account_id: Id) -> Result<Vec<Follow>> {
let followees: Vec<Follow> = 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<I>(&self, account_id: I) -> Result<Vec<Follow>>
where
I: Into<Id> + Send,
{
pub async fn followers_of(&self, account_id: Id) -> Result<Vec<Follow>> {
let followers: Vec<Follow> = sqlx::query_as("SELECT * FROM follows WHERE followee_id = $1")
.bind(account_id.into())
.bind(account_id)
.fetch_all(&self.pool)
.await?;
Ok(followers)

View file

@ -13,23 +13,15 @@ impl PgLikeDataSource {
PgLikeDataSource { pool }
}
pub async fn by_id<I>(&self, id: I) -> Result<Like>
where
I: Into<Id> + Send,
{
pub async fn by_id(&self, id: Id) -> Result<Like> {
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<T, E>(&self, new: T) -> Result<Like>
where
T: TryInto<Sane<NewLike>, Error = E> + Send,
E: Into<Error>,
{
let new = new.try_into().map_err(|e| e.into())?.inner();
pub async fn create(&self, new: NewLike) -> Result<Like> {
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<N, A>(&self, note_id: N, account_id: A) -> Result<()>
where
N: Into<Id>,
A: Into<Id>,
{
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<I>(&self, note_id: I) -> Result<u64>
where
I: Into<Id> + Send,
{
let note_id = note_id.into();
pub async fn count_by_note(&self, note_id: Id) -> Result<u64> {
let result: Count =
sqlx::query_as("SELECT COUNT(*) as count FROM likes WHERE note_id = $1")
.bind(note_id)

View file

@ -13,11 +13,7 @@ impl PgNoteDataSource {
PgNoteDataSource { pool }
}
pub async fn by_id<I>(&self, id: I) -> Result<Note>
where
I: Into<Id> + Send,
{
let id: Id = id.into();
pub async fn by_id(&self, id: Id) -> Result<Note> {
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<I>(&self, id: I, since: NaiveDateTime) -> Result<Vec<Note>>
where
I: Into<Id> + Send,
{
let id: Id = id.into();
pub async fn by_account(&self, id: Id, since: NaiveDateTime) -> Result<Vec<Note>> {
let notes: Vec<Note> = 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<T>(&self, new: T) -> Result<Note>
where
T: Into<NewNote> + Send,
{
let new = new.into();
pub async fn create(&self, new: NewNote) -> Result<Note> {
let note: Note = sqlx::query_as(
"INSERT INTO notes (
account_id,

View file

@ -8,7 +8,7 @@ use crate::core::*;
pub struct Note {
pub id: Id,
pub account_id: Id,
pub uri: Option<String>,
pub iri: Option<String>,
pub content: String,
pub source: Option<String>,
pub summary: Option<String>,
@ -19,7 +19,7 @@ pub struct Note {
pub struct NewNote {
pub account_id: Id,
pub uri: String,
pub iri: Option<String>,
pub content: String,
pub source: Option<String>,
pub summary: Option<String>,

View file

@ -19,7 +19,7 @@ impl AccountRepo {
where
I: Into<Id> + Send,
{
self.db.by_id(id).await
self.db.by_id(id.into()).await
}
pub async fn create<T, E>(&self, new: T) -> Result<Account>
@ -27,8 +27,7 @@ impl AccountRepo {
T: TryInto<Sane<NewAccount>, Error = E> + Send,
E: Into<Error>,
{
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
}
}

View file

@ -19,14 +19,14 @@ impl FollowRepo {
where
I: Into<Id> + Send,
{
self.db.by_id(id).await
self.db.by_id(id.into()).await
}
pub async fn create<T>(&self, new: T) -> Result<Follow>
where
T: Into<NewFollow> + Send,
{
self.db.create(new).await
self.db.create(new.into()).await
}
pub async fn by_follower_and_followee<I, J>(
@ -38,6 +38,7 @@ impl FollowRepo {
I: Into<Id> + Send,
J: Into<Id> + 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<Id> + Send,
{
self.db.followees_of(account_id).await
self.db.followees_of(account_id.into()).await
}
pub async fn followers_of<I>(&self, account_id: I) -> Result<Vec<Follow>>
where
I: Into<Id> + Send,
{
self.db.followers_of(account_id).await
self.db.followers_of(account_id.into()).await
}
}

View file

@ -19,7 +19,7 @@ impl LikeRepo {
where
I: Into<Id> + Send,
{
self.db.by_id(id).await
self.db.by_id(id.into()).await
}
pub async fn create<T, E>(&self, new: T) -> Result<Like>
@ -27,6 +27,7 @@ impl LikeRepo {
T: TryInto<Sane<NewLike>, Error = E> + Send,
E: Into<Error>,
{
let new = new.try_into().map_err(|e| e.into())?.inner();
self.db.create(new).await
}
@ -35,13 +36,13 @@ impl LikeRepo {
N: Into<Id>,
A: Into<Id>,
{
self.db.delete(note_id, account_id).await
self.db.delete(note_id.into(), account_id.into()).await
}
pub async fn count_by_note<I>(&self, note_id: I) -> Result<u64>
where
I: Into<Id> + Send,
{
self.db.count_by_note(note_id).await
self.db.count_by_note(note_id.into()).await
}
}

View file

@ -20,20 +20,22 @@ impl NoteRepo {
where
I: Into<Id> + Send,
{
self.db.by_id(id).await
self.db.by_id(id.into()).await
}
pub async fn by_account<I>(&self, account_id: I, since: NaiveDateTime) -> Result<Vec<Note>>
where
I: Into<Id> + Send,
{
self.db.by_account(account_id, since).await
self.db.by_account(account_id.into(), since).await
}
pub async fn create<T>(&self, new: T) -> Result<Note>
pub async fn create<T, E>(&self, new: T) -> Result<Note>
where
T: Into<NewNote> + Send,
T: TryInto<Sane<NewNote>, Error = E> + Send,
E: Into<Error>,
{
let new = new.try_into().map_err(|e| e.into())?.inner();
self.db.create(new).await
}
}

View file

@ -32,6 +32,7 @@ async fn signup(data: web::Json<SignupData>, state: AppState) -> Result<HttpResp
.repo
.accounts
.create(Insane::from(NewAccount {
iri: None,
name: data.username,
domain: "localhost".into(),
display_name: None,