From 30336859644e5c667f75ad4094cdc75f399bff2c Mon Sep 17 00:00:00 2001 From: Asqar Arslanov Date: Fri, 18 Jul 2025 14:55:33 +0300 Subject: [PATCH] . --- .../2025-05-22-213806_setup/down.sql | 8 +++ .../migrations/2025-05-22-213806_setup/up.sql | 31 ++++++++++-- crates/evops-db/src/models.rs | 47 ++++++++++++++++-- crates/evops-db/src/schema.rs | 47 ++++++++++++++++-- crates/evops-db/src/services/event/create.rs | 36 +++++++------- crates/evops-db/src/services/event/find.rs | 11 ++--- crates/evops-db/src/services/event/list.rs | 6 +-- crates/evops-db/src/services/event/update.rs | 49 ++++++++++--------- 8 files changed, 172 insertions(+), 63 deletions(-) diff --git a/crates/evops-db/migrations/2025-05-22-213806_setup/down.sql b/crates/evops-db/migrations/2025-05-22-213806_setup/down.sql index e9c98ea..c7ecb47 100644 --- a/crates/evops-db/migrations/2025-05-22-213806_setup/down.sql +++ b/crates/evops-db/migrations/2025-05-22-213806_setup/down.sql @@ -2,6 +2,10 @@ DROP INDEX event_id_idx; DROP INDEX tag_id_idx; +DROP TABLE location_translations; + +DROP TABLE event_translations; + DROP TABLE events_to_tags; DROP TABLE tag_aliases; @@ -13,3 +17,7 @@ DROP TABLE events; DROP TABLE tags; DROP TABLE users; + +DROP TABLE locations; + +DROP TABLE languages; diff --git a/crates/evops-db/migrations/2025-05-22-213806_setup/up.sql b/crates/evops-db/migrations/2025-05-22-213806_setup/up.sql index 7e34f5c..72e1b6e 100644 --- a/crates/evops-db/migrations/2025-05-22-213806_setup/up.sql +++ b/crates/evops-db/migrations/2025-05-22-213806_setup/up.sql @@ -1,3 +1,12 @@ +CREATE TABLE languages ( + id uuid PRIMARY KEY, + name text UNIQUE NOT NULL +); + +CREATE TABLE locations ( + id uuid PRIMARY KEY +); + CREATE TABLE users ( id uuid PRIMARY KEY, name text NOT NULL @@ -18,9 +27,10 @@ CREATE TABLE tag_aliases ( CREATE TABLE events ( id uuid PRIMARY KEY, - title text NOT NULL, - description text NOT NULL, - author_id uuid NOT NULL REFERENCES users (id), + author_id uuid REFERENCES users (id), + primary_language_id uuid REFERENCES languages (id), + start timestamptz, + location_id uuid REFERENCES locations (id), with_attendance bool NOT NULL, created_at timestamptz NOT NULL, modified_at timestamptz NOT NULL @@ -40,3 +50,18 @@ CREATE TABLE events_to_tags ( tag_id uuid REFERENCES tags (id), PRIMARY KEY (event_id, tag_id) ); + +CREATE TABLE event_translations ( + event_id uuid REFERENCES events (id), + language_id uuid REFERENCES languages (id), + PRIMARY KEY (event_id, language_id), + title text NOT NULL, + description text NOT NULL +); + +CREATE TABLE location_translations ( + location_id uuid REFERENCES locations (id), + language_id uuid REFERENCES languages (id), + PRIMARY KEY (location_id, language_id), + name text NOT NULL +); diff --git a/crates/evops-db/src/models.rs b/crates/evops-db/src/models.rs index 343328e..728c6f2 100644 --- a/crates/evops-db/src/models.rs +++ b/crates/evops-db/src/models.rs @@ -4,6 +4,21 @@ use uuid::Uuid; use crate::schema; +#[derive(Queryable, Selectable, Identifiable)] +#[diesel(table_name = schema::languages)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct Language { + pub id: Uuid, + pub name: String, +} + +#[derive(Queryable, Selectable, Identifiable)] +#[diesel(table_name = schema::locations)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct Location { + pub id: Uuid, +} + #[derive(Queryable, Selectable, Identifiable)] #[diesel(table_name = schema::users)] #[diesel(check_for_backend(diesel::pg::Pg))] @@ -37,9 +52,10 @@ pub struct TagAlias { #[diesel(check_for_backend(diesel::pg::Pg))] pub struct Event { pub id: Uuid, - pub title: String, - pub description: String, - pub author_id: Uuid, + pub author_id: Option, + pub primary_language_id: Option, + pub start: Option>, + pub location_id: Option, pub with_attendance: bool, pub created_at: DateTime, pub modified_at: DateTime, @@ -65,3 +81,28 @@ pub struct EventToTag { pub event_id: Uuid, pub tag_id: Uuid, } + +#[derive(Queryable, Selectable, Identifiable, Associations)] +#[diesel(table_name = schema::event_translations)] +#[diesel(belongs_to(Event))] +#[diesel(belongs_to(Language))] +#[diesel(primary_key(event_id, language_id))] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct EventTranslation { + pub event_id: Uuid, + pub language_id: Uuid, + title: String, + description: String, +} + +#[derive(Queryable, Selectable, Identifiable, Associations)] +#[diesel(table_name = schema::location_translations)] +#[diesel(belongs_to(Location))] +#[diesel(belongs_to(Language))] +#[diesel(primary_key(location_id, language_id))] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct LocationTranslation { + pub location_id: Uuid, + pub language_id: Uuid, + name: String, +} diff --git a/crates/evops-db/src/schema.rs b/crates/evops-db/src/schema.rs index cd74cac..f1b7354 100644 --- a/crates/evops-db/src/schema.rs +++ b/crates/evops-db/src/schema.rs @@ -9,11 +9,21 @@ diesel::table! { } diesel::table! { - events (id) { - id -> Uuid, + event_translations (event_id, language_id) { + event_id -> Uuid, + language_id -> Uuid, title -> Text, description -> Text, - author_id -> Uuid, + } +} + +diesel::table! { + events (id) { + id -> Uuid, + author_id -> Nullable, + primary_language_id -> Nullable, + start -> Nullable, + location_id -> Nullable, with_attendance -> Bool, created_at -> Timestamptz, modified_at -> Timestamptz, @@ -27,6 +37,27 @@ diesel::table! { } } +diesel::table! { + languages (id) { + id -> Uuid, + name -> Text, + } +} + +diesel::table! { + location_translations (location_id, language_id) { + location_id -> Uuid, + language_id -> Uuid, + name -> Text, + } +} + +diesel::table! { + locations (id) { + id -> Uuid, + } +} + diesel::table! { tag_aliases (tag_id, alias) { tag_id -> Uuid, @@ -49,15 +80,25 @@ diesel::table! { } diesel::joinable!(event_images -> events (event_id)); +diesel::joinable!(event_translations -> events (event_id)); +diesel::joinable!(event_translations -> languages (language_id)); +diesel::joinable!(events -> languages (primary_language_id)); +diesel::joinable!(events -> locations (location_id)); diesel::joinable!(events -> users (author_id)); diesel::joinable!(events_to_tags -> events (event_id)); diesel::joinable!(events_to_tags -> tags (tag_id)); +diesel::joinable!(location_translations -> languages (language_id)); +diesel::joinable!(location_translations -> locations (location_id)); diesel::joinable!(tag_aliases -> tags (tag_id)); diesel::allow_tables_to_appear_in_same_query!( event_images, + event_translations, events, events_to_tags, + languages, + location_translations, + locations, tag_aliases, tags, users, diff --git a/crates/evops-db/src/services/event/create.rs b/crates/evops-db/src/services/event/create.rs index b2f472b..8df8478 100644 --- a/crates/evops-db/src/services/event/create.rs +++ b/crates/evops-db/src/services/event/create.rs @@ -14,9 +14,10 @@ use crate::schema; #[diesel(check_for_backend(diesel::pg::Pg))] struct NewEvent<'a> { id: Uuid, - title: &'a str, - description: &'a str, author_id: Uuid, + primary_language_id: Option, + start: Option>, + location_id: Option, with_attendance: bool, created_at: &'a DateTime, modified_at: &'a DateTime, @@ -50,22 +51,21 @@ impl crate::Database { let event_id = evops_models::EventId::new(Uuid::now_v7()); let now = Utc::now(); - diesel::insert_into(schema::events::table) - .values(self::NewEvent { - id: event_id.into_inner(), - title: form.title.as_ref(), - description: form.description.as_ref(), - author_id: form.author_id.into_inner(), - with_attendance: form.with_attendance, - created_at: &now, - modified_at: &now, - }) - .execute(conn) - .await - .map_err(|e| match e { - diesel::result::Error::NotFound => ApiError::NotFound(e.to_string()), - _ => e.into(), - })?; + todo!(); + // diesel::insert_into(schema::events::table) + // .values(self::NewEvent { + // id: event_id.into_inner(), + // author_id: form.author_id.into_inner(), + // with_attendance: form.with_attendance, + // created_at: &now, + // modified_at: &now, + // }) + // .execute(conn) + // .await + // .map_err(|e| match e { + // diesel::result::Error::NotFound => ApiError::NotFound(e.to_string()), + // _ => e.into(), + // })?; diesel::insert_into(schema::events_to_tags::table) .values({ diff --git a/crates/evops-db/src/services/event/find.rs b/crates/evops-db/src/services/event/find.rs index b6427a7..a7a1774 100644 --- a/crates/evops-db/src/services/event/find.rs +++ b/crates/evops-db/src/services/event/find.rs @@ -16,16 +16,11 @@ impl crate::Database { let event_model = Self::find_event_model(&mut self.conn, id).await?; let event = evops_models::Event { id, - author: { - self.find_user(evops_models::UserId::new(event_model.author_id)) - .await? - }, + author: { self.find_user(evops_models::UserId::new(todo!())).await? }, image_ids: Self::image_ids_of_event_model_sorted(&mut self.conn, &event_model).await?, tags: Self::tags_of_event_model(&mut self.conn, &event_model).await?, - title: unsafe { evops_models::EventTitle::new_unchecked(event_model.title) }, - description: unsafe { - evops_models::EventDescription::new_unchecked(event_model.description) - }, + title: unsafe { evops_models::EventTitle::new_unchecked(todo!()) }, + description: unsafe { evops_models::EventDescription::new_unchecked(todo!()) }, with_attendance: event_model.with_attendance, created_at: event_model.created_at, modified_at: event_model.modified_at, diff --git a/crates/evops-db/src/services/event/list.rs b/crates/evops-db/src/services/event/list.rs index 299737d..a7c4556 100644 --- a/crates/evops-db/src/services/event/list.rs +++ b/crates/evops-db/src/services/event/list.rs @@ -119,10 +119,8 @@ impl crate::Database { .collect(); unsafe { evops_models::EventImageIds::new_unchecked(inner_value) } }, - title: unsafe { evops_models::EventTitle::new_unchecked(event.title) }, - description: unsafe { - evops_models::EventDescription::new_unchecked(event.description) - }, + title: unsafe { evops_models::EventTitle::new_unchecked(todo!()) }, + description: unsafe { evops_models::EventDescription::new_unchecked(todo!()) }, tags: { let inner_value = tags .remove(&event.id) diff --git a/crates/evops-db/src/services/event/update.rs b/crates/evops-db/src/services/event/update.rs index eb1661c..5517a68 100644 --- a/crates/evops-db/src/services/event/update.rs +++ b/crates/evops-db/src/services/event/update.rs @@ -41,30 +41,31 @@ impl crate::Database { id: evops_models::EventId, form: &evops_models::UpdateEventForm, ) -> ApiResult<()> { - diesel::update(schema::events::table.find(id.into_inner())) - .set({ - let description_eq = { - form.description - .as_ref() - .map(|it| schema::events::description.eq(it.as_ref())) - }; - let title_eq = { - form.title - .as_ref() - .map(|it| schema::events::title.eq(it.as_ref())) - }; - let track_attendance_eq = { - form.track_attendance - .map(|it| schema::events::with_attendance.eq(it)) - }; - (description_eq, title_eq, track_attendance_eq) - }) - .execute(conn) - .await - .map_err(|e| match e { - diesel::result::Error::NotFound => ApiError::NotFound(e.to_string()), - _ => e.into(), - })?; + todo!(); + // diesel::update(schema::events::table.find(id.into_inner())) + // .set({ + // let description_eq = { + // form.description + // .as_ref() + // .map(|it| schema::events::description.eq(it.as_ref())) + // }; + // let title_eq = { + // form.title + // .as_ref() + // .map(|it| schema::events::title.eq(it.as_ref())) + // }; + // let track_attendance_eq = { + // form.track_attendance + // .map(|it| schema::events::with_attendance.eq(it)) + // }; + // (description_eq, title_eq, track_attendance_eq) + // }) + // .execute(conn) + // .await + // .map_err(|e| match e { + // diesel::result::Error::NotFound => ApiError::NotFound(e.to_string()), + // _ => e.into(), + // })?; Ok(()) }