feat: Implement person and tag management services
- Added `Person` and `Tag` models to the core library. - Created `PersonService` and `TagService` traits with implementations for managing persons and tags. - Introduced repositories for `Person`, `Tag`, `FaceRegion`, and `PersonShare` with PostgreSQL support. - Updated authorization logic to include permissions for accessing and editing persons. - Enhanced the schema to support new models and relationships. - Implemented database migrations for new tables related to persons and tags. - Added request and response structures for API interactions with persons and tags.
This commit is contained in:
98
libertas_infra/src/repositories/person_repository.rs
Normal file
98
libertas_infra/src/repositories/person_repository.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{error::{CoreError, CoreResult}, models::Person, repositories::PersonRepository};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::db_models::PostgresPerson;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PostgresPersonRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresPersonRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl PersonRepository for PostgresPersonRepository {
|
||||
async fn create(&self, person: Person) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO people (id, owner_id, name)
|
||||
VALUES ($1, $2, $3)
|
||||
"#,
|
||||
person.id,
|
||||
person.owner_id,
|
||||
person.name
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Person>> {
|
||||
let pg_person = sqlx::query_as!(
|
||||
PostgresPerson,
|
||||
r#"
|
||||
SELECT id, owner_id, name
|
||||
FROM people
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(pg_person.map(Person::from))
|
||||
}
|
||||
|
||||
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Person>> {
|
||||
let pg_people = sqlx::query_as!(
|
||||
PostgresPerson,
|
||||
r#"
|
||||
SELECT id, owner_id, name
|
||||
FROM people
|
||||
WHERE owner_id = $1
|
||||
"#,
|
||||
user_id
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(pg_people.into_iter().map(Person::from).collect())
|
||||
}
|
||||
|
||||
async fn update(&self, person: Person) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
UPDATE people
|
||||
SET name = $1
|
||||
WHERE id = $2
|
||||
"#,
|
||||
person.name,
|
||||
person.id
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(&self, id: Uuid) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
DELETE FROM people
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user