Files
libertas/libertas_infra/src/mappers.rs
Gabriel Kaszewski 4675285603 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.
2025-11-15 11:18:11 +01:00

189 lines
5.9 KiB
Rust

use libertas_core::models::{Album, AlbumPermission, AlbumShare, FaceRegion, Media, MediaMetadata, MediaMetadataSource, Person, PersonPermission, Role, Tag, User};
use crate::db_models::{PostgresAlbum, PostgresAlbumPermission, PostgresAlbumShare, PostgresFaceRegion, PostgresMedia, PostgresMediaMetadata, PostgresMediaMetadataSource, PostgresPerson, PostgresPersonPermission, PostgresPersonShared, PostgresRole, PostgresTag, PostgresUser};
impl From<PostgresRole> for Role {
fn from(pg_role: PostgresRole) -> Self {
match pg_role {
PostgresRole::User => Role::User,
PostgresRole::Admin => Role::Admin,
}
}
}
impl From<Role> for PostgresRole {
fn from(role: Role) -> Self {
match role {
Role::User => PostgresRole::User,
Role::Admin => PostgresRole::Admin,
}
}
}
impl From<PostgresMediaMetadataSource> for MediaMetadataSource {
fn from(pg_source: PostgresMediaMetadataSource) -> Self {
match pg_source {
PostgresMediaMetadataSource::Exif => MediaMetadataSource::Exif,
PostgresMediaMetadataSource::TrackInfo => MediaMetadataSource::TrackInfo,
}
}
}
impl From<MediaMetadataSource> for PostgresMediaMetadataSource {
fn from(source: MediaMetadataSource) -> Self {
match source {
MediaMetadataSource::Exif => PostgresMediaMetadataSource::Exif,
MediaMetadataSource::TrackInfo => PostgresMediaMetadataSource::TrackInfo,
}
}
}
impl From<PostgresUser> for User {
fn from(pg_user: PostgresUser) -> Self {
User {
id: pg_user.id,
username: pg_user.username,
email: pg_user.email,
hashed_password: pg_user.hashed_password,
created_at: pg_user.created_at,
updated_at: pg_user.updated_at,
role: Role::from(pg_user.role.as_str()),
storage_quota: pg_user.storage_quota,
storage_used: pg_user.storage_used,
}
}
}
impl From<PostgresAlbum> for Album {
fn from(pg_album: PostgresAlbum) -> Self {
Album {
id: pg_album.id,
owner_id: pg_album.owner_id,
name: pg_album.name,
description: pg_album.description,
is_public: pg_album.is_public,
created_at: pg_album.created_at,
updated_at: pg_album.updated_at,
}
}
}
impl From<PostgresMedia> for Media {
fn from(pg_media: PostgresMedia) -> Self {
Media {
id: pg_media.id,
owner_id: pg_media.owner_id,
storage_path: pg_media.storage_path,
original_filename: pg_media.original_filename,
mime_type: pg_media.mime_type,
hash: pg_media.hash,
created_at: pg_media.created_at,
thumbnail_path: pg_media.thumbnail_path,
}
}
}
impl From<PostgresMediaMetadata> for MediaMetadata {
fn from(pg_metadata: PostgresMediaMetadata) -> Self {
MediaMetadata {
id: pg_metadata.id,
media_id: pg_metadata.media_id,
source: MediaMetadataSource::from(pg_metadata.source.as_str()),
tag_name: pg_metadata.tag_name,
tag_value: pg_metadata.tag_value,
}
}
}
impl From<PostgresAlbumPermission> for AlbumPermission {
fn from(pg_permission: PostgresAlbumPermission) -> Self {
match pg_permission {
PostgresAlbumPermission::View => AlbumPermission::View,
PostgresAlbumPermission::Contribute => AlbumPermission::Contribute,
}
}
}
impl From<AlbumPermission> for PostgresAlbumPermission {
fn from(permission: AlbumPermission) -> Self {
match permission {
AlbumPermission::View => PostgresAlbumPermission::View,
AlbumPermission::Contribute => PostgresAlbumPermission::Contribute,
}
}
}
impl From<PostgresAlbumShare> for AlbumShare {
fn from(pg_share: PostgresAlbumShare) -> Self {
AlbumShare {
album_id: pg_share.album_id,
user_id: pg_share.user_id,
permission: AlbumPermission::from(pg_share.permission),
}
}
}
impl From<PostgresTag> for Tag {
fn from(pg_tag: PostgresTag) -> Self {
Tag {
id: pg_tag.id,
name: pg_tag.name,
}
}
}
impl From<PostgresPerson> for Person {
fn from(pg_person: PostgresPerson) -> Self {
Person {
id: pg_person.id,
owner_id: pg_person.owner_id,
name: pg_person.name,
thumbnail_media_id: None, // Not in the DB schema
}
}
}
impl From<PostgresFaceRegion> for FaceRegion {
fn from(pg_face: PostgresFaceRegion) -> Self {
FaceRegion {
id: pg_face.id,
media_id: pg_face.media_id,
person_id: pg_face.person_id,
x_min: pg_face.x_min,
y_min: pg_face.y_min,
x_max: pg_face.x_max,
y_max: pg_face.y_max,
}
}
}
impl From<PostgresPersonPermission> for PersonPermission {
fn from(pg_perm: PostgresPersonPermission) -> Self {
match pg_perm {
PostgresPersonPermission::View => PersonPermission::View,
PostgresPersonPermission::CanUse => PersonPermission::CanUse,
}
}
}
impl From<PersonPermission> for PostgresPersonPermission {
fn from(perm: PersonPermission) -> Self {
match perm {
PersonPermission::View => PostgresPersonPermission::View,
PersonPermission::CanUse => PostgresPersonPermission::CanUse,
}
}
}
impl From<PostgresPersonShared> for (Person, PersonPermission) {
fn from(pg_shared: PostgresPersonShared) -> Self {
let person = Person {
id: pg_shared.id,
owner_id: pg_shared.owner_id,
name: pg_shared.name,
thumbnail_media_id: None,
};
let permission = PersonPermission::from(pg_shared.permission);
(person, permission)
}
}