Skip to main content

headless_lms_models/
url_redirections.rs

1use utoipa::ToSchema;
2
3use crate::prelude::*;
4
5/// A redirect from a page's old path, created when the page moved to a new one.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
7pub struct CreatedRedirect {
8    pub page_id: Uuid,
9    pub old_url_path: String,
10    pub new_url_path: String,
11}
12
13pub async fn upsert(
14    conn: &mut PgConnection,
15    pkey_policy: PKeyPolicy<Uuid>,
16    destination_page_id: Uuid,
17    old_url_path: &str,
18    course_id: Uuid,
19) -> ModelResult<Uuid> {
20    let res = sqlx::query!(
21        "
22INSERT INTO url_redirections (id, destination_page_id, old_url_path, course_id)
23VALUES ($1, $2, $3, $4)
24ON CONFLICT (old_url_path, course_id, deleted_at) DO UPDATE SET
25    destination_page_id = $2
26RETURNING *
27        ",
28        pkey_policy.into_uuid(),
29        destination_page_id,
30        old_url_path,
31        course_id,
32    )
33    .fetch_one(conn)
34    .await?;
35    Ok(res.id)
36}