<commit_before>#include "ItemPU.hpp"
#include "item.pb.h"
#include <sqlpp11/sqlpp11.h>
#include <sqlpp11/postgresql/insert.h>
#include "sql_schema/items.h"
#include "sql_schema/parameters.h"


using namespace pb;

namespace eedb{
namespace pu{

constexpr schema::items i;

ItemPU::ItemPU(){

}

void ItemPU::process(ClientRequest &msgReq)
{
    DB db;
    process(db, msgReq);
}

void ItemPU::process(DB &db, ClientRequest &msgReq)
{
    // Check if this is the message that handler wants
    Q_ASSERT( msgReq.data_case() == pb::ClientRequest::kItemReqFieldNumber );
    Q_ASSERT( msgReq.has_itemreq() );

    auto req = msgReq.itemreq();

    if(user()->isOffline())
        sendServerError(pb::Error_UserOffilne);

    else{
        auto msgType = req.action_case();
        switch ( msgType ) {
        case ItemRequest::kAdd:
            handle_add(db, *req.mutable_add() );
            break;
        case ItemRequest::kGet:
//            handle_login(db, req.login() );
            break;
        case ItemRequest::kModify:
//            handle_logout(db, req.logout() );
            break;
        case ItemRequest::kRemove:
//            handle_get(db, req.get() );
            break;
        case UserReq::ACTION_NOT_SET:
            sendServerError(pb::Error_NoActionChoosen);
            break;
        }
    }
}

void ItemPU::handle_add(DB &db, ItemRequest_Add &msg)
{
    constexpr schema::items it;

    auth::AccesControl stat(user()->id());

    bool allow = false;
    if(msg.has_is_private() && msg.is_private())
        allow = stat.checkUserAction<schema::items>(db, "add_private_item");
    else
        allow = stat.checkUserAction<schema::items>(db, "add_public_item");

    if(allow){
        run_saveItemInDb(db, msg);
    } else {
        sendServerError(pb::Error_AccesDeny);
    }
}

#include <QJsonObject>

void ItemPU::run_saveItemInDb(DB &db, ItemRequest_Add &msg)
{
    //check parameters

    QJsonObject params;

    if(msg.parameters_size() > 0){
        static constexpr schema::parameters p;
        std::vector<quint64> parametersIds;
        for(const auto &parameter:msg.parameters())
            parametersIds.push_back( parameter.id() );

        // check is all parameters are avalible in database
        bool allAvalible = db( select(count(p.uid))
                               .from(p)
                               .where(p.uid.in(sqlpp::value_list(parametersIds))))
                .front().count == parametersIds.size();

        if(!allAvalible){
            add_response()->mutable_itemres()->set_code(pb::ItemResponse_Replay_ParameterDontExists);
            return;
        }

        for(const auto &parameter:msg.parameters()){
            params.insert("id", parameter.id() );
        }
    }

    auto res = add_response()->mutable_itemres();
    auto prep = db.prepare(sqlpp::postgresql::insert_into(i).set(
                               i.name = parameter(i.name),
                               i.category_id = msg.category_id(),
                               i.symbol = parameter(i.symbol),
                               i.owner = user()->id(),
                               i.description = parameter(i.description),
                               i.params = parameter(i.params)
            ).returning(i.uid) );

    prep.params.name = msg.name();
    prep.params.symbol = msg.symbol();

    if(msg.has_description() && !msg.description().empty())
        prep.params.description = msg.description();

    auto return_value = db(prep);

    if(msg.has_returning_id() && msg.returning_id())
        res->set_id(return_value.front().uid);

    res->set_code(ItemResponse_Replay_OK);
}

}
}
<commit_msg>fix build<commit_after>#include "ItemPU.hpp"
#include "item.pb.h"
#include <sqlpp11/sqlpp11.h>
#include <sqlpp11/postgresql/insert.h>
#include "sql_schema/items.h"
#include "sql_schema/parameters.h"


using namespace pb;

namespace eedb{
namespace pu{

constexpr schema::items i;

ItemPU::ItemPU(){

}

void ItemPU::process(ClientRequest &msgReq)
{
    DB db;
    process(db, msgReq);
}

void ItemPU::process(DB &db, ClientRequest &msgReq)
{
    // Check if this is the message that handler wants
    Q_ASSERT( msgReq.data_case() == pb::ClientRequest::kItemReqFieldNumber );
    Q_ASSERT( msgReq.has_itemreq() );

    auto req = msgReq.itemreq();

    if(user()->isOffline())
        sendServerError(pb::Error_UserOffilne);

    else{
        auto msgType = req.action_case();
        switch ( msgType ) {
        case ItemRequest::kAdd:
            handle_add(db, *req.mutable_add() );
            break;
        case ItemRequest::kGet:
//            handle_login(db, req.login() );
            break;
        case ItemRequest::kModify:
//            handle_logout(db, req.logout() );
            break;
        case ItemRequest::kRemove:
//            handle_get(db, req.get() );
            break;
        case UserReq::ACTION_NOT_SET:
            sendServerError(pb::Error_NoActionChoosen);
            break;
        }
    }
}

void ItemPU::handle_add(DB &db, ItemRequest_Add &msg)
{
    constexpr schema::items it;

    auth::AccesControl stat(user()->id());

    bool allow = false;
    if(msg.has_is_private() && msg.is_private())
        allow = stat.checkUserAction<schema::items>(db, "add_private_item");
    else
        allow = stat.checkUserAction<schema::items>(db, "add_public_item");

    if(allow){
        run_saveItemInDb(db, msg);
    } else {
        sendServerError(pb::Error_AccesDeny);
    }
}

#include <QJsonObject>

void ItemPU::run_saveItemInDb(DB &db, ItemRequest_Add &msg)
{
    //check parameters

    QJsonObject params;

    if(msg.parameters_size() > 0){
        static constexpr schema::parameters p;
        std::vector<quint64> parametersIds;
        for(const auto &parameter:msg.parameters())
            parametersIds.push_back( parameter.id() );

        // check is all parameters are avalible in database
        bool allAvalible = db( select(count(p.uid))
                               .from(p)
                               .where(p.uid.in(sqlpp::value_list(parametersIds))))
                .front().count == parametersIds.size();

        if(!allAvalible){
            add_response()->mutable_itemres()->set_code(pb::ItemResponse_Replay_ParameterDontExists);
            return;
        }

//        for(const auto &parameter:msg.parameters()){
//            params.insert("id", parameter.id() );
//        }
    }

    auto res = add_response()->mutable_itemres();
    auto prep = db.prepare(sqlpp::postgresql::insert_into(i).set(
                               i.name = parameter(i.name),
                               i.category_id = msg.category_id(),
                               i.symbol = parameter(i.symbol),
                               i.owner = user()->id(),
                               i.description = parameter(i.description),
                               i.params = parameter(i.params)
            ).returning(i.uid) );

    prep.params.name = msg.name();
    prep.params.symbol = msg.symbol();

    if(msg.has_description() && !msg.description().empty())
        prep.params.description = msg.description();

    auto return_value = db(prep);

    if(msg.has_returning_id() && msg.returning_id())
        res->set_id(return_value.front().uid);

    res->set_code(ItemResponse_Replay_OK);
}

}
}

{'fraction_non_alphanumeric': 0.1357318070318888, 'fraction_numerical': 0.0024529844644317253, 'mean_word_length': 2.2617777777777777, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7578226', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2231, 'n_words': 387}