Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UsersServer #10

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Исправлены замечания
  • Loading branch information
lerakrya8 committed Dec 16, 2020
commit 935c749fcf64b7b40ec523735f55346fa06cfd16
7 changes: 4 additions & 3 deletions ServerUsers/project/includes/Database.h
Original file line number Diff line number Diff line change
@@ -13,9 +13,10 @@ class Database {
public:
Database() : database_("dbname=users host=localhost user=andrewkireev password=") {}
virtual ~Database() {}
virtual bool insert_(const std::map<std::string, std::string>& users_data) = 0;
virtual bool delete_(int id) = 0;
virtual bool update_(const std::map<std::string, std::string>& data) = 0;
void insert_();
// virtual bool delete_(int id) = 0;
// pqxx::result select_();

protected:
pqxx::connection database_;
};
2 changes: 1 addition & 1 deletion ServerUsers/project/includes/HandlerUser.h
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ class HandlerUser {
static std::map<std::string, std::string> parser_json(std::string& request);
void handle_request(std::string& request);
void create_user(int number_request, const std::map<std::string, std::string>& data_user, int user_id) const;
void data_user(int id, int number_request, int user_id) const;
void data_user(int id, int number_request) const;
void all_users(int number_request) const;
// void delete_user(int id, int number_request) const;
void update_data(int number_request, const std::map<std::string, std::string>& data_user, int user_id) const;
6 changes: 3 additions & 3 deletions ServerUsers/project/includes/Session.h
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
#include <boost/asio.hpp>
#include "UsersDatabase.h"

const int max_length = 1024;

class Session : public std::enable_shared_from_this<Session>
{
public:
@@ -30,9 +32,7 @@ class Session : public std::enable_shared_from_this<Session>

boost::asio::io_service& io_service_;
boost::asio::ip::tcp::socket socket_;
int max_length = 1024;
char data_[1024];
// std::string data_;
char data_[max_length];
UsersDatabase& data_base_;
};

8 changes: 4 additions & 4 deletions ServerUsers/project/includes/UsersDatabase.h
Original file line number Diff line number Diff line change
@@ -12,13 +12,13 @@
class UsersDatabase : public Database {
public:
~UsersDatabase() override;
bool insert_(const std::map<std::string, std::string>& users_data) override;
bool insert(const std::map<std::string, std::string>& users_data, int id_user);
std::string data_user(int id);
std::string all_users();
bool delete_(int id) override;
bool update_(const std::map<std::string, std::string>& data) override;
// bool delete_(int id) override;
bool update(const std::map<std::string, std::string>& data, int id_user);
bool exist(int id);
std::string id_by_nick(int number_request, std::string& nickname);
std::string id_by_nick(std::string& nickname);
};

#endif //SERVERUSERS_USERSDATABASE_H
17 changes: 17 additions & 0 deletions ServerUsers/project/src/Database.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by lerakrya on 16.12.2020.
//

#include "Database.h"

void Database::insert_() {
pqxx::work W(database_);
W.exec(sql_request);
W.commit();
}

//pqxx::result Database::select_() {
// pqxx::nontransaction N(database_);
// return pqxx::result R( N.exec( sql_request ));
//}

33 changes: 25 additions & 8 deletions ServerUsers/project/src/HandlerUser.cpp
Original file line number Diff line number Diff line change
@@ -17,10 +17,27 @@ std::map<string, string> HandlerUser::parser_json(string& request) {
std::stringstream stream_request(request);
boost::property_tree::read_json(stream_request, pt);
std::map<string, string> result;
char alf = 'A';
for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
result[alf + std::string(it->first)] = it->second.get_value<string>();
alf++;
string field;
if (std::string(it->first) == "id") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему нет сущности, хранящей названия полей?

field = "Aid";
}
if (std::string(it->first) == "firstname") {
field = "Bfirstname";
}
if (std::string(it->first) == "lastname") {
field = "Clastname";
}
if (std::string(it->first) == "nickname") {
field = "Dlastname";
}
if (std::string(it->first) == "email") {
field = "Eemail";
}
if (std::string(it->first) == "photo") {
field = "Fphoto";
}
result[field] = it->second.get_value<string>();
}
return result;
}
@@ -80,7 +97,7 @@ void HandlerUser::handle_request(string& request) {
is_exist(num_request, id_exists);
}
else if (data_id != 0) {
data_user(data_id, num_request, id_user);
data_user(data_id, num_request);
}
else {
id_by_nick(num_request, method);
@@ -89,7 +106,7 @@ void HandlerUser::handle_request(string& request) {

void HandlerUser::create_user(int number_request, const std::map<string, string>& data_user, int user_id) const {
try {
int result = data_base_.insert_(data_user);
int result = data_base_.insert_(data_user, user_id);
string str_result = std::to_string(number_request) + "\n";
str_result += "{\n \"response\": " + std::to_string(result) + "\n}";
// if (result == 200) {
@@ -104,7 +121,7 @@ void HandlerUser::create_user(int number_request, const std::map<string, string>
}
}

void HandlerUser::data_user(int id, int number_request, int user_id) const {
void HandlerUser::data_user(int id, int number_request) const {
try {
data_base_.data_user(id);
string result = std::to_string(number_request) + "\n";
@@ -158,7 +175,7 @@ void HandlerUser::all_users(int number_request) const {

void HandlerUser::update_data(int number_request, const std::map<string, string>& data_user, int user_id) const {
try {
int result = data_base_.update_(data_user);
int result = data_base_.update_(data_user, user_id);
string str_result = std::to_string(number_request) + "\n";
str_result += "{\n \"response\": " + std::to_string(result) + "\n}";
// if (result == 200) {
@@ -185,7 +202,7 @@ void HandlerUser::is_exist(int number_request, int id_user) const {
}
}

void HandlerUser::id_by_nick(int number_request, std::string& nickname) {
void HandlerUser::id_by_nick(int number_request, string& nickname) {
try {
data_base_.all_users();
string result = std::to_string(number_request) + "\n";
2 changes: 1 addition & 1 deletion ServerUsers/project/src/Session.cpp
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ void Session::send_answer(std::string& answer) {
void Session::start()
{
// std::cout << "start" << std::endl;
socket_.async_read_some(boost::asio::buffer(data_),
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&Session::handle_read, this,shared_from_this(),
boost::asio::placeholders::error));
}
53 changes: 27 additions & 26 deletions ServerUsers/project/src/UsersDatabase.cpp
Original file line number Diff line number Diff line change
@@ -9,7 +9,11 @@ using std::string;

UsersDatabase::~UsersDatabase() = default;

bool UsersDatabase::insert_(const std::map<string, string>& users_data) {
bool UsersDatabase::insert(const std::map<string, string>& users_data, int id_user) {
if (id_user == -1) {
return false;
}

string sql_request("INSERT INTO users VALUES(");
int i = 0;
for(auto& data : users_data) {
@@ -24,22 +28,18 @@ using std::string;
}
sql_request += ")";

if(data_user(atoi(users_data.at("Aid").c_str())) == "No user") {
pqxx::work W(database_);
W.exec(sql_request);
W.commit();
if (exist(atoi(users_data.at("Aid").c_str())) == false) {
insert_();
return true;
}
return false;
}


string UsersDatabase::data_user(int id) {
string sql_request("SELECT * FROM Users WHERE id=");
string sql_request = "SELECT * FROM Users WHERE id=";
sql_request += std::to_string(id);

pqxx::nontransaction N(database_);

pqxx::result R( N.exec( sql_request ));

string answer = "{\n";
@@ -64,7 +64,6 @@ using std::string;
string sql_request("SELECT nickname FROM users");

pqxx::nontransaction N(database_);

pqxx::result R( N.exec( sql_request ));

string answer = "{\n";
@@ -86,7 +85,6 @@ using std::string;
sql_request += std::to_string(id);

pqxx::nontransaction N(database_);

pqxx::result R( N.exec( sql_request ));

string answer = "{\n";
@@ -102,12 +100,11 @@ using std::string;
return true;
}

std::string UsersDatabase::id_by_nick(int number_request, std::string& nickname) {
std::string UsersDatabase::id_by_nick(std::string& nickname) {
string sql_request("SELECT id FROM Users WHERE nickname=");
sql_request += nickname;

pqxx::nontransaction N(database_);

pqxx::result R( N.exec( sql_request ));

string answer = "{\n";
@@ -123,23 +120,27 @@ using std::string;
return answer;
}

bool UsersDatabase::delete_(int id) {
string sql_request("DELETE FROM users WHERE id=");
sql_request += std::to_string(id);
// bool UsersDatabase::delete_(int id) {
// string sql_request("DELETE FROM users WHERE id=");
// sql_request += std::to_string(id);
//
// if (data_user(id) == "No user") {
// return false;
// }
//
// pqxx::work W(database_);
//
// W.exec( sql_request );
// W.commit();
//
// return true;
// }

if (data_user(id) == "No user") {
bool UsersDatabase::update(const std::map<string, string>& data, int id_user) {
if (data.at("Aid") != std::to_string(id_user)) {
return false;
}

pqxx::work W(database_);

W.exec( sql_request );
W.commit();

return true;
}

bool UsersDatabase::update_(const std::map<string, string>& data) {
string sql_request("UPDATE users SET ");

int i = 0;
@@ -155,7 +156,7 @@ using std::string;

sql_request += " WHERE id=" + data.at("Aid");

if(data_user(atoi(data.at("Aid").c_str())) == "No user") {
if(exist(atoi(data.at("Aid").c_str())) == false) {
return false;
}
pqxx::work W(database_);