diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.cpp index 0bae917..ad1b416 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.cpp @@ -7,6 +7,7 @@ Date: 19-May-2026 */ #include +#include "SerializedRecords.h" #include "Notification.h" #include "StringHelper.h" #include "Factory.h" @@ -23,7 +24,8 @@ Returns: */ Notification::Notification() : m_id("NOT" + std::to_string(++m_uid)), - m_recipient(nullptr) {} + m_recipient(nullptr), m_state(util::State::ACTIVE) { +} /* Function: Notification @@ -43,7 +45,9 @@ Notification::Notification(const std::string& recipientUserId, User* recipient, m_recipient(recipient), m_title(title), m_message(message), - m_createdAt(createdAt) {} + m_state(util::State::ACTIVE), + m_createdAt(createdAt) { +} /* Function: Notification (parameterized constructor with ID) @@ -58,13 +62,14 @@ Parameters: Returns: - A new Notification object */ -Notification::Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt) +Notification::Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt, const util::State& state) : m_id(id), m_recipientUserId(recipientUserId), m_recipient(nullptr), m_title(title), m_message(message), - m_createdAt(createdAt) + m_createdAt(createdAt), + m_state(state) { int idNumber = util::extractNumber(m_id); if (idNumber > m_uid) @@ -79,7 +84,7 @@ Description: Retrieves the unique ID of the notification. Returns: - const std::string& representing the notification ID. */ -const std::string& Notification::getId() const +const std::string& Notification::getId() const { return m_id; } @@ -139,6 +144,17 @@ const util::Timestamp& Notification::getCreatedAt() const return m_createdAt; } +/* +Function: getState +Description: Retrieves the Notification state +Returns: + - const util::Timestamp& representing the creation timestamp. +*/ +util::State Notification::getState() const +{ + return m_state; +} + /* Function: setId Description: Sets the unique ID of the notification. @@ -217,71 +233,54 @@ void Notification::setCreatedAt(const util::Timestamp& createdAt) m_createdAt = createdAt; } +/* +Function: setState +Description: Sets the Notification state. +Parameters: + - state: Notification state value. +Returns: + - void +*/ +void Notification::setState(util::State state) +{ + m_state = state; +} + /* Function: serialize -Description: Serializes the notification into a CSV-formatted string. +Description: Serializes the Notification object into a SerializedNotification record. Parameters: - None Returns: - - std::string: Serialized notification record + - SerializedNotification: Serialized representation of the notification */ -std::string Notification::serialize() const +SerializedNotification Notification::serialize() const { - std::ostringstream serializedNotification; - serializedNotification << m_id << ',' - << m_recipientUserId << ',' - << m_title << ',' - << m_message << ',' - << m_createdAt.toString(); - return serializedNotification.str(); + SerializedNotification serialized = {}; + strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str()); + strcpy_s(serialized.recipientUserId, sizeof(serialized.recipientUserId), m_recipientUserId.c_str()); + strcpy_s(serialized.title, sizeof(serialized.title), m_title.c_str()); + strcpy_s(serialized.message, sizeof(serialized.message), m_message.c_str()); + serialized.createdAt = m_createdAt; + serialized.state = m_state; + return serialized; } /* Function: deserialize -Description: Deserializes a CSV-formatted string into a Notification object. +Description: Deserializes a SerializedNotification record into a Notification object. Parameters: - - record: const std::string&, serialized notification record + - serializedNotification: const SerializedNotification&, serialized notification record Returns: - Notification*: Pointer to the deserialized Notification object -Throws: - - std::runtime_error if timestamp parsing fails */ -Notification* Notification::deserialize(const std::string& record) +Notification* Notification::deserialize(const SerializedNotification& serializedNotification) { - std::string id, recipientUserId, title, message, createdAtTimestampString; - std::istringstream serializedNotification(record); - getline(serializedNotification, id, ','); - getline(serializedNotification, recipientUserId, ','); - getline(serializedNotification, title, ','); - getline(serializedNotification, message, ','); - getline(serializedNotification, createdAtTimestampString, ','); - util::Timestamp createdAtTimestamp; - try - { - createdAtTimestamp = util::Timestamp::fromString(createdAtTimestampString); - } - catch (...) - { - throw std::runtime_error("Invalid createdAt timestamp"); - } return Factory::getObject( - id, - recipientUserId, - title, - message, - createdAtTimestamp - ); -} - -/* -Function: getHeaders -Description: Retrieves the CSV headers for notification serialization. -Parameters: - - None -Returns: - - std::string: Header string ("ID,RecipientID,Title,Message,Timestamp") -*/ -std::string Notification::getHeaders() -{ - return "ID,RecipientID,Title,Message,Timestamp"; + serializedNotification.id, + serializedNotification.recipientUserId, + serializedNotification.title, + serializedNotification.message, + serializedNotification.createdAt, + serializedNotification.state); } \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.h index 57869fa..d92bc2c 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.h @@ -9,8 +9,10 @@ Date: 19-May-2026 #pragma once #include #include "Timestamp.h" +#include "Enums.h" class User; +struct SerializedNotification; class Notification { @@ -22,10 +24,11 @@ private: std::string m_title; std::string m_message; util::Timestamp m_createdAt; + util::State m_state; public: Notification(); Notification(const std::string& recipientUserId, User* recipient, const std::string& title, const std::string& message, const util::Timestamp& createdAt); - Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt); + Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt, const util::State& state); const std::string& getId() const; const std::string& getRecipientUserId() const; User* getRecipient() const; @@ -38,7 +41,8 @@ public: void setTitle(const std::string& title); void setMessage(const std::string& message); void setCreatedAt(const util::Timestamp& createdAt); - std::string serialize() const; - static Notification* deserialize(const std::string&); - static std::string getHeaders(); + util::State getState() const; + void setState(util::State state); + SerializedNotification serialize() const; + static Notification* deserialize(const SerializedNotification&); };