add objects table
This commit is contained in:
parent
f1a631efff
commit
0d9504ca3b
10 changed files with 946 additions and 15 deletions
812
Cargo.lock
generated
812
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -11,12 +11,19 @@ async-trait = "0.1.59"
|
||||||
chrono = { version = "0.4", features = [ "alloc", "clock", "serde" ] }
|
chrono = { version = "0.4", features = [ "alloc", "clock", "serde" ] }
|
||||||
dotenvy = "0.15.6"
|
dotenvy = "0.15.6"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
iref = "2.2"
|
||||||
|
json-ld = { version = "0.10", features = [ "reqwest" ] }
|
||||||
jsonwebtoken = { version = "8", default-features = false }
|
jsonwebtoken = { version = "8", default-features = false }
|
||||||
|
locspan = "0.7"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
pretty_env_logger = "0.4"
|
pretty_env_logger = "0.4"
|
||||||
|
rdf-types = "0.12"
|
||||||
|
reqwest = { version = "0.11", features = [ "rustls" ] }
|
||||||
serde = { version = "1.0", features = [ "derive" ] }
|
serde = { version = "1.0", features = [ "derive" ] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_test = "1.0"
|
serde_test = "1.0"
|
||||||
sqlx = { version = "0.6", features = [ "chrono", "runtime-actix-rustls", "postgres" ] }
|
sqlx = { version = "0.6", features = [ "chrono", "postgres", "runtime-actix-rustls", "uuid" ] }
|
||||||
|
static-iref = "2"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
tokio = "1.23"
|
tokio = "1.23"
|
||||||
|
uuid = { version = "1.2", features = [ "v4" ] }
|
||||||
|
|
11
migrations/20221225041649_create_objects.sql
Normal file
11
migrations/20221225041649_create_objects.sql
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
CREATE TABLE objects (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
iri VARCHAR NOT NULL,
|
||||||
|
data VARCHAR NOT NULL,
|
||||||
|
created_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||||
|
updated_at TIMESTAMP NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX index_objects_on_iri ON objects (iri);
|
||||||
|
CREATE INDEX index_objects_on_created_at ON objects (created_at);
|
||||||
|
CREATE INDEX index_objects_on_updated_at ON objects (updated_at);
|
|
@ -2,12 +2,14 @@ pub mod account;
|
||||||
pub mod follow;
|
pub mod follow;
|
||||||
pub mod like;
|
pub mod like;
|
||||||
pub mod note;
|
pub mod note;
|
||||||
|
pub mod object;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
pub use account::*;
|
pub use account::*;
|
||||||
pub use follow::*;
|
pub use follow::*;
|
||||||
pub use like::*;
|
pub use like::*;
|
||||||
pub use note::*;
|
pub use note::*;
|
||||||
|
pub use object::*;
|
||||||
pub use user::*;
|
pub use user::*;
|
||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
|
|
3
src/data/object/mod.rs
Normal file
3
src/data/object/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod pg;
|
||||||
|
|
||||||
|
pub use pg::PgObjectDataSource;
|
55
src/data/object/pg.rs
Normal file
55
src/data/object/pg.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
use sqlx::types::Uuid;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
use crate::core::*;
|
||||||
|
use crate::model::object::*;
|
||||||
|
|
||||||
|
pub struct PgObjectDataSource {
|
||||||
|
pool: PgPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PgObjectDataSource {
|
||||||
|
pub fn new(pool: PgPool) -> PgObjectDataSource {
|
||||||
|
PgObjectDataSource { pool }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create(&self, new: NewObject) -> Result<Object> {
|
||||||
|
let object: Object = sqlx::query_as(
|
||||||
|
"INSERT INTO objects (id, iri, data)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING *",
|
||||||
|
)
|
||||||
|
.bind(new.id)
|
||||||
|
.bind(new.iri)
|
||||||
|
.bind(new.data)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(object)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update(&self, object: &Object) -> Result<()> {
|
||||||
|
sqlx::query("UPDATE objects SET data = $1, updated_at = $2 WHERE id = $3")
|
||||||
|
.bind(&object.data)
|
||||||
|
.bind(&object.updated_at)
|
||||||
|
.bind(&object.id)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn by_id(&self, id: Uuid) -> Result<Object> {
|
||||||
|
let object: Object = sqlx::query_as("SELECT * FROM objects WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(object)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn by_iri(&self, iri: &str) -> Result<Object> {
|
||||||
|
let object: Object = sqlx::query_as("SELECT * FROM objects WHERE iri = $1")
|
||||||
|
.bind(iri)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(object)
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,10 +2,12 @@ pub mod account;
|
||||||
pub mod follow;
|
pub mod follow;
|
||||||
pub mod like;
|
pub mod like;
|
||||||
pub mod note;
|
pub mod note;
|
||||||
|
pub mod object;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
pub use account::{Account, NewAccount};
|
pub use account::{Account, NewAccount};
|
||||||
pub use follow::{Follow, NewFollow};
|
pub use follow::{Follow, NewFollow};
|
||||||
pub use like::{Like, NewLike};
|
pub use like::{Like, NewLike};
|
||||||
pub use note::{NewNote, Note};
|
pub use note::{NewNote, Note};
|
||||||
|
pub use object::{NewObject, Object};
|
||||||
pub use user::{NewUser, User};
|
pub use user::{NewUser, User};
|
||||||
|
|
18
src/model/object.rs
Normal file
18
src/model/object.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
|
use sqlx::types::Uuid;
|
||||||
|
use sqlx::FromRow;
|
||||||
|
|
||||||
|
#[derive(FromRow)]
|
||||||
|
pub struct Object {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub iri: String,
|
||||||
|
pub data: String,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
|
pub updated_at: NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NewObject {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub iri: String,
|
||||||
|
pub data: String,
|
||||||
|
}
|
|
@ -4,11 +4,14 @@ pub mod account;
|
||||||
pub mod follow;
|
pub mod follow;
|
||||||
pub mod like;
|
pub mod like;
|
||||||
pub mod note;
|
pub mod note;
|
||||||
|
pub mod object;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
use account::AccountRepo;
|
use account::AccountRepo;
|
||||||
use follow::FollowRepo;
|
use follow::FollowRepo;
|
||||||
use like::LikeRepo;
|
use like::LikeRepo;
|
||||||
use note::NoteRepo;
|
use note::NoteRepo;
|
||||||
|
use object::ObjectRepo;
|
||||||
use user::UserRepo;
|
use user::UserRepo;
|
||||||
|
|
||||||
/// The central collection of all data accessible to the app.
|
/// The central collection of all data accessible to the app.
|
||||||
|
@ -19,6 +22,7 @@ pub struct AppRepo {
|
||||||
pub follows: FollowRepo,
|
pub follows: FollowRepo,
|
||||||
pub likes: LikeRepo,
|
pub likes: LikeRepo,
|
||||||
pub notes: NoteRepo,
|
pub notes: NoteRepo,
|
||||||
|
pub objects: ObjectRepo,
|
||||||
pub users: UserRepo,
|
pub users: UserRepo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +33,7 @@ impl AppRepo {
|
||||||
follows: FollowRepo::new(db_pool.clone()),
|
follows: FollowRepo::new(db_pool.clone()),
|
||||||
likes: LikeRepo::new(db_pool.clone()),
|
likes: LikeRepo::new(db_pool.clone()),
|
||||||
notes: NoteRepo::new(db_pool.clone()),
|
notes: NoteRepo::new(db_pool.clone()),
|
||||||
|
objects: ObjectRepo::new(db_pool.clone()),
|
||||||
users: UserRepo::new(db_pool.clone()),
|
users: UserRepo::new(db_pool.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
44
src/repo/object.rs
Normal file
44
src/repo/object.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
use sqlx::types::Uuid;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
use crate::core::*;
|
||||||
|
use crate::data::object::PgObjectDataSource;
|
||||||
|
use crate::model::{NewObject, Object};
|
||||||
|
|
||||||
|
pub struct ObjectRepo {
|
||||||
|
db: PgObjectDataSource,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectRepo {
|
||||||
|
pub fn new(db_pool: PgPool) -> ObjectRepo {
|
||||||
|
ObjectRepo {
|
||||||
|
db: PgObjectDataSource::new(db_pool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create(&self, new: NewObject) -> Result<Object> {
|
||||||
|
self.db.create(new).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update<T, E>(&self, object: T) -> Result<Object>
|
||||||
|
where
|
||||||
|
T: TryInto<Sane<Object>, Error = E>,
|
||||||
|
E: Into<Error>,
|
||||||
|
{
|
||||||
|
let mut object = object.try_into().map_err(|e| e.into())?.inner();
|
||||||
|
object.updated_at = utc_now();
|
||||||
|
self.db.update(&object).await?;
|
||||||
|
Ok(object)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn by_id<U>(&self, id: U) -> Result<Object>
|
||||||
|
where
|
||||||
|
U: Into<Uuid>,
|
||||||
|
{
|
||||||
|
self.db.by_id(id.into()).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn by_iri(&self, iri: &str) -> Result<Object> {
|
||||||
|
self.db.by_iri(iri).await
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue