Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/evops-db/migrations/2025-05-22-213806_setup/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,3 +17,7 @@ DROP TABLE events;
DROP TABLE tags;

DROP TABLE users;

DROP TABLE locations;

DROP TABLE languages;
31 changes: 28 additions & 3 deletions crates/evops-db/migrations/2025-05-22-213806_setup/up.sql
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
);
47 changes: 44 additions & 3 deletions crates/evops-db/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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<Uuid>,
pub primary_language_id: Option<Uuid>,
pub start: Option<DateTime<Utc>>,
pub location_id: Option<Uuid>,
pub with_attendance: bool,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
Expand All @@ -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,
}
47 changes: 44 additions & 3 deletions crates/evops-db/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uuid>,
primary_language_id -> Nullable<Uuid>,
start -> Nullable<Timestamptz>,
location_id -> Nullable<Uuid>,
with_attendance -> Bool,
created_at -> Timestamptz,
modified_at -> Timestamptz,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
36 changes: 18 additions & 18 deletions crates/evops-db/src/services/event/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uuid>,
start: Option<DateTime<Utc>>,
location_id: Option<Uuid>,
with_attendance: bool,
created_at: &'a DateTime<Utc>,
modified_at: &'a DateTime<Utc>,
Expand Down Expand Up @@ -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({
Expand Down
11 changes: 3 additions & 8 deletions crates/evops-db/src/services/event/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions crates/evops-db/src/services/event/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 25 additions & 24 deletions crates/evops-db/src/services/event/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down