Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
sabudilovskiy committed Oct 27, 2023
1 parent 6dc7b31 commit d187c5e
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ gen:
$(MAKE) gen-sources
$(MAKE) gen-all-headers

.PHONY: build-log
build-log:
.PHONY: log-build
log-build:
$(MAKE) -s test-debug > log_build.txt 2>&1

# # Internal hidden targets that are used only in docker environment
Expand Down
1 change: 1 addition & 0 deletions src/controllers/lesson/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "legacy/models/lesson_week_type/postgre.hpp"
#include "legacy/models/subgroup/postgre.hpp"
#include "models/user_credentials/postgres.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace controllers::lesson
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "legacy/models/admin_account/type.hpp"
#include "legacy/models/admin_filter/postgre.hpp"
#include "legacy/models/user_credentials/postgre.hpp"
#include "legacy/models/substring/postgre.hpp"
#include "sql_queries.hpp"

namespace legacy::components::controllers::postgres::admin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "legacy/models/substring/postgre.hpp"
#include "legacy/models/user_credentials/postgre.hpp"
#include "sql_queries.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace legacy::components::controllers::postgres::faculty
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "legacy/models/substring/postgre.hpp"
#include "legacy/models/user_credentials/postgre.hpp"
#include "sql_queries.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace legacy::components::controllers::postgres::group_stage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "legacy/models/lesson_v1/postgre.hpp"
#include "legacy/models/lesson_v1/type.hpp"
#include "sql_queries.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace legacy::components::controllers::postgres::lesson
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "legacy/models/teacher_info/postgre.hpp"
#include "legacy/models/user_credentials/postgre.hpp"
#include "sql_queries.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace legacy::components::controllers::postgres::teacher
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "legacy/models/user/type.hpp"
#include "legacy/models/user_type/postgre.hpp"
#include "sql_queries.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace legacy::components::controllers::postgres::token
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "legacy/models/user/postgre.hpp"
#include "legacy/models/user/type.hpp"
#include "legacy/models/user_credentials/postgre.hpp"
#include "legacy/models/substring/postgre.hpp"

namespace legacy::components::controllers::postgres::user
{
Expand Down
22 changes: 8 additions & 14 deletions src/legacy/models/substring/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@

namespace legacy::models
{
SubString Parse(const std::string& str, userver::formats::parse::To<SubString>)
{
SubString result;
result.GetUnderlying().reserve(str.size() + 2);
result.GetUnderlying().append("%").append(str).append("%");
return result;
}
// SubString Parse(const std::string& str, userver::formats::parse::To<SubString>)
// {
// SubString result;
// result.GetUnderlying().reserve(str.size() + 2);
// result.GetUnderlying().append("%").append(str).append("%");
// return result;
// }
SubString Parse(const userver::formats::json::Value& value,
userver::formats::parse::To<SubString>)
{
if (!value.IsString())
{
throw std::runtime_error(fmt::format(
"Expected string type, but got: {}", utils::GetType(value)));
}
auto raw_value = value.As<std::string>();
return Parse(raw_value, userver::formats::parse::To<SubString>{});
return SubString{value.As<std::string>()};
}
} // namespace legacy::models
2 changes: 1 addition & 1 deletion src/legacy/models/substring/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace legacy::models
{
SubString Parse(const std::string& str, userver::formats::parse::To<SubString>);
// SubString Parse(const std::string& str, userver::formats::parse::To<SubString>);
SubString Parse(const userver::formats::json::Value& value,
userver::formats::parse::To<SubString>);
} // namespace legacy::models
27 changes: 26 additions & 1 deletion src/legacy/models/substring/postgre.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <userver/storages/postgres/io/composite_types.hpp>
#include <userver/storages/postgres/io/string_types.hpp>
#include <userver/utils/trivial_map.hpp>

#include "type.hpp"
Expand All @@ -11,4 +11,29 @@ template <>
struct CppToSystemPg<SubString> : PredefinedOid<PredefinedOids::kText>
{
};

template <>
struct BufferFormatter<SubString> {
using CharFormatter = BufferFormatter<const char*>;
const SubString& value;

explicit BufferFormatter(const SubString& val) : value{val} {}
template <typename Buffer>
void operator()(const UserTypes&, Buffer& buf) const {
CharFormatter::WriteN(buf, "%", 1);
CharFormatter::WriteN(buf, value.data(), value.size());
CharFormatter::WriteN(buf, "%", 1);
}
};

template <>
struct BufferParser<SubString> {
SubString& value;

explicit BufferParser(SubString& val) : value{val} {}

void operator()(const FieldBuffer& buffer){
value.GetUnderlying() = buffer.ToString();
}
};
} // namespace userver::storages::postgres::io
14 changes: 6 additions & 8 deletions src/legacy/models/substring/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

