mirror of
https://gitlab.com/comunic/comunicterm
synced 2024-12-25 13:08:54 +00:00
Can get user info
This commit is contained in:
parent
963edbf6e3
commit
3ddaca2376
@ -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
47
entities/user.cpp
Normal 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
33
entities/user.h
Normal 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
64
helpers/userhelper.cpp
Normal 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
25
helpers/userhelper.h
Normal 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;
|
||||
};
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user