Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
sabudilovskiy committed Oct 20, 2023
1 parent afbc373 commit 43ff626
Show file tree
Hide file tree
Showing 61 changed files with 1,728 additions and 309 deletions.
9 changes: 1 addition & 8 deletions .gen/objs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ src/openapi/doc/request.hpp
src/openapi/doc/reflective.hpp
src/openapi/doc/property.hpp
src/openapi/doc/path.hpp
src/openapi/doc/optional.hpp
src/openapi/doc/int.hpp
src/openapi/doc/header.hpp
src/openapi/doc/cookie.hpp
Expand Down Expand Up @@ -166,14 +167,6 @@ src/legacy/views/teacher/create/view.hpp
src/legacy/views/teacher/create/view.cpp
src/legacy/views/teacher/create/Responses.hpp
src/legacy/views/teacher/create/Request.hpp
src/legacy/views/register/view.hpp
src/legacy/views/register/view.cpp
src/legacy/views/register/Responses.hpp
src/legacy/views/register/Request.hpp
src/legacy/views/login/view.hpp
src/legacy/views/login/view.cpp
src/legacy/views/login/Responses.hpp
src/legacy/views/login/Request.hpp
src/legacy/views/hello/view.hpp
src/legacy/views/hello/view.cpp
src/legacy/views/group-stage/list/view.hpp
Expand Down
10 changes: 0 additions & 10 deletions configs/static_config.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ components_manager:
path: /tests/{action}
method: POST
task_processor: main-task-processor
handler-login:
path: /login
method: POST
load-enabled: false
task_processor: main-task-processor
handler-register:
path: /register
method: POST
load-enabled: false
task_processor: main-task-processor
handler-timetable-get:
path: /timetable/get
method: POST
Expand Down
86 changes: 86 additions & 0 deletions scripts/generate_sql_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import sys

def read_sql(result: dict, sql_file, directory_path):
# Получите относительный путь относительно directory_path
relative_path = os.path.relpath(sql_file, directory_path)
file_name = os.path.basename(sql_file)
file_name_without_extension, _ = os.path.splitext(file_name)
if file_name_without_extension in result.keys():
raise ValueError(f"Дублируется имя файла. Первый путь:{result['path']}, второй путь:{relative_path}")
with open(sql_file, 'r') as file:
content = file.read()
result[file_name_without_extension] = {
'path': directory_path + '/' + relative_path,
'content': content
}
def read_sqls(directory_path):
result = {}
for root, dir, files in os.walk(directory_path):
for file in files:
if file.endswith(".sql"):
read_sql(result, os.path.join(root, file), directory_path)
return result


def place_begin_namespace(content, namespace):
return content + '\nnamespace ' + namespace + '{\n\n'

def place_end_namespace(content, namespace):
return content + '}\n'

def place_extern_sql_query(result_hpp: str, name: str, path: str):
add = '//Generated from: ' + path + '\nextern const userver::storages::postgres::Query ' + name + ';\n\n'
return result_hpp + add

def place_impl_sql_query(result_cpp: str, name: str, path: str, content: str):
add = '//Generated from: ' + path + '\nconst userver::storages::postgres::Query ' + name + ' = {\n'
add = add + 'R"-(\n'
add = add + content
add = add + ')-",\n'
add = add + '\tuserver::storages::postgres::Query::Name("' + name + '"),\n'
add = add + '\tuserver::storages::postgres::Query::LogMode::kFull\n'
add = add + '};\n\n'
return result_cpp + add

def prepare_result_contents(sqls: dict, namespace, name_result):
result_hpp = '''#pragma once
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */
#include <userver/storages/postgres/query.hpp>
'''
result_cpp = '''
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */
#include "''' + name_result + '.hpp"\n'

result_hpp = place_begin_namespace(result_hpp, namespace)
result_cpp = place_begin_namespace(result_cpp, namespace)
for name, item in sqls.items():
result_hpp = place_extern_sql_query(result_hpp, name, item['path'])
result_cpp = place_impl_sql_query(result_cpp, name, item['path'], item['content'])
result_hpp = place_end_namespace(result_hpp, namespace)
result_cpp = place_end_namespace(result_cpp, namespace)
return result_hpp, result_cpp

def main():
if len(sys.argv) < 5:
print('Usage: python3 generate_sql_queries.py <sql_directory> <path/to/result> <name_result> <namespace>')
return

