mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(blog) add adj (adjacent posts) to get_post
This commit is contained in:
2 files changed
+76
-4
No files matched your search
@@ -102,11 +102,53 @@ WHERE post_id = $1;
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// todo: next, prev post
|
let adjacent = match query(r"
|
||||||
|
WITH target_post AS (SELECT t.date FROM meta t WHERE post_id = $1),
|
||||||
|
prev AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
||||||
|
FROM meta t
|
||||||
|
WHERE t.date < (SELECT date FROM target_post)
|
||||||
|
ORDER BY t.date DESC
|
||||||
|
LIMIT 1),
|
||||||
|
next AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
||||||
|
FROM meta t
|
||||||
|
WHERE t.date > (SELECT date FROM target_post)
|
||||||
|
ORDER BY t.date
|
||||||
|
LIMIT 1)
|
||||||
|
SELECT json_strip_nulls(json_build_object(
|
||||||
|
'prev',
|
||||||
|
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
||||||
|
FROM prev),
|
||||||
|
'next',
|
||||||
|
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
||||||
|
FROM next)
|
||||||
|
)) AS result;
|
||||||
|
"
|
||||||
|
)
|
||||||
|
.bind(post_id)
|
||||||
|
.fetch_one(&db_connection)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => {res.get::<Value, _>("result")}
|
||||||
|
Err(e) => {
|
||||||
|
error!(
|
||||||
|
"Database error on Reading `{}` from table `{}`: <{}>: {}",
|
||||||
|
"adjoint posts data",
|
||||||
|
"meta",
|
||||||
|
post_id.to_string(),
|
||||||
|
e.to_string()
|
||||||
|
);
|
||||||
|
return Err((
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json::from(json!({"error": e.to_string()})),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
let mut res = post.as_object().unwrap().to_owned();
|
let mut res = post.as_object().unwrap().to_owned();
|
||||||
res.insert("tags".to_string(), tags.into());
|
res.insert("tags".to_string(), tags.into());
|
||||||
res.insert("meta".to_string(), meta.into());
|
res.insert("meta".to_string(), meta);
|
||||||
|
res.insert("adj".to_string(), adjacent);
|
||||||
|
|
||||||
Ok(Json::from(json!(res)))
|
Ok(Json::from(json!(res)))
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,8 @@ DROP table meta;
|
|||||||
DO
|
DO
|
||||||
$$
|
$$
|
||||||
DECLARE
|
DECLARE
|
||||||
_post_id UUID := 'd5a209d4-93b1-474e-8e4e-9c78d6e6e763'; -- test data
|
_post_id UUID := '{}';
|
||||||
_meta_cols JSONB := '{"title": " title", "tags": "example tags", "new_col1": "new_value1", "new_col3": "new_value2"}'; -- test data
|
_meta_cols JSONB := '{}';
|
||||||
_exists BOOLEAN;
|
_exists BOOLEAN;
|
||||||
_col_name TEXT;
|
_col_name TEXT;
|
||||||
_col_value TEXT;
|
_col_value TEXT;
|
||||||
@@ -17,11 +17,16 @@ $$
|
|||||||
_insert_columns TEXT := 'post_id';
|
_insert_columns TEXT := 'post_id';
|
||||||
_insert_values TEXT := quote_literal(_post_id);
|
_insert_values TEXT := quote_literal(_post_id);
|
||||||
_update_set TEXT := '';
|
_update_set TEXT := '';
|
||||||
|
_blacklist TEXT[] := ARRAY ['id', 'tags'];
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT EXISTS(SELECT 1 FROM meta WHERE post_id = _post_id) INTO _exists;
|
SELECT EXISTS(SELECT 1 FROM meta WHERE post_id = _post_id) INTO _exists;
|
||||||
|
|
||||||
FOR _col_name, _col_value IN SELECT key, value FROM jsonb_each_text(_meta_cols)
|
FOR _col_name, _col_value IN SELECT key, value FROM jsonb_each_text(_meta_cols)
|
||||||
LOOP
|
LOOP
|
||||||
|
IF _col_name = ANY (_blacklist) THEN
|
||||||
|
CONTINUE;
|
||||||
|
END IF;
|
||||||
|
|
||||||
IF NOT EXISTS (SELECT 1
|
IF NOT EXISTS (SELECT 1
|
||||||
FROM information_schema.columns
|
FROM information_schema.columns
|
||||||
WHERE table_name = 'meta'
|
WHERE table_name = 'meta'
|
||||||
@@ -47,3 +52,28 @@ $$
|
|||||||
END IF;
|
END IF;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql;
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
SELECT jsonb_strip_nulls(to_jsonb(t)) AS meta
|
||||||
|
FROM meta t
|
||||||
|
WHERE post_id = '8ca3fa2b-f040-448c-8885-81aab56db9bb';
|
||||||
|
|
||||||
|
WITH target_post AS (SELECT t.date FROM meta t WHERE post_id = '8ca3fa2b-f040-448c-8885-81aab56db9bb'),
|
||||||
|
prev AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
||||||
|
FROM meta t
|
||||||
|
WHERE t.date < (SELECT date FROM target_post)
|
||||||
|
ORDER BY t.date DESC
|
||||||
|
LIMIT 1),
|
||||||
|
next AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
||||||
|
FROM meta t
|
||||||
|
WHERE t.date > (SELECT date FROM target_post)
|
||||||
|
ORDER BY t.date
|
||||||
|
LIMIT 1)
|
||||||
|
SELECT json_strip_nulls(json_build_object(
|
||||||
|
'prev',
|
||||||
|
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
||||||
|
FROM prev),
|
||||||
|
'next',
|
||||||
|
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
||||||
|
FROM next)
|
||||||
|
)) AS result;
|
||||||
|
|
||||||
Reference in new issue
Block a user