1
0
mirror of https://gitlab.com/comunic/comunicterm synced 2024-11-16 02:21:08 +00:00

Can get user info

This commit is contained in:
Pierre HUBERT 2020-01-09 13:43:07 +01:00
parent 963edbf6e3
commit 3ddaca2376
6 changed files with 183 additions and 3 deletions

View File

@ -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

47
entities/user.cpp Normal file
View File

@ -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;
}

33
entities/user.h Normal file
View File

@ -0,0 +1,33 @@
/**
* Single user information
*
* @author Pierre HUBERT
*/
#pragma once
#include <string>
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;
};

64
helpers/userhelper.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "userhelper.h"
#include <api_request.h>
using namespace std;
std::map<int, User> UserHelper::mCache;
UserHelper::UserHelper()
{
}
User UserHelper::GetSingle(int id)
{
auto l = vector<int>();
l.push_back(id);
return getMultiple(l)[id];
}
map<int, User> UserHelper::getMultiple(std::vector<int> ids)
{
map<int, User> users;
vector<int> 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;
}

25
helpers/userhelper.h Normal file
View File

@ -0,0 +1,25 @@
/**
* User helper
*
* @author Pierre HUBERT
*/
#pragma once
#include <map>
#include <vector>
#include <entities/user.h>
class UserHelper
{
public:
UserHelper();
static User GetSingle(int id);
static std::map<int, User> getMultiple(std::vector<int> ids);
private:
static std::map<int, User> mCache;
};

View File

@ -5,6 +5,7 @@
#include <iostream>
#include <helpers/accounthelper.h>
#include <helpers/userhelper.h>
#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;