From 3ddaca237665cb4e0196b2e75d97cb1b30d13c65 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 9 Jan 2020 13:43:07 +0100 Subject: [PATCH] Can get user info --- ComunicTerm.pro | 8 ++++-- entities/user.cpp | 47 +++++++++++++++++++++++++++++++ entities/user.h | 33 ++++++++++++++++++++++ helpers/userhelper.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ helpers/userhelper.h | 25 +++++++++++++++++ loginscreen.cpp | 9 +++++- 6 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 entities/user.cpp create mode 100644 entities/user.h create mode 100644 helpers/userhelper.cpp create mode 100644 helpers/userhelper.h diff --git a/ComunicTerm.pro b/ComunicTerm.pro index e7b5bcf..443aa1b 100644 --- a/ComunicTerm.pro +++ b/ComunicTerm.pro @@ -11,7 +11,9 @@ SOURCES += \ apiresponse.cpp \ loginscreen.cpp \ ui_utils.cpp \ - helpers/accounthelper.cpp + helpers/accounthelper.cpp \ + helpers/userhelper.cpp \ + entities/user.cpp HEADERS += \ config.h \ @@ -19,4 +21,6 @@ HEADERS += \ apiresponse.h \ loginscreen.h \ ui_utils.h \ - helpers/accounthelper.h + helpers/accounthelper.h \ + helpers/userhelper.h \ + entities/user.h diff --git a/entities/user.cpp b/entities/user.cpp new file mode 100644 index 0000000..8cb6b93 --- /dev/null +++ b/entities/user.cpp @@ -0,0 +1,47 @@ +#include "user.h" + +User::User() +{ + +} + +User::User(int id, std::string firstName, std::string lastName) + : mID(id), mFirstName(firstName), mLastName(lastName) +{ + +} + +int User::iD() const +{ + return mID; +} + +void User::setID(int iD) +{ + mID = iD; +} + +std::string User::firstName() const +{ + return mFirstName; +} + +void User::setFirstName(const std::string &firstName) +{ + mFirstName = firstName; +} + +std::string User::lastName() const +{ + return mLastName; +} + +void User::setLastName(const std::string &lastName) +{ + mLastName = lastName; +} + +std::string User::fullName() const +{ + return mFirstName + " " + mLastName; +} diff --git a/entities/user.h b/entities/user.h new file mode 100644 index 0000000..5fb50e5 --- /dev/null +++ b/entities/user.h @@ -0,0 +1,33 @@ +/** + * Single user information + * + * @author Pierre HUBERT + */ + +#pragma once + +#include + + +class User +{ +public: + User(); + User(int id, std::string firstName, std::string lastName); + + int iD() const; + void setID(int iD); + + std::string firstName() const; + void setFirstName(const std::string &firstName); + + std::string lastName() const; + void setLastName(const std::string &lastName); + + std::string fullName() const; + +private: + int mID; + std::string mFirstName; + std::string mLastName; +}; diff --git a/helpers/userhelper.cpp b/helpers/userhelper.cpp new file mode 100644 index 0000000..7f27922 --- /dev/null +++ b/helpers/userhelper.cpp @@ -0,0 +1,64 @@ +#include "userhelper.h" + +#include + +using namespace std; + +std::map UserHelper::mCache; + +UserHelper::UserHelper() +{ + +} + +User UserHelper::GetSingle(int id) +{ + auto l = vector(); + l.push_back(id); + return getMultiple(l)[id]; +} + +map UserHelper::getMultiple(std::vector ids) +{ + map users; + vector toGet; + + // Check for missing users in cache + for(auto id : ids) { + if(users.find(id) == users.end()) + toGet.push_back(id); + else + users[id] = users[id]; + } + + // Get required users info + if(toGet.size() > 0) { + + string ids = ""; + for(auto i : toGet) ids += to_string(i) + ","; + + auto req = ApiRequest("user/getInfoMultiple", true); + req.addArg("usersID", ids); + + auto res = req.exec(); + if(res.code() != 200) + throw runtime_error("Could not get the list of users (status code of response: " + to_string(res.code())); + + + // Parse results + const auto obj = res.object(); + for(auto id : toGet) { + const auto user_obj = obj.at(to_string(id)).as_object(); + User newUser(user_obj.at("userID").as_number().to_int32(), + user_obj.at("firstName").as_string(), + user_obj.at("lastName").as_string()); + + mCache[id] = newUser; + users[id] = newUser; + } + + } + + + return users; +} diff --git a/helpers/userhelper.h b/helpers/userhelper.h new file mode 100644 index 0000000..14cb8f2 --- /dev/null +++ b/helpers/userhelper.h @@ -0,0 +1,25 @@ +/** + * User helper + * + * @author Pierre HUBERT + */ + +#pragma once + +#include +#include + +#include + + +class UserHelper +{ +public: + UserHelper(); + + static User GetSingle(int id); + static std::map getMultiple(std::vector ids); + +private: + static std::map mCache; +}; diff --git a/loginscreen.cpp b/loginscreen.cpp index 325fc72..2b06551 100644 --- a/loginscreen.cpp +++ b/loginscreen.cpp @@ -5,6 +5,7 @@ #include #include +#include #include "loginscreen.h" #include "ui_utils.h" @@ -59,7 +60,13 @@ bool LoginScreen::exec() } while(res != SUCCESS); - getch(); /* Wait for user input */ + try { + const auto user = UserHelper::GetSingle(AccountHelper::userID()); + ui_utils::alert(stdscr, "Welcome " + user.fullName() + "!"); + } catch (...) { + ui_utils::alert(stdscr, "Could not get user info!"); + } + endwin(); /* End curses mode */ return true;