sql_directory = sys.argv[1]
path_to_result = sys.argv[2]
name_result = sys.argv[3]
namespace = sys.argv[4]
sqls = read_sqls(sql_directory)
result_hpp, result_cpp = prepare_result_contents(sqls, namespace, name_result)
hpp_path = path_to_result + '/' + name_result + '.hpp'
cpp_path = path_to_result + '/' + name_result + '.cpp'
with open(hpp_path, 'w') as hpp:
hpp.write(result_hpp)
with open(cpp_path, 'w') as hpp:
hpp.write(result_cpp)

if __name__ == '__main__':
main()
4 changes: 0 additions & 4 deletions service/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "legacy/views/faculty/list/view.hpp"
#include "legacy/views/group-stage/list/view.hpp"
#include "legacy/views/hello/view.hpp"
#include "legacy/views/login/view.hpp"
#include "legacy/views/register/view.hpp"
#include "legacy/views/teacher/create/view.hpp"
#include "legacy/views/teacher/list/view.hpp"
#include "legacy/views/teacher/request/approve/link/view.hpp"
Expand All @@ -55,8 +53,6 @@ void AppendPgControllers(userver::components::ComponentList& component_list)
void AppendLegacyViews(userver::components::ComponentList& component_list)
{
using namespace legacy::views;
login::Append(component_list);
register_::Append(component_list);
timetable::get::Append(component_list);
admin::create::Append(component_list);
admin::list::Append(component_list);
Expand Down
150 changes: 150 additions & 0 deletions src/codegen/sql.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@

/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */

#include "sql.hpp"

namespace sql{

//Generated from: src/sql/token/get_user_id_by_token.sql
const userver::storages::postgres::Query get_user_id_by_token = {
R"-(
WITH found_token
AS
(
select id_user
from timetable_vsu.token
WHERE id = $1 AND expire_time > $2
)
SELECT id, login, password, user_type
from timetable_vsu."user"
LEFT OUTER JOIN found_token ON id_user = "user".id
)-",
userver::storages::postgres::Query::Name("get_user_id_by_token"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/token/add_token_to_user.sql
const userver::storages::postgres::Query add_token_to_user = {
R"-(
insert into timetable_vsu."token" (id_user, expire_time) values ($1, $2) RETURNING id
)-",
userver::storages::postgres::Query::Name("add_token_to_user"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/drop_user_by_login.sql
const userver::storages::postgres::Query drop_user_by_login = {
R"-(
DELETE FROM timetable_vsu."user" WHERE login=$1;
)-",
userver::storages::postgres::Query::Name("drop_user_by_login"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/create_teacher_request.sql
const userver::storages::postgres::Query create_teacher_request = {
R"-(
INSERT INTO timetable_vsu."teacher_requests"(id_user, description) values ($1, $2) ON CONFLICT DO NOTHING
)-",
userver::storages::postgres::Query::Name("create_teacher_request"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/get_user_by_token.sql
const userver::storages::postgres::Query get_user_by_token = {
R"-(
WITH all_user AS
(
SELECT
u.id AS user_id,
a.id AS admin_id,
t.id AS token_id,
tl.id_teacher AS teacher_id
from timetable_vsu.token AS t
left join timetable_vsu.user AS u on u.id = t.id_user
left join timetable_vsu.admin AS a on u.id = a.id_user
left join timetable_vsu.teacher_link AS tl on u.id = tl.id_user
where t.id = $1
)
SELECT
user_id,
CASE
WHEN admin_id IS NOT NULL THEN 'admin'::timetable_vsu.user_type
WHEN teacher_id IS NOT NULL THEN 'teacher'::timetable_vsu.user_type
ELSE 'user'
END AS type
FROM all_user;
)-",
userver::storages::postgres::Query::Name("get_user_by_token"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/internal_add_user.sql
const userver::storages::postgres::Query internal_add_user = {
R"-(
insert into timetable_vsu."user"(id, login, password) values ($1, $2.login, $2.password)
)-",
userver::storages::postgres::Query::Name("internal_add_user"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/create_admin_request.sql
const userver::storages::postgres::Query create_admin_request = {
R"-(
INSERT INTO timetable_vsu."admin_requests"(id_user, description) values ($1, $2) ON CONFLICT DO NOTHING
)-",
userver::storages::postgres::Query::Name("create_admin_request"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/drop_user_by_id.sql
const userver::storages::postgres::Query drop_user_by_id = {
R"-(
DELETE FROM timetable_vsu."user" WHERE id=$1;
)-",
userver::storages::postgres::Query::Name("drop_user_by_id"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/add_user.sql
const userver::storages::postgres::Query add_user = {
R"-(
insert into timetable_vsu."user"
( login, password)
values ($1.login, $1.password)
ON CONFLICT DO NOTHING
RETURNING id
)-",
userver::storages::postgres::Query::Name("add_user"),
userver::storages::postgres::Query::LogMode::kFull
};

//Generated from: src/sql/user/get_user_by_credentials.sql
const userver::storages::postgres::Query get_user_by_credentials = {
R"-(
WITH all_user AS
(
SELECT
u.id AS user_id,
a.id AS admin_id,
tl.id_teacher AS teacher_id
from
timetable_vsu.user AS u
left join timetable_vsu.admin AS a on u.id = a.id_user
left join timetable_vsu.teacher_link AS tl on u.id = tl.id_user
where u.login = $1.login and u."password" = $1.password
)
SELECT
user_id,
CASE
WHEN admin_id IS NOT NULL THEN 'admin'::timetable_vsu.user_type
WHEN teacher_id IS NOT NULL THEN 'teacher'::timetable_vsu.user_type
ELSE 'user'
END AS type
FROM all_user;
)-",
userver::storages::postgres::Query::Name("get_user_by_credentials"),
userver::storages::postgres::Query::LogMode::kFull
};

}
39 changes: 39 additions & 0 deletions src/codegen/sql.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */

#include <userver/storages/postgres/query.hpp>

namespace sql{

//Generated from: src/sql/token/get_user_id_by_token.sql
extern const userver::storages::postgres::Query get_user_id_by_token;

//Generated from: src/sql/token/add_token_to_user.sql
extern const userver::storages::postgres::Query add_token_to_user;

//Generated from: src/sql/user/drop_user_by_login.sql
extern const userver::storages::postgres::Query drop_user_by_login;

//Generated from: src/sql/user/create_teacher_request.sql
extern const userver::storages::postgres::Query create_teacher_request;

//Generated from: src/sql/user/get_user_by_token.sql
extern const userver::storages::postgres::Query get_user_by_token;

//Generated from: src/sql/user/internal_add_user.sql
extern const userver::storages::postgres::Query internal_add_user;

//Generated from: src/sql/user/create_admin_request.sql
extern const userver::storages::postgres::Query create_admin_request;

//Generated from: src/sql/user/drop_user_by_id.sql
extern const userver::storages::postgres::Query drop_user_by_id;

//Generated from: src/sql/user/add_user.sql
extern const userver::storages::postgres::Query add_user;

//Generated from: src/sql/user/get_user_by_credentials.sql
extern const userver::storages::postgres::Query get_user_by_credentials;

}
40 changes: 40 additions & 0 deletions src/controllers/lesson/controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "controller.hpp"

#include <boost/uuid/uuid.hpp>
#include <chrono>
#include <codegen/sql.hpp>
#include <openapi/postgres/mapping.hpp>
#include <userver/components/component_config.hpp>
#include <userver/components/component_context.hpp>
#include <userver/components/component_list.hpp>
#include <userver/components/loggable_component_base.hpp>
#include <userver/storages/postgres/component.hpp>
#include <userver/storages/postgres/io/uuid.hpp>
#include <userver/utils/datetime.hpp>
#include <utils/shared_transaction.hpp>

#include "controller_fwd.hpp"
#include "models/user_credentials/postgres.hpp"

namespace controllers::lesson
{
void Append(userver::components::ComponentList& list)
{
list.Append<Controller>();
}
Controller::Controller(const userver::components::ComponentConfig& cfg,
const userver::components::ComponentContext& ctx)
: Base(cfg, ctx)
{
}
boost::uuids::uuid Controller::CreateNew(
const boost::uuids::uuid& id_user,
utils::SharedTransaction transaction) const
{
utils::FillSharedTransaction(transaction, pg_cluster_);
auto pg_result = transaction->transaction_.Execute(
sql::add_token_to_user, id_user,
userver::utils::datetime::Now() + std::chrono::hours(24));
return pg_result.AsSingleRow<boost::uuids::uuid>();
}
} // namespace controllers::token
20 changes: 20 additions & 0 deletions src/controllers/lesson/controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <boost/uuid/uuid.hpp>
#include <userver/components/loggable_component_base.hpp>
#include <utils/component_list_fwd.hpp>
#include <utils/pg_controller.hpp>
#include <utils/shared_transaction.hpp>

namespace controllers::lesson
{
using Base = utils::PgController<"new-lesson-controller", "postgres-db-1">;
struct Controller : Base
{
Controller(const userver::components::ComponentConfig& cfg,
const userver::components::ComponentContext& ctx);
boost::uuids::uuid CreateNew(
const boost::uuids::uuid& id_user,
utils::SharedTransaction transaction = nullptr) const;
};
} // namespace controllers::token
Loading

0 comments on commit 43ff626

Please sign in to comment.