diff --git a/src/cache/articles_cache.cpp b/src/cache/articles_cache.cpp index bf5fc8e..98e6be0 100644 --- a/src/cache/articles_cache.cpp +++ b/src/cache/articles_cache.cpp @@ -5,30 +5,30 @@ namespace real_medium::cache::articles_cache { userver::storages::postgres::Query ArticlesCachePolicy::kQuery = - userver::storages::postgres::Query(real_medium::sql::kSelectFullArticleInfo.data()); + userver::storages::postgres::Query(real_medium::sql::kSelectFullArticleInfo.c_str()); void ArticlesCacheContainer::insert_or_assign(Key&& key, Article&& article) { - auto articlePtr = std::make_shared(std::move(article)); - auto oldValue = articleByKey_.find(key); - if (oldValue != articleByKey_.end()) { - articleBySlug_.erase(oldValue->second->slug); - for (const auto& oldFollower : oldValue->second->authorFollowedByUsersIds) - if (articlePtr->authorFollowedByUsersIds.find(oldFollower) == articlePtr->authorFollowedByUsersIds.end()) - articlesByFollower_[oldFollower].erase(articlePtr->articleId); + auto article_ptr = std::make_shared(std::move(article)); + auto old_value = article_by_key_.find(key); + if (old_value != article_by_key_.end()) { + article_by_slug_.erase(old_value->second->slug); + for (const auto& oldFollower : old_value->second->authorFollowedByUsersIds) + if (article_ptr->authorFollowedByUsersIds.find(oldFollower) == article_ptr->authorFollowedByUsersIds.end()) + articles_by_follower_[oldFollower].erase(article_ptr->articleId); } - articleByKey_.insert_or_assign(key, articlePtr); - articleBySlug_.insert_or_assign(articlePtr->slug, articlePtr); - for (const auto& follower : articlePtr->authorFollowedByUsersIds) - articlesByFollower_[follower].insert_or_assign(articlePtr->articleId, articlePtr); - recentArticles_.insert_or_assign({articlePtr->createdAt, articlePtr->articleId}, articlePtr); + article_by_key_.insert_or_assign(key, article_ptr); + article_by_slug_.insert_or_assign(article_ptr->slug, article_ptr); + for (const auto& follower : article_ptr->authorFollowedByUsersIds) + articles_by_follower_[follower].insert_or_assign(article_ptr->articleId, article_ptr); + recent_articles_.insert_or_assign({article_ptr->createdAt, article_ptr->articleId}, article_ptr); } -size_t ArticlesCacheContainer::size() const { return articleByKey_.size(); } +size_t ArticlesCacheContainer::size() const { return article_by_key_.size(); } ArticlesCacheContainer::ArticlePtr ArticlesCacheContainer::findArticleBySlug(const Slug& slug) const { - auto it = articleBySlug_.find(slug); - if (it == articleBySlug_.end()) return nullptr; + auto it = article_by_slug_.find(slug); + if (it == article_by_slug_.end()) return nullptr; return it->second; } @@ -37,7 +37,7 @@ std::vector ArticlesCacheContainer::getRecen ) const { std::vector articles; int offset = 0; - for (const auto& it : recentArticles_) { + for (const auto& it : recent_articles_) { if (filter.limit && articles.size() >= userver::utils::numeric_cast(filter.limit)) break; const auto& tags = it.second->tags; @@ -57,16 +57,16 @@ std::vector ArticlesCacheContainer::getRecen return articles; } std::vector -ArticlesCacheContainer::getFeed(real_medium::handlers::FeedArticleFilterDTO& filter, UserId authId) const { - auto followedArticlesUMap = articlesByFollower_.find(authId); - if (followedArticlesUMap == articlesByFollower_.end()) return {}; +ArticlesCacheContainer::getFeed(real_medium::handlers::FeedArticleFilterDTO& filter, UserId auth_id) const { + auto followed_articles_umap = articles_by_follower_.find(auth_id); + if (followed_articles_umap == articles_by_follower_.end()) return {}; RecentArticlesMap followedArticlesOrdered; - for (const auto& it : followedArticlesUMap->second) + for (const auto& it : followed_articles_umap->second) followedArticlesOrdered.insert_or_assign({it.second->createdAt, it.second->articleId}, it.second); std::vector articles; - ; + int offset = 0; for (const auto& it : followedArticlesOrdered) { if (filter.limit && articles.size() >= userver::utils::numeric_cast(filter.limit)) break; diff --git a/src/cache/articles_cache.hpp b/src/cache/articles_cache.hpp index ec4af9f..4868b1c 100644 --- a/src/cache/articles_cache.hpp +++ b/src/cache/articles_cache.hpp @@ -1,14 +1,18 @@ #pragma once + #include #include #include + #include #include + #include "db/sql.hpp" #include "dto/filter.hpp" #include "models/article.hpp" namespace real_medium::cache::articles_cache { + class ArticlesCacheContainer { using Timepoint = userver::storages::postgres::TimePointTz; using Slug = std::string; @@ -40,10 +44,10 @@ class ArticlesCacheContainer { }; using RecentArticlesMap = std::map>; - std::unordered_map articleByKey_; - std::unordered_map articleBySlug_; - std::unordered_map> articlesByFollower_; - RecentArticlesMap recentArticles_; + std::unordered_map article_by_key_; + std::unordered_map article_by_slug_; + std::unordered_map> articles_by_follower_; + RecentArticlesMap recent_articles_; }; struct ArticlesCachePolicy { @@ -55,5 +59,7 @@ struct ArticlesCachePolicy { static constexpr auto kUpdatedField = "updated_at"; using UpdatedFieldType = userver::storages::postgres::TimePointTz; }; + using ArticlesCache = ::userver::components::PostgreCache; + } // namespace real_medium::cache::articles_cache diff --git a/src/cache/comments_cache.cpp b/src/cache/comments_cache.cpp index 512fb4b..c2cac07 100644 --- a/src/cache/comments_cache.cpp +++ b/src/cache/comments_cache.cpp @@ -9,23 +9,23 @@ namespace real_medium::cache::comments_cache { userver::storages::postgres::Query CommentCachePolicy::kQuery = - userver::storages::postgres::Query(real_medium::sql::kSelectCachedComments.data()); + userver::storages::postgres::Query(real_medium::sql::kSelectCachedComments.c_str()); void CommentsCacheContainer::insert_or_assign( - real_medium::cache::comments_cache::CommentsCacheContainer::Key&& commentId, + real_medium::cache::comments_cache::CommentsCacheContainer::Key&& comment_id, real_medium::cache::comments_cache::CommentsCacheContainer::Comment&& comment ) { - auto commentPtr = std::make_shared(std::move(comment)); - if (comment_to_key_.count(commentId)) { - auto& oldSlug = comment_to_key_[commentId]->slug; - if (oldSlug != commentPtr->slug) { - auto& comments = comments_to_slug_[oldSlug]; - comments_to_slug_[commentPtr->slug] = comments; - comments_to_slug_.erase(oldSlug); + auto comment_ptr = std::make_shared(std::move(comment)); + if (comment_to_key_.count(comment_id)) { + auto& old_slug = comment_to_key_[comment_id]->slug; + if (old_slug != comment_ptr->slug) { + auto& comments = comments_to_slug_[old_slug]; + comments_to_slug_[comment_ptr->slug] = comments; + comments_to_slug_.erase(old_slug); } } - comment_to_key_.insert_or_assign(commentId, commentPtr); - comments_to_slug_[commentPtr->slug].insert_or_assign(commentId, commentPtr); + comment_to_key_.insert_or_assign(comment_id, comment_ptr); + comments_to_slug_[comment_ptr->slug].insert_or_assign(comment_id, comment_ptr); }; size_t CommentsCacheContainer::size() const { return comment_to_key_.size(); } diff --git a/src/db/sql.hpp b/src/db/sql.hpp index 43dccd6..7395dde 100644 --- a/src/db/sql.hpp +++ b/src/db/sql.hpp @@ -1,21 +1,21 @@ #pragma once -#include +#include namespace real_medium::sql { -inline constexpr std::string_view kInsertUser = R"~( +inline constexpr userver::utils::zstring_view kInsertUser = R"~( INSERT INTO real_medium.users(username, email, bio, image, password_hash, salt) VALUES($1, $2, $3, $4, $5, $6) RETURNING * )~"; -inline constexpr std::string_view kSelectUserByEmailAndPassword = R"~( +inline constexpr userver::utils::zstring_view kSelectUserByEmailAndPassword = R"~( SELECT * FROM real_medium.users WHERE email = $1 AND password_hash = $2 )~"; -inline constexpr std::string_view kUpdateUser = R"~( +inline constexpr userver::utils::zstring_view kUpdateUser = R"~( UPDATE real_medium.users SET username = COALESCE($2, username), email = COALESCE($3, email), @@ -27,16 +27,16 @@ WHERE user_id = $1 RETURNING * )~"; -inline constexpr std::string_view kFindUserById = R"~( +inline constexpr userver::utils::zstring_view kFindUserById = R"~( SELECT * FROM real_medium.users WHERE user_id = $1 )~"; -inline constexpr std::string_view kFindUserIDByUsername = R"~( +inline constexpr userver::utils::zstring_view kFindUserIDByUsername = R"~( SELECT user_id FROM real_medium.users WHERE username = $1 )~"; // Comments -inline constexpr std::string_view kFindCommentByIdAndSlug = R"~( +inline constexpr userver::utils::zstring_view kFindCommentByIdAndSlug = R"~( WITH article AS ( SELECT article_id FROM real_medium.articles WHERE slug = $2 ) @@ -45,7 +45,7 @@ JOIN article ON article.article_id = real_medium.comments.article_id WHERE comment_id = $1 )~"; -inline constexpr std::string_view kFindCommentsByArticleId = R"~( +inline constexpr userver::utils::zstring_view kFindCommentsByArticleId = R"~( WITH comments AS ( SELECT * FROM real_medium.comments WHERE article_id = $1 ) @@ -66,12 +66,12 @@ SELECT FROM comments )~"; -inline constexpr std::string_view kDeleteCommentById = R"~( +inline constexpr userver::utils::zstring_view kDeleteCommentById = R"~( DELETE FROM real_medium.comments WHERE comment_id = $1 AND user_id = $2 RETURNING * )~"; -inline constexpr std::string_view kAddComment = R"~( +inline constexpr userver::utils::zstring_view kAddComment = R"~( WITH comment AS ( INSERT INTO real_medium.comments(body, user_id, article_id) VALUES ($1, $2, $3) @@ -91,16 +91,16 @@ SELECT FROM comment )~"; -inline constexpr std::string_view kFindIdArticleBySlug = R"~( +inline constexpr userver::utils::zstring_view kFindIdArticleBySlug = R"~( SELECT article_id FROM real_medium.articles WHERE slug = $1 )~"; -inline constexpr std::string_view kIsProfileFollowing = R"~( +inline constexpr userver::utils::zstring_view kIsProfileFollowing = R"~( RETURN EXISTS (SELECT 1 FROM real_medium.followers WHERE follower_user_id = $1 AND followed_user_id = $2); )~"; // TODO: reuse common kIsProfileFollowing -inline constexpr std::string_view kGetProfileByUsername = R"~( +inline constexpr userver::utils::zstring_view kGetProfileByUsername = R"~( WITH profile AS ( SELECT * FROM real_medium.users WHERE username = $1 ) @@ -112,11 +112,11 @@ SELECT profile.username, profile.bio, profile.image, FROM profile )~"; -inline constexpr std::string_view kGetSaltByEmail = R"~( +inline constexpr userver::utils::zstring_view kGetSaltByEmail = R"~( SELECT salt FROM real_medium.users WHERE email = $1 )~"; -inline constexpr std::string_view KFollowingUser = R"~( +inline constexpr userver::utils::zstring_view KFollowingUser = R"~( WITH profile AS ( SELECT * FROM real_medium.users WHERE user_id = $1 ), following AS ( @@ -132,7 +132,7 @@ SELECT FROM profile )~"; -inline constexpr std::string_view KUnFollowingUser = R"~( +inline constexpr userver::utils::zstring_view KUnFollowingUser = R"~( WITH profile AS ( SELECT * FROM real_medium.users WHERE user_id = $1 ), following AS ( @@ -147,19 +147,19 @@ SELECT FROM profile )~"; -inline constexpr std::string_view kCreateArticle{R"~( +inline constexpr userver::utils::zstring_view kCreateArticle{R"~( SELECT real_medium.create_article($1, $2, $3, $4, $5, $6) )~"}; -inline constexpr std::string_view kGetArticleWithAuthorProfile{R"~( +inline constexpr userver::utils::zstring_view kGetArticleWithAuthorProfile{R"~( SELECT real_medium.get_article_with_author_profile($1, $2) )~"}; -inline constexpr std::string_view kGetArticleWithAuthorProfileBySlug{R"~( +inline constexpr userver::utils::zstring_view kGetArticleWithAuthorProfileBySlug{R"~( SELECT real_medium.get_article_with_author_profile_by_slug($1, $2) )~"}; -inline constexpr std::string_view kInsertFavoritePair = R"~( +inline constexpr userver::utils::zstring_view kInsertFavoritePair = R"~( WITH tmp(article_id, user_id) AS ( SELECT article_id, $1 FROM real_medium.articles WHERE slug=$2 ) @@ -168,13 +168,13 @@ ON CONFLICT DO NOTHING RETURNING article_id )~"; -inline constexpr std::string_view kIncrementFavoritesCount = R"~( +inline constexpr userver::utils::zstring_view kIncrementFavoritesCount = R"~( UPDATE real_medium.articles SET favorites_count=favorites_count + 1 WHERE article_id=$1 )~"; -inline constexpr std::string_view kDeleteFavoritePair = R"~( +inline constexpr userver::utils::zstring_view kDeleteFavoritePair = R"~( WITH tmp(article_id, user_id) AS ( SELECT article_id, $1 FROM real_medium.articles WHERE slug=$2 ) @@ -183,33 +183,33 @@ WHERE (article_id, user_id) IN (SELECT article_id, user_id FROM tmp) RETURNING article_id )~"; -inline constexpr std::string_view kDecrementFavoritesCount = R"~( +inline constexpr userver::utils::zstring_view kDecrementFavoritesCount = R"~( UPDATE real_medium.articles SET favorites_count=favorites_count - 1 WHERE article_id=$1 )~"; -inline constexpr std::string_view kFindArticlesByFollowedUsers = R"~( +inline constexpr userver::utils::zstring_view kFindArticlesByFollowedUsers = R"~( SELECT real_medium.get_feed_articles($1, $2, $3) )~"; -inline constexpr std::string_view kGetArticleIdBySlug{R"~( +inline constexpr userver::utils::zstring_view kGetArticleIdBySlug{R"~( SELECT real_medium.get_article_id_by_slug($1) )~"}; -inline constexpr std::string_view kUpdateArticleBySlug{R"~( +inline constexpr userver::utils::zstring_view kUpdateArticleBySlug{R"~( SELECT real_medium.update_article_by_slug($1, $2, $3, $4, $5, $6) )~"}; -inline constexpr std::string_view kDeleteArticleBySlug{R"~( +inline constexpr userver::utils::zstring_view kDeleteArticleBySlug{R"~( SELECT real_medium.delete_article_by_slug($1, $2) )~"}; -inline constexpr std::string_view kFindArticlesByFilters{R"~( +inline constexpr userver::utils::zstring_view kFindArticlesByFilters{R"~( SELECT real_medium.get_articles_by_filters($1, $2, $3, $4, $5, $6) )~"}; -inline constexpr std::string_view kSelectFullArticleInfo = {R"~( +inline constexpr userver::utils::zstring_view kSelectFullArticleInfo = {R"~( SELECT a.article_id AS articleId, a.title, a.slug, @@ -245,7 +245,7 @@ FROM real_medium.articles a JOIN real_medium.users u ON a.user_id = u.user_id )~"}; -inline constexpr std::string_view kSelectCachedComments = {R"~( +inline constexpr userver::utils::zstring_view kSelectCachedComments = {R"~( SELECT c.comment_id, c.created_at AS createdAt, c.updated_at AS updatedAt, diff --git a/src/handlers/articles/articles_favorite.cpp b/src/handlers/articles/articles_favorite.cpp index c580f9b..5ca8ccf 100644 --- a/src/handlers/articles/articles_favorite.cpp +++ b/src/handlers/articles/articles_favorite.cpp @@ -21,18 +21,18 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( userver::storages::postgres::Transaction::RW ); - auto res = transaction.Execute(sql::kInsertFavoritePair.data(), user_id, slug); + auto res = transaction.Execute(sql::kInsertFavoritePair.c_str(), user_id, slug); if (!res.IsEmpty()) { auto article_id = res.AsSingleRow(); - transaction.Execute(sql::kIncrementFavoritesCount.data(), article_id); + transaction.Execute(sql::kIncrementFavoritesCount.c_str(), article_id); transaction.Commit(); } const auto get_article_res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kSlave, - real_medium::sql::kGetArticleWithAuthorProfileBySlug.data(), + real_medium::sql::kGetArticleWithAuthorProfileBySlug.c_str(), slug, user_id ); diff --git a/src/handlers/articles/articles_get.cpp b/src/handlers/articles/articles_get.cpp index d4abdb9..3521893 100644 --- a/src/handlers/articles/articles_get.cpp +++ b/src/handlers/articles/articles_get.cpp @@ -26,11 +26,11 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( auto user_id = context.GetData>("id"); auto data = GetArticlesCache().Get(); - auto recentArticles = data->getRecent(filter); + auto recent_articles = data->getRecent(filter); userver::formats::json::ValueBuilder builder; builder["articles"] = userver::formats::common::Type::kArray; - for (auto& article : recentArticles) builder["articles"].PushBack(dto::Article::Parse(*article, user_id)); - builder["articlesCount"] = recentArticles.size(); + for (auto& article : recent_articles) builder["articles"].PushBack(dto::Article::Parse(*article, user_id)); + builder["articlesCount"] = recent_articles.size(); return builder.ExtractValue(); } diff --git a/src/handlers/articles/articles_post.cpp b/src/handlers/articles/articles_post.cpp index 21fb541..b3f3298 100644 --- a/src/handlers/articles/articles_post.cpp +++ b/src/handlers/articles/articles_post.cpp @@ -15,9 +15,9 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( const userver::formats::json::Value& request_json, userver::server::request::RequestContext& context ) const { - handlers::CreateArticleRequest createArticleRequest = request_json["article"].As(); + auto create_article_request = request_json["article"].As(); try { - validator::validate(createArticleRequest); + validator::validate(create_article_request); } catch (const real_medium::utils::error::ValidationException& ex) { // userver doesn't yet support 422 HTTP error code, so we handle the // exception by ourselves. In general the exceptions are processed by the @@ -26,24 +26,24 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( return ex.GetDetails(); } - const auto userId = context.GetData>("id"); + const auto user_id = context.GetData>("id"); - std::string articleId; + std::string article_id; try { - const auto slug = real_medium::utils::slug::Slugify(createArticleRequest.title.value()); + const auto slug = real_medium::utils::slug::Slugify(create_article_request.title.value()); const auto res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - real_medium::sql::kCreateArticle.data(), - createArticleRequest.title, + real_medium::sql::kCreateArticle.c_str(), + create_article_request.title, slug, - createArticleRequest.body, - createArticleRequest.description, - userId, - createArticleRequest.tags + create_article_request.body, + create_article_request.description, + user_id, + create_article_request.tags ); - articleId = res.AsSingleRow(); + article_id = res.AsSingleRow(); } catch (const userver::storages::postgres::UniqueViolation& ex) { const auto constraint = ex.GetServerMessage().GetConstraint(); if (constraint == "uniq_slug") { @@ -55,9 +55,9 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( const auto res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - real_medium::sql::kGetArticleWithAuthorProfile.data(), - articleId, - userId + real_medium::sql::kGetArticleWithAuthorProfile.c_str(), + article_id, + user_id ); userver::formats::json::ValueBuilder builder; diff --git a/src/handlers/articles/articles_slug_delete.cpp b/src/handlers/articles/articles_slug_delete.cpp index a9e01e2..5913bf8 100644 --- a/src/handlers/articles/articles_slug_delete.cpp +++ b/src/handlers/articles/articles_slug_delete.cpp @@ -12,9 +12,9 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( userver::server::request::RequestContext& context ) const { const auto& slug = request.GetPathArg("slug"); - const auto userId = context.GetData>("id"); + const auto user_id = context.GetData>("id"); auto res = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kMaster, real_medium::sql::kGetArticleIdBySlug.data(), slug + userver::storages::postgres::ClusterHostType::kMaster, real_medium::sql::kGetArticleIdBySlug.c_str(), slug ); if (res.IsEmpty()) { request.SetResponseStatus(userver::server::http::HttpStatus::kNotFound); @@ -22,9 +22,9 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - real_medium::sql::kDeleteArticleBySlug.data(), + real_medium::sql::kDeleteArticleBySlug.c_str(), slug, - userId + user_id ); if (res.IsEmpty()) { diff --git a/src/handlers/articles/articles_slug_put.cpp b/src/handlers/articles/articles_slug_put.cpp index 1d25d08..ff6ef88 100644 --- a/src/handlers/articles/articles_slug_put.cpp +++ b/src/handlers/articles/articles_slug_put.cpp @@ -15,36 +15,36 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( userver::server::request::RequestContext& context ) const { auto slug = request.GetPathArg("slug"); - handlers::UpdateArticleRequest updateRequest = request_json["article"].As(); + auto update_request = request_json["article"].As(); try { - validator::validate(updateRequest); + validator::validate(update_request); } catch (const real_medium::utils::error::ValidationException& ex) { request.SetResponseStatus(userver::server::http::HttpStatus::kUnprocessableEntity); return ex.GetDetails(); } - auto userId = context.GetData>("id"); + auto user_id = context.GetData>("id"); - std::string articleId; + std::string article_id; try { - const auto newSlug = - updateRequest.title - ? std::make_optional(real_medium::utils::slug::Slugify(*updateRequest.title)) + const auto new_slug = + update_request.title + ? std::make_optional(real_medium::utils::slug::Slugify(*update_request.title)) : std::nullopt; const auto res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - real_medium::sql::kUpdateArticleBySlug.data(), + real_medium::sql::kUpdateArticleBySlug.c_str(), slug, - userId, - updateRequest.title, - newSlug, - updateRequest.description, - updateRequest.body + user_id, + update_request.title, + new_slug, + update_request.description, + update_request.body ); if (res.IsEmpty()) { request.SetResponseStatus(userver::server::http::HttpStatus::kNotFound); return {}; } - articleId = res.AsSingleRow(); + article_id = res.AsSingleRow(); } catch (const userver::storages::postgres::UniqueViolation& ex) { const auto constraint = ex.GetServerMessage().GetConstraint(); if (constraint == "uniq_slug") { @@ -55,9 +55,9 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } const auto res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - real_medium::sql::kGetArticleWithAuthorProfile.data(), - articleId, - userId + real_medium::sql::kGetArticleWithAuthorProfile.c_str(), + article_id, + user_id ); userver::formats::json::ValueBuilder builder; diff --git a/src/handlers/articles/articles_unfavorite.cpp b/src/handlers/articles/articles_unfavorite.cpp index a67547d..6ee0c60 100644 --- a/src/handlers/articles/articles_unfavorite.cpp +++ b/src/handlers/articles/articles_unfavorite.cpp @@ -19,18 +19,18 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( userver::storages::postgres::Transaction::RW ); - auto res = transaction.Execute(sql::kDeleteFavoritePair.data(), user_id, slug); + auto res = transaction.Execute(sql::kDeleteFavoritePair.c_str(), user_id, slug); if (!res.IsEmpty()) { auto article_id = res.AsSingleRow(); - transaction.Execute(sql::kDecrementFavoritesCount.data(), article_id); + transaction.Execute(sql::kDecrementFavoritesCount.c_str(), article_id); transaction.Commit(); } const auto get_article_res = GetPg().Execute( userver::storages::postgres::ClusterHostType::kSlave, - real_medium::sql::kGetArticleWithAuthorProfileBySlug.data(), + real_medium::sql::kGetArticleWithAuthorProfileBySlug.c_str(), slug, user_id ); diff --git a/src/handlers/auth/auth_bearer.cpp b/src/handlers/auth/auth_bearer.cpp index 682a30e..9fc55f2 100644 --- a/src/handlers/auth/auth_bearer.cpp +++ b/src/handlers/auth/auth_bearer.cpp @@ -48,7 +48,7 @@ AuthCheckerBearer::AuthCheckResult AuthCheckerBearer::CheckAuth( } const auto bearer_sep_pos = auth_value.find(' '); - if (bearer_sep_pos == std::string::npos || std::string_view{auth_value.data(), bearer_sep_pos} != "Token") { + if (bearer_sep_pos == std::string::npos || std::string_view{auth_value.c_str(), bearer_sep_pos} != "Token") { return AuthCheckResult{ AuthCheckResult::Status::kTokenNotFound, {}, @@ -56,7 +56,7 @@ AuthCheckerBearer::AuthCheckResult AuthCheckerBearer::CheckAuth( userver::server::handlers::HandlerErrorCode::kUnauthorized }; } - std::string_view token{auth_value.data() + bearer_sep_pos + 1}; + std::string_view token{auth_value.c_str() + bearer_sep_pos + 1}; jwt::jwt_payload payload; try { payload = utils::jwt::DecodeJWT(token); @@ -71,7 +71,7 @@ AuthCheckerBearer::AuthCheckResult AuthCheckerBearer::CheckAuth( auto id = payload.get_claim_value("id"); const auto res = - pg_cluster_->Execute(userver::storages::postgres::ClusterHostType::kSlave, sql::kFindUserById.data(), id); + pg_cluster_->Execute(userver::storages::postgres::ClusterHostType::kSlave, sql::kFindUserById.c_str(), id); if (res.IsEmpty()) { return AuthCheckResult{ AuthCheckResult::Status::kTokenNotFound, diff --git a/src/handlers/comments/comment_delete.cpp b/src/handlers/comments/comment_delete.cpp index 4070851..e1b0ea2 100644 --- a/src/handlers/comments/comment_delete.cpp +++ b/src/handlers/comments/comment_delete.cpp @@ -17,7 +17,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( const auto& slug = request.GetPathArg("slug"); const auto result_find_comment = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kMaster, sql::kFindCommentByIdAndSlug.data(), comment_id, slug + userver::storages::postgres::ClusterHostType::kMaster, sql::kFindCommentByIdAndSlug.c_str(), comment_id, slug ); if (result_find_comment.IsEmpty()) { @@ -27,7 +27,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } const auto result_delete_comment = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kMaster, sql::kDeleteCommentById.data(), comment_id, user_id + userver::storages::postgres::ClusterHostType::kMaster, sql::kDeleteCommentById.c_str(), comment_id, user_id ); if (result_delete_comment.IsEmpty()) { diff --git a/src/handlers/comments/comment_post.cpp b/src/handlers/comments/comment_post.cpp index 771eaa4..445fe62 100644 --- a/src/handlers/comments/comment_post.cpp +++ b/src/handlers/comments/comment_post.cpp @@ -31,7 +31,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( const auto& slug = request.GetPathArg("slug"); const auto res_find_article = - GetPg().Execute(userver::storages::postgres::ClusterHostType::kMaster, sql::kFindIdArticleBySlug.data(), slug); + GetPg().Execute(userver::storages::postgres::ClusterHostType::kMaster, sql::kFindIdArticleBySlug.c_str(), slug); if (res_find_article.IsEmpty()) { auto& response = request.GetHttpResponse(); @@ -43,7 +43,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( const auto res_ins_new_comment = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - sql::kAddComment.data(), + sql::kAddComment.c_str(), comment_body, user_id, article_id diff --git a/src/handlers/profiles/profiles.cpp b/src/handlers/profiles/profiles.cpp index 9e8c774..c087a7e 100644 --- a/src/handlers/profiles/profiles.cpp +++ b/src/handlers/profiles/profiles.cpp @@ -21,7 +21,7 @@ json::Value Handler::HandleRequestJsonThrow(const HttpRequest& request, const js const { auto user_id = context.GetData>("id"); const auto& username = request.GetPathArg("username"); - auto res = GetPg().Execute(ClusterHostType::kMaster, sql::kGetProfileByUsername.data(), username, user_id); + auto res = GetPg().Execute(ClusterHostType::kMaster, sql::kGetProfileByUsername.c_str(), username, user_id); if (res.IsEmpty()) { auto& response = request.GetHttpResponse(); response.SetStatus(userver::server::http::HttpStatus::kNotFound); diff --git a/src/handlers/profiles/profiles_follow.cpp b/src/handlers/profiles/profiles_follow.cpp index c18d89b..507b181 100644 --- a/src/handlers/profiles/profiles_follow.cpp +++ b/src/handlers/profiles/profiles_follow.cpp @@ -31,7 +31,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } const auto res_find_id_username = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kSlave, sql::kFindUserIDByUsername.data(), username + userver::storages::postgres::ClusterHostType::kSlave, sql::kFindUserIDByUsername.c_str(), username ); if (res_find_id_username.IsEmpty()) { auto& response = request.GetHttpResponse(); @@ -48,7 +48,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } const auto res_following = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kSlave, sql::KFollowingUser.data(), username_id, user_id + userver::storages::postgres::ClusterHostType::kSlave, sql::KFollowingUser.c_str(), username_id, user_id ); const auto profile = res_following.AsSingleRow(userver::storages::postgres::kRowTag); diff --git a/src/handlers/profiles/profiles_follow_delete.cpp b/src/handlers/profiles/profiles_follow_delete.cpp index 2f87557..f1ef8ac 100644 --- a/src/handlers/profiles/profiles_follow_delete.cpp +++ b/src/handlers/profiles/profiles_follow_delete.cpp @@ -30,7 +30,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } const auto res_find_id_username = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kSlave, sql::kFindUserIDByUsername.data(), username + userver::storages::postgres::ClusterHostType::kSlave, sql::kFindUserIDByUsername.c_str(), username ); if (res_find_id_username.IsEmpty()) { auto& response = request.GetHttpResponse(); @@ -46,7 +46,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( } const auto res_unfollowing = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kSlave, sql::KUnFollowingUser.data(), username_id, user_id + userver::storages::postgres::ClusterHostType::kSlave, sql::KUnFollowingUser.c_str(), username_id, user_id ); const auto profile = res_unfollowing.AsSingleRow(userver::storages::postgres::kRowTag); diff --git a/src/handlers/tags/tags.cpp b/src/handlers/tags/tags.cpp index 8ee377e..4e48a05 100644 --- a/src/handlers/tags/tags.cpp +++ b/src/handlers/tags/tags.cpp @@ -11,8 +11,8 @@ namespace real_medium::handlers::tags::get { userver::formats::json::Value Handler:: HandleRequestJsonThrow(const userver::server::http::HttpRequest&, const userver::formats::json::Value&, userver::server::request::RequestContext&) const { - constexpr static auto query = "SELECT tag_name FROM real_medium.tag_list"; - auto result = GetPg().Execute(userver::storages::postgres::ClusterHostType::kSlave, query); + constexpr static auto kQuery = "SELECT tag_name FROM real_medium.tag_list"; + auto result = GetPg().Execute(userver::storages::postgres::ClusterHostType::kSlave, kQuery); auto tags = result.AsSetOf(); userver::formats::json::ValueBuilder response; diff --git a/src/handlers/users/user_get.cpp b/src/handlers/users/user_get.cpp index e327b80..a2202b1 100644 --- a/src/handlers/users/user_get.cpp +++ b/src/handlers/users/user_get.cpp @@ -13,7 +13,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( auto user_id = context.GetData>("id"); const auto result = - GetPg().Execute(userver::storages::postgres::ClusterHostType::kMaster, sql::kFindUserById.data(), user_id); + GetPg().Execute(userver::storages::postgres::ClusterHostType::kMaster, sql::kFindUserById.c_str(), user_id); if (result.IsEmpty()) { auto& response = request.GetHttpResponse(); diff --git a/src/handlers/users/user_put.cpp b/src/handlers/users/user_put.cpp index bdd6d5b..85dc88a 100644 --- a/src/handlers/users/user_put.cpp +++ b/src/handlers/users/user_put.cpp @@ -34,7 +34,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow( const auto result = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - sql::kUpdateUser.data(), + sql::kUpdateUser.c_str(), user_id, user_change_data.username, user_change_data.email, diff --git a/src/handlers/users/users.cpp b/src/handlers/users/users.cpp index 76bd03b..967e95f 100644 --- a/src/handlers/users/users.cpp +++ b/src/handlers/users/users.cpp @@ -33,7 +33,7 @@ userver::formats::json::Value RegisterUser:: try { auto query_result = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - sql::kInsertUser.data(), + sql::kInsertUser.c_str(), user_register.username, user_register.email, user_register.bio, diff --git a/src/handlers/users/users_login.cpp b/src/handlers/users/users_login.cpp index fbfbb2c..e5fe50f 100644 --- a/src/handlers/users/users_login.cpp +++ b/src/handlers/users/users_login.cpp @@ -26,7 +26,6 @@ class LoginUser final : public Common { HandleRequestJsonThrow(const userver::server::http::HttpRequest& request, const userver::formats::json::Value& request_json, userver::server::request::RequestContext&) const override { auto&& user_login = request_json["user"].As(); - ; try { validator::validate(user_login); @@ -36,7 +35,7 @@ class LoginUser final : public Common { } auto salt = GetPg().Execute( - userver::storages::postgres::ClusterHostType::kMaster, sql::kGetSaltByEmail.data(), user_login.email + userver::storages::postgres::ClusterHostType::kMaster, sql::kGetSaltByEmail.c_str(), user_login.email ); if (salt.IsEmpty()) { auto& response = request.GetHttpResponse(); @@ -46,20 +45,20 @@ class LoginUser final : public Common { auto password_hash = userver::crypto::hash::Sha256(user_login.password.value() + salt.AsSingleRow()); - auto userResult = GetPg().Execute( + auto user_result = GetPg().Execute( userver::storages::postgres::ClusterHostType::kMaster, - sql::kSelectUserByEmailAndPassword.data(), + sql::kSelectUserByEmailAndPassword.c_str(), user_login.email, password_hash ); - if (userResult.IsEmpty()) { + if (user_result.IsEmpty()) { auto& response = request.GetHttpResponse(); response.SetStatus(userver::server::http::HttpStatus::kNotFound); return {}; } - auto user = userResult.AsSingleRow(userver::storages::postgres::kRowTag); + auto user = user_result.AsSingleRow(userver::storages::postgres::kRowTag); userver::formats::json::ValueBuilder response; response["user"] = user; diff --git a/src/utils/jwt.cpp b/src/utils/jwt.cpp index 502d86c..dbdb977 100644 --- a/src/utils/jwt.cpp +++ b/src/utils/jwt.cpp @@ -5,7 +5,7 @@ namespace real_medium::utils::jwt { using namespace ::jwt::params; std::string GenerateJWT(std::string_view id) { - ::jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"id", id.data()}})}; + ::jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"id", id}})}; return obj.signature(); } diff --git a/src/utils/make_error.cpp b/src/utils/make_error.cpp index 5ad3c6d..b991fe6 100644 --- a/src/utils/make_error.cpp +++ b/src/utils/make_error.cpp @@ -4,7 +4,7 @@ namespace real_medium::utils::error { userver::formats::json::Value MakeError(std::string_view field_name, std::string_view message) { userver::formats::json::ValueBuilder error_builder; - error_builder["errors"][field_name.data()].PushBack(message); + error_builder["errors"][std::string{field_name}].PushBack(message); return error_builder.ExtractValue(); } } // namespace real_medium::utils::error