namespace legacy::models
{
std::string Serialize(const SubString& value,
userver::formats::serialize::To<std::string>)
{
return value.GetUnderlying().substr(1, value.GetUnderlying().size() - 2);
}
// std::string Serialize(const SubString& value,
// userver::formats::serialize::To<std::string>)
// {
// return value.GetUnderlying().substr(1, value.GetUnderlying().size() - 2);
// }
userver::formats::json::Value Serialize(
const SubString& value,
userver::formats::serialize::To<userver::formats::json::Value>)
{
std::string str =
Serialize(value, userver::formats::serialize::To<std::string>{});
return userver::formats::json::ValueBuilder(str).ExtractValue();
return userver::formats::json::ValueBuilder{value.GetUnderlying()}.ExtractValue();
}
} // namespace legacy::models
4 changes: 2 additions & 2 deletions src/legacy/models/substring/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace legacy::models
userver::formats::json::Value Serialize(
const SubString& value,
userver::formats::serialize::To<userver::formats::json::Value>);
std::string Serialize(const SubString& value,
userver::formats::serialize::To<std::string>);
// std::string Serialize(const SubString& value,
// userver::formats::serialize::To<std::string>);
} // namespace legacy::models
40 changes: 37 additions & 3 deletions src/legacy/models/substring/type.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
#pragma once
#include <chrono>
#include <cstddef>
#include <string>
#include <type_traits>
#include <userver/utils/strong_typedef.hpp>
namespace legacy::models
{
//расставляет % с обоих сторон строки, чтобы запросы в постгресе с
//использованием ILIKE работали как поиск подстроки
using SubString =
userver::utils::StrongTypedef<struct SubStringTag, std::string>;
class SubString{
public:
using UnderlyingType = std::string;
SubString() = default;
SubString(const SubString&) = default;
SubString(SubString&&) noexcept = default;
SubString& operator=(const SubString&) = default;
SubString& operator=(SubString&&) noexcept = default;
template <typename... Args>
SubString(Args&&... args) noexcept(std::is_nothrow_constructible_v<std::string, Args&&...>) : data_(std::forward<Args>(args)...){}

template <typename Arg>
SubString& operator=(Arg&& args) noexcept(std::is_nothrow_assignable_v<std::string, Arg>){
data_ = std::forward<Arg>(args);
}

std::string& GetUnderlying(){
return data_;
}
const std::string& GetUnderlying() const{
return data_;
}

std::size_t size() const{
return data_.size();
}
auto data() const {
return data_.data();
}
auto operator<=>(const SubString&) const= default;

private:
std::string data_{};
};
} // namespace legacy::models
1 change: 1 addition & 0 deletions src/legacy/views/admin/list/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "legacy/components/controllers/postgres/admin/controller.hpp"
#include "legacy/components/controllers/postgres/user/controller.hpp"
#include "legacy/models/auth_token/serialize.hpp"
#include "legacy/models/substring/parse.hpp"
#include "legacy/models/user/serialize.hpp"
namespace legacy::views::admin::list
{
Expand Down
1 change: 1 addition & 0 deletions src/legacy/views/faculty/list/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "http/handler_parsed.hpp"
#include "legacy/components/controllers/postgres/faculty/controller.hpp"
#include "legacy/models/auth_token/serialize.hpp"
#include "legacy/models/substring/parse.hpp"
#include "legacy/models/user/serialize.hpp"
namespace legacy::views::faculty::list
{
Expand Down
1 change: 1 addition & 0 deletions src/legacy/views/group-stage/list/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "legacy/models/auth_token/serialize.hpp"
#include "legacy/models/education_type/all.hpp"
#include "legacy/models/user/serialize.hpp"
#include "legacy/models/substring/parse.hpp"
namespace legacy::views::group::stage::list
{
namespace
Expand Down
1 change: 1 addition & 0 deletions src/legacy/views/teacher/list/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "legacy/components/controllers/postgres/teacher/controller.hpp"
#include "legacy/models/auth_token/serialize.hpp"
#include "legacy/models/user/serialize.hpp"
#include "legacy/models/substring/parse.hpp"
namespace legacy::views::teacher::list
{
namespace
Expand Down

0 comments on commit d187c5e

Please sign in to comment.