Skip to content
Merged
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ userver_target_generate_chaotic(${PROJECT_NAME}-chgen
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC ${PROJECT_NAME}-chgen)

userver_add_sql_library(
${PROJECT_NAME}_sql
NAMESPACE real_medium
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
SQL_FILES src/db/*.sql
)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC ${PROJECT_NAME}_sql)

target_link_libraries(${PROJECT_NAME}_objs PUBLIC cpp-jwt)

find_package(ICU COMPONENTS uc i18n REQUIRED)
Expand Down
3 changes: 1 addition & 2 deletions src/cache/articles_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace real_medium::cache::articles_cache {

userver::storages::postgres::Query ArticlesCachePolicy::kQuery =
userver::storages::postgres::Query(real_medium::sql::kSelectFullArticleInfo.c_str());
userver::storages::postgres::Query ArticlesCachePolicy::kQuery = real_medium::sql::kSelectFullArticleInfo;

void ArticlesCacheContainer::insert_or_assign(Key&& key, Article&& article) {
auto article_ptr = std::make_shared<const Article>(std::move(article));
Expand Down
3 changes: 1 addition & 2 deletions src/cache/comments_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace real_medium::cache::comments_cache {

userver::storages::postgres::Query CommentCachePolicy::kQuery =
userver::storages::postgres::Query(real_medium::sql::kSelectCachedComments.c_str());
userver::storages::postgres::Query CommentCachePolicy::kQuery = real_medium::sql::kSelectCachedComments;

void CommentsCacheContainer::insert_or_assign(
real_medium::cache::comments_cache::CommentsCacheContainer::Key&& comment_id,
Expand Down
17 changes: 17 additions & 0 deletions src/db/add_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
WITH comment AS (
INSERT INTO real_medium.comments(body, user_id, article_id)
VALUES ($1, $2, $3)
RETURNING *
)
SELECT
comment.comment_id,
comment.created_at,
comment.updated_at,
comment.body,
(
SELECT
ROW(users.username, users.bio, users.image, false)::real_medium.profile
FROM real_medium.users
WHERE user_id = $2
) AS author
FROM comment
1 change: 1 addition & 0 deletions src/db/create_article.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.create_article($1, $2, $3, $4, $5, $6)
3 changes: 3 additions & 0 deletions src/db/decrement_favorites_count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE real_medium.articles
SET favorites_count=favorites_count - 1
WHERE article_id=$1
1 change: 1 addition & 0 deletions src/db/delete_article_by_slug.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.delete_article_by_slug($1, $2)
3 changes: 3 additions & 0 deletions src/db/delete_comment_by_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DELETE FROM real_medium.comments
WHERE comment_id = $1 AND user_id = $2
RETURNING *
6 changes: 6 additions & 0 deletions src/db/delete_favorite_pair.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WITH tmp(article_id, user_id) AS (
SELECT article_id, $1 FROM real_medium.articles WHERE slug=$2
)
DELETE FROM real_medium.favorites
WHERE (article_id, user_id) IN (SELECT article_id, user_id FROM tmp)
RETURNING article_id
1 change: 1 addition & 0 deletions src/db/find_articles_by_filters.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.get_articles_by_filters($1, $2, $3, $4, $5, $6)
1 change: 1 addition & 0 deletions src/db/find_articles_by_followed_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.get_feed_articles($1, $2, $3)
6 changes: 6 additions & 0 deletions src/db/find_comment_by_id_and_slug.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WITH article AS (
SELECT article_id FROM real_medium.articles WHERE slug = $2
)
SELECT * FROM real_medium.comments
JOIN article ON article.article_id = real_medium.comments.article_id
WHERE comment_id = $1
18 changes: 18 additions & 0 deletions src/db/find_comments_by_article_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
WITH comments AS (
SELECT * FROM real_medium.comments WHERE article_id = $1
)
SELECT
comments.comment_id,
comments.created_at,
comments.updated_at,
comments.body,
(
SELECT
ROW(users.username, users.bio, users.image,
CASE WHEN EXISTS (SELECT 1 FROM real_medium.followers
WHERE followers.followed_user_id = comments.user_id AND followers.follower_user_id = $2)
THEN true ELSE false END)::real_medium.profile
FROM real_medium.users
WHERE user_id = comments.user_id
) AS author
FROM comments
1 change: 1 addition & 0 deletions src/db/find_id_article_by_slug.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT article_id FROM real_medium.articles WHERE slug = $1
1 change: 1 addition & 0 deletions src/db/find_user_by_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM real_medium.users WHERE user_id = $1
1 change: 1 addition & 0 deletions src/db/find_user_id_by_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT user_id FROM real_medium.users WHERE username = $1
13 changes: 13 additions & 0 deletions src/db/following_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH profile AS (
SELECT * FROM real_medium.users WHERE user_id = $1
), following AS (
INSERT INTO real_medium.followers(followed_user_id, follower_user_id) VALUES ($1, $2)
ON CONFLICT DO NOTHING
RETURNING *
)
SELECT
profile.username,
profile.bio,
profile.image,
CASE WHEN EXISTS (SELECT 1 FROM following) THEN TRUE ELSE FALSE END
FROM profile
1 change: 1 addition & 0 deletions src/db/get_article_id_by_slug.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.get_article_id_by_slug($1)
1 change: 1 addition & 0 deletions src/db/get_article_with_author_profile.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.get_article_with_author_profile($1, $2)
1 change: 1 addition & 0 deletions src/db/get_article_with_author_profile_by_slug.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT real_medium.get_article_with_author_profile_by_slug($1, $2)
9 changes: 9 additions & 0 deletions src/db/get_profile_by_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
WITH profile AS (
SELECT * FROM real_medium.users WHERE username = $1
)
SELECT profile.username, profile.bio, profile.image,
CASE WHEN EXISTS (
SELECT 1 FROM real_medium.followers
WHERE followed_user_id = profile.user_id AND follower_user_id = $2
) THEN true ELSE false END AS following
FROM profile
1 change: 1 addition & 0 deletions src/db/get_salt_by_email.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT salt FROM real_medium.users WHERE email = $1
3 changes: 3 additions & 0 deletions src/db/increment_favorites_count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE real_medium.articles
SET favorites_count=favorites_count + 1
WHERE article_id=$1
6 changes: 6 additions & 0 deletions src/db/insert_favorite_pair.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WITH tmp(article_id, user_id) AS (
SELECT article_id, $1 FROM real_medium.articles WHERE slug=$2
)
INSERT INTO real_medium.favorites(article_id, user_id) (SELECT article_id, user_id FROM tmp)
ON CONFLICT DO NOTHING
RETURNING article_id
3 changes: 3 additions & 0 deletions src/db/insert_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO real_medium.users(username, email, bio, image, password_hash, salt)
VALUES($1, $2, $3, $4, $5, $6)
RETURNING *
1 change: 1 addition & 0 deletions src/db/is_profile_following.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RETURN EXISTS (SELECT 1 FROM real_medium.followers WHERE follower_user_id = $1 AND followed_user_id = $2)
15 changes: 15 additions & 0 deletions src/db/select_cached_comments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT c.comment_id,
c.created_at AS createdAt,
c.updated_at AS updatedAt,
c.body,
c.user_id,
a.slug,
ROW(u.user_id, u.username, u.email, u.bio, u.image, u.password_hash, u.salt)::real_medium.user AS author_info,
ARRAY(
SELECT follower_user_id
FROM real_medium.followers fl
WHERE fl.followed_user_id = c.user_id
) AS following
FROM real_medium.comments c
JOIN real_medium.articles a ON a.article_id = c.article_id
JOIN real_medium.users u ON c.user_id = u.user_id
33 changes: 33 additions & 0 deletions src/db/select_full_article_info.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
SELECT a.article_id AS articleId,
a.title,
a.slug,
a.description,
a.body,
ARRAY(
SELECT tag_name
FROM real_medium.tag_list tl
JOIN real_medium.article_tag at ON tl.tag_id = at.tag_id
WHERE at.article_id = a.article_id
) AS tags,
a.created_at AS createdAt,
a.updated_at AS updatedAt,
ARRAY(
SELECT user_id
FROM real_medium.favorites f
WHERE f.article_id = a.article_id
) AS article_favorited_by_user_ids,

ARRAY(
SELECT u.username
FROM real_medium.favorites f
JOIN real_medium.users u ON f.user_id = u.user_id
WHERE f.article_id = a.article_id
) AS article_favorited_by_usernames,
ARRAY(
SELECT follower_user_id
FROM real_medium.followers fl
WHERE fl.followed_user_id = a.user_id
) AS author_followed_by_user_ids,
ROW(u.user_id, u.username, u.email, u.bio, u.image, u.password_hash, u.salt)::real_medium.user AS author_info
FROM real_medium.articles a
JOIN real_medium.users u ON a.user_id = u.user_id
2 changes: 2 additions & 0 deletions src/db/select_user_by_email_and_password.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT * FROM real_medium.users
WHERE email = $1 AND password_hash = $2
Loading