227 lines
5.1 KiB
C++
227 lines
5.1 KiB
C++
#include <sstream>
|
|
#include "User.h"
|
|
#include "Notification.h"
|
|
#include "Enums.h"
|
|
#include "Factory.h"
|
|
#include "StringHelper.h"
|
|
|
|
int User::m_uid = 0;
|
|
|
|
User::User()
|
|
: m_id("USR" + std::to_string(++m_uid)),
|
|
m_type(util::UserType::CUSTOMER),
|
|
m_status(util::State::ACTIVE) {}
|
|
|
|
User::User(const std::string& userName, const std::string& password, const std::string& name, const std::string& phone, const std::string& email, util::UserType role)
|
|
: m_id("USR" + std::to_string(++m_uid)),
|
|
m_userName(userName),
|
|
m_password(password),
|
|
m_name(name),
|
|
m_phone(phone),
|
|
m_email(email),
|
|
m_type(role),
|
|
m_status(util::State::ACTIVE) {}
|
|
|
|
/*
|
|
Function: User (parameterized constructor with ID)
|
|
Description: Initializes a user with an existing ID, credentials, personal details,
|
|
role, and state. Updates UID tracking based on ID.
|
|
Parameters:
|
|
- userId: const std::string&, unique user ID
|
|
- userName: const std::string&, username
|
|
- password: const std::string&, password
|
|
- name: const std::string&, full name
|
|
- phone: const std::string&, phone number
|
|
- email: const std::string&, email address
|
|
- role: util::UserType, role of the user
|
|
- status: util::State, state of the user (ACTIVE/INACTIVE)
|
|
Returns:
|
|
- A new User object
|
|
*/
|
|
User::User(const std::string& userId, const std::string& userName, const std::string& password, const std::string& name, const std::string& phone, const std::string& email, util::UserType role, util::State status)
|
|
: m_id(userId),
|
|
m_userName(userName),
|
|
m_password(password),
|
|
m_name(name),
|
|
m_phone(phone),
|
|
m_email(email),
|
|
m_type(role),
|
|
m_status(status)
|
|
{
|
|
int idNumber = util::extractNumber(m_id);
|
|
if (idNumber > m_uid)
|
|
{
|
|
m_uid = idNumber;
|
|
}
|
|
}
|
|
|
|
User::~User()
|
|
{
|
|
auto values = m_notifications.getValues();
|
|
for (int index = 0; index < values.getSize(); index++)
|
|
{
|
|
delete values[index];
|
|
}
|
|
}
|
|
|
|
const std::string& User::getId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
|
|
const std::string& User::getUserName() const
|
|
{
|
|
return m_userName;
|
|
}
|
|
|
|
const std::string& User::getPassword() const
|
|
{
|
|
return m_password;
|
|
}
|
|
|
|
const std::string& User::getName() const
|
|
{
|
|
return m_name;
|
|
}
|
|
|
|
const std::string& User::getPhone() const
|
|
{
|
|
return m_phone;
|
|
}
|
|
|
|
const std::string& User::getEmail() const
|
|
{
|
|
return m_email;
|
|
}
|
|
|
|
util::Map<std::string, Notification*>& User::getNotifications()
|
|
{
|
|
return m_notifications;
|
|
}
|
|
|
|
util::UserType User::getUserType() const
|
|
{
|
|
return m_type;
|
|
}
|
|
|
|
util::State User::getState() const
|
|
{
|
|
return m_status;
|
|
}
|
|
|
|
void User::setId(const std::string& id)
|
|
{
|
|
m_id = id;
|
|
}
|
|
|
|
void User::setUserName(const std::string& userName)
|
|
{
|
|
m_userName = userName;
|
|
}
|
|
|
|
void User::setPassword(const std::string& password)
|
|
{
|
|
m_password = password;
|
|
}
|
|
|
|
void User::setName(const std::string& name)
|
|
{
|
|
m_name = name;
|
|
}
|
|
|
|
void User::setPhone(const std::string& phone)
|
|
{
|
|
m_phone = phone;
|
|
}
|
|
|
|
void User::setEmail(const std::string& email)
|
|
{
|
|
m_email = email;
|
|
}
|
|
|
|
void User::addNotification(Notification* notification)
|
|
{
|
|
m_notifications.insert(notification->getId(), notification);
|
|
}
|
|
|
|
void User::setRole(util::UserType role)
|
|
{
|
|
m_type = role;
|
|
}
|
|
|
|
void User::setState(util::State status)
|
|
{
|
|
m_status = status;
|
|
}
|
|
|
|
void User::update(Notification* notification)
|
|
{
|
|
}
|
|
|
|
/*
|
|
Function: serialize
|
|
Description: Serializes the user into a CSV-formatted string.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Serialized user record
|
|
*/
|
|
std::string User::serialize() const
|
|
{
|
|
std::ostringstream serializedUser;
|
|
serializedUser << m_id << ','
|
|
<< m_userName << ','
|
|
<< m_password << ','
|
|
<< m_name << ','
|
|
<< m_phone << ','
|
|
<< m_email << ','
|
|
<< util::getUserTypeString(m_type) << ','
|
|
<< util::getStateString(m_status);
|
|
return serializedUser.str();
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a CSV-formatted string into a User object.
|
|
Parameters:
|
|
- record: const std::string&, serialized user record
|
|
Returns:
|
|
- User*: Pointer to the deserialized User object
|
|
*/
|
|
User* User::deserialize(const std::string& record)
|
|
{
|
|
std::string id, name, username, phone, password, email;
|
|
std::string userTypeString, stateString;
|
|
std::istringstream serializedUser(record);
|
|
getline(serializedUser, id, ',');
|
|
getline(serializedUser, username, ',');
|
|
getline(serializedUser, password, ',');
|
|
getline(serializedUser, name, ',');
|
|
getline(serializedUser, phone, ',');
|
|
getline(serializedUser, email, ',');
|
|
getline(serializedUser, userTypeString, ',');
|
|
getline(serializedUser, stateString);
|
|
util::UserType userType = util::getUserType(userTypeString);
|
|
util::State status = util::getState(stateString);
|
|
return Factory::getObject<User>(id,
|
|
username,
|
|
password,
|
|
name,
|
|
phone,
|
|
email,
|
|
userType,
|
|
status);
|
|
}
|
|
|
|
/*
|
|
Function: getHeaders
|
|
Description: Retrieves the CSV headers for user serialization.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Header string ("ID,Username,Password,Name,Phone,Email,UserType,UserStatus")
|
|
*/
|
|
std::string User::getHeaders()
|
|
{
|
|
return "ID,Username,Password,Name,Phone,Email,UserType,UserStatus";
|
|
} |