This commit is contained in:
2026-06-10 15:42:34 +05:30
parent 602b538830
commit ec1f4ea117
13 changed files with 240 additions and 107 deletions
@@ -6,42 +6,68 @@ Date: 19-May-2026
*/ */
#pragma once #pragma once
#include <windows.h>
#include <string> #include <string>
#include "Map.h" #include "Map.h"
#include "MappingInfo.h"
#include "TrackedRecord.h"
#include "SharedMemory.h"
class User; class User;
class Notification;
class Service; class Service;
class ComboPackage; class ComboPackage;
class InventoryItem;
class ServiceBooking; class ServiceBooking;
class JobCard; class JobCard;
class InventoryItem;
class Invoice; class Invoice;
class Payment; class Payment;
class DataStore class DataStore
{ {
private: private:
util::Map<std::string, User*> m_users; DataStore();
util::Map<std::string, Service*> m_services;
util::Map<std::string, ComboPackage*> m_comboPackages;
util::Map<std::string, ServiceBooking*> m_serviceBookings;
util::Map<std::string, JobCard*> m_jobCards;
util::Map<std::string, InventoryItem*> m_inventoryItems;
util::Map<std::string, Invoice*> m_invoices;
util::Map<std::string, Payment*> m_payments;
DataStore() {}
public:
static DataStore& getInstance();
DataStore(const DataStore&) = delete; DataStore(const DataStore&) = delete;
DataStore& operator=(const DataStore&) = delete; DataStore& operator=(const DataStore&) = delete;
DataStore(DataStore&&) = delete; DataStore(DataStore&&) = delete;
DataStore& operator=(DataStore&&) = delete; DataStore& operator=(DataStore&&) = delete;
util::Map<std::string, User*>& getUsers(); bool unlockMutex();
util::Map<std::string, Service*>& getServices(); HANDLE m_globalMutex;
util::Map<std::string, ComboPackage*>& getComboPackages(); MappingInfo m_users;
util::Map<std::string, ServiceBooking*>& getServiceBookings(); MappingInfo m_notifications;
util::Map<std::string, JobCard*>& getJobCards(); MappingInfo m_services;
util::Map<std::string, InventoryItem*>& getInventoryItems(); MappingInfo m_comboPackages;
util::Map<std::string, Invoice*>& getInvoices(); MappingInfo m_inventoryItems;
util::Map<std::string, Payment*>& getPayments(); MappingInfo m_serviceBookings;
}; MappingInfo m_jobCards;
MappingInfo m_invoices;
MappingInfo m_payments;
public:
static DataStore& getInstance();
bool initialize();
void shutdown();
util::Map<std::string, TrackedRecord<User>> getUsers();
util::Map<std::string, TrackedRecord<Notification>> getNotifications();
util::Map<std::string, TrackedRecord<Service>> getServices();
util::Map<std::string, TrackedRecord<ComboPackage>> getComboPackages();
util::Map<std::string, TrackedRecord<InventoryItem>> getInventoryItems();
util::Map<std::string, TrackedRecord<ServiceBooking>> getServiceBookings();
util::Map<std::string, TrackedRecord<JobCard>> getJobCards();
util::Map<std::string, TrackedRecord<Invoice>> getInvoices();
util::Map<std::string, TrackedRecord<Payment>> getPayments();
void saveUsers(util::Map<std::string, TrackedRecord<User>>& users);
void saveNotifications(util::Map<std::string, TrackedRecord<Notification>>& notifications);
void saveServices(util::Map<std::string, TrackedRecord<Service>>& services);
void saveComboPackages(util::Map<std::string, TrackedRecord<ComboPackage>>& comboPackages);
void saveInventoryItems(util::Map<std::string, TrackedRecord<InventoryItem>>& inventoryItems);
void saveServiceBookings(util::Map<std::string, TrackedRecord<ServiceBooking>>& bookings);
void saveJobCards(util::Map<std::string, TrackedRecord<JobCard>>& jobCards);
void saveInvoices(util::Map<std::string, TrackedRecord<Invoice>>& invoices);
void savePayments(util::Map<std::string, TrackedRecord<Payment>>& payments);
bool lockDataStore();
bool unlockDataStore();
private:
template<typename TObject, typename TSerialized>
util::Map<std::string, TrackedRecord<TObject>> loadRecords(MappingInfo& mapping);
template<typename TObject, typename TSerialized>
void saveRecords(MappingInfo& mapping, util::Map<std::string, TrackedRecord<TObject>>& records);
};
@@ -0,0 +1,8 @@
#pragma once
#include <cstddef>
struct FileHeader
{
size_t recordCount;
size_t capacity;
};
@@ -0,0 +1,19 @@
#pragma once
#include <windows.h>
#include <string>
struct MappingInfo
{
HANDLE fileHandle;
HANDLE mappingHandle;
void* mappedView;
std::string fileName;
size_t recordSize;
size_t mappedCapacity;
MappingInfo()
: fileHandle(NULL),
mappingHandle(NULL),
mappedView(nullptr),
recordSize(0),
mappedCapacity(0) {}
};
@@ -0,0 +1,9 @@
#pragma once
enum class RecordState : int
{
CLEAN,
NEW_RECORD,
MODIFIED,
DELETED
};
@@ -0,0 +1,58 @@
#pragma once
#include "Utility.h"
struct SerializedUser
{
char id[64];
char username[64];
char password[64];
char name[128];
char phone[32];
char email[128];
util::UserType userType;
util::State status;
};
struct SerializedNotification
{
};
struct SerializedService
{
char id[64];
char name[128];
char inventoryItemIDs[1024];
double laborCost;
util::State status;
};
struct SerializedComboPackage
{
};
struct SerializedInventoryItem
{
};
struct SerializedServiceBooking
{
};
struct SerializedJobCard
{
};
struct SerializedInvoice
{
};
struct SerializedPayment
{
};
@@ -0,0 +1,19 @@
#pragma once
#include <cstddef>
#include "MappingInfo.h"
#include "FileHeader.h"
namespace SharedMemory
{
bool createOrOpenMapping(MappingInfo& mapping);
void closeMapping(MappingInfo& mapping);
bool ensureLatestMapping(MappingInfo& mapping);
bool resizeMapping(MappingInfo& mapping, size_t newCapacity);
FileHeader* getHeader(MappingInfo& mapping);
void* getRecordAddress(MappingInfo& mapping, size_t index);
size_t getRecordCount(MappingInfo& mapping);
void setRecordCount(MappingInfo& mapping, size_t count);
size_t getCapacity(MappingInfo& mapping);
bool ensureCapacityForInsert(MappingInfo& mapping);
bool shrinkIfNeeded(MappingInfo& mapping);
};
@@ -0,0 +1,23 @@
#pragma once
#include "RecordState.h"
static const size_t INVALID_SLOT = static_cast<size_t>(-1);
template<typename T>
struct TrackedRecord
{
T* data;
RecordState state;
size_t slotIndex;
TrackedRecord()
: data(nullptr),
state(RecordState::CLEAN),
slotIndex(INVALID_SLOT) {}
TrackedRecord(
T* object,
RecordState recordState,
size_t slot)
: data(object),
state(recordState),
slotIndex(slot) {}
};
@@ -8,6 +8,7 @@ Date: 19-May-2026
*/ */
#include <sstream> #include <sstream>
#include "SerializedRecords.h"
#include "Service.h" #include "Service.h"
#include "InventoryItem.h" #include "InventoryItem.h"
#include "StringHelper.h" #include "StringHelper.h"
@@ -266,61 +267,40 @@ static util::Vector<std::string> getInventoryItemIDsAsVector(const std::string&
/* /*
Function: serialize Function: serialize
Description: Serializes the service into a CSV-formatted string. Description: Serializes the Service object into a SerializedService record.
Parameters: Parameters:
- None - None
Returns: Returns:
- std::string: Serialized service record - SerializedService: Serialized representation of the service
*/ */
std::string Service::serialize() const SerializedService Service::serialize() const
{ {
std::ostringstream serializedService; SerializedService serialized = {};
serializedService << m_id << ',' strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
<< m_name << ',' strcpy_s(serialized.name, sizeof(serialized.name), m_name.c_str());
<< getInventoryItemIDsAsString(m_requiredInventoryItemIDs) << ',' strcpy_s(serialized.inventoryItemIDs, sizeof(serialized.inventoryItemIDs), getInventoryItemIDsAsString(m_requiredInventoryItemIDs).c_str());
<< m_laborCost << ',' serialized.laborCost = m_laborCost;
<< util::getStateString(m_status); serialized.status = m_status;
return serializedService.str(); return serialized;
} }
/* /*
Function: deserialize Function: deserialize
Description: Deserializes a CSV-formatted string into a Service object. Description: Deserializes a SerializedService record into a Service object.
Parameters: Parameters:
- record: const std::string&, serialized service record - serializedService: const SerializedService&, serialized service record
Returns: Returns:
- Service*: Pointer to the deserialized Service object - Service*: Pointer to the deserialized Service object
Throws:
- std::runtime_error if labor cost parsing fails
*/ */
Service* Service::deserialize(const std::string& record) Service* Service::deserialize(const SerializedService& serializedService)
{ {
std::string id, name; util::Vector<std::string> inventoryItemIDs = getInventoryItemIDsAsVector(serializedService.inventoryItemIDs);
std::string inventoryItemIDsString, laborCostString, statusString;
double laborCost;
std::istringstream serializedService(record);
getline(serializedService, id, ',');
getline(serializedService, name, ',');
getline(serializedService, inventoryItemIDsString, ',');
getline(serializedService, laborCostString, ',');
getline(serializedService, statusString, ',');
util::Vector<std::string> inventoryItemIDs = getInventoryItemIDsAsVector(inventoryItemIDsString);
try
{
laborCost = std::stod(laborCostString);
}
catch (...)
{
throw std::runtime_error("Invalid labor cost");
}
util::State status = util::getState(statusString);
return Factory::getObject<Service>( return Factory::getObject<Service>(
id, serializedService.id,
name, serializedService.name,
inventoryItemIDs, inventoryItemIDs,
laborCost, serializedService.laborCost,
status serializedService.status);
);
} }
/* /*
@@ -14,6 +14,7 @@ Date: 19-May-2026
#include "Enums.h" #include "Enums.h"
class InventoryItem; class InventoryItem;
struct SerializedService;
class Service class Service
{ {
@@ -40,7 +41,7 @@ public:
void setRequiredInventoryItems(const util::Map<std::string, InventoryItem*>& requiredInventoryItems); void setRequiredInventoryItems(const util::Map<std::string, InventoryItem*>& requiredInventoryItems);
void setLaborCost(double laborCost); void setLaborCost(double laborCost);
void setState(util::State status); void setState(util::State status);
std::string serialize() const; SerializedService serialize() const;
static Service* deserialize(const std::string&); static Service* deserialize(const SerializedService&);
static std::string getHeaders(); static std::string getHeaders();
}; };
@@ -8,6 +8,7 @@ Date: 19-May-2026
*/ */
#include <sstream> #include <sstream>
#include "SerializedRecords.h"
#include "User.h" #include "User.h"
#include "Notification.h" #include "Notification.h"
#include "Enums.h" #include "Enums.h"
@@ -324,57 +325,45 @@ void User::setState(util::State status)
/* /*
Function: serialize Function: serialize
Description: Serializes the user into a CSV-formatted string. Description: Serializes the User object into a SerializedUser record.
Parameters: Parameters:
- None - None
Returns: Returns:
- std::string: Serialized user record - SerializedUser: Serialized representation of the user
*/ */
std::string User::serialize() const SerializedUser User::serialize() const
{ {
std::ostringstream serializedUser; SerializedUser serialized = {};
serializedUser << m_id << ',' strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
<< m_userName << ',' strcpy_s(serialized.username, sizeof(serialized.username), m_userName.c_str());
<< m_password << ',' strcpy_s(serialized.password, sizeof(serialized.password), m_password.c_str());
<< m_name << ',' strcpy_s(serialized.name, sizeof(serialized.name), m_name.c_str());
<< m_phone << ',' strcpy_s(serialized.phone, sizeof(serialized.phone), m_phone.c_str());
<< m_email << ',' strcpy_s(serialized.email, sizeof(serialized.email), m_email.c_str());
<< util::getUserTypeString(m_type) << ',' serialized.userType = m_type;
<< util::getStateString(m_status); serialized.status = m_status;
return serializedUser.str(); return serialized;
} }
/* /*
Function: deserialize Function: deserialize
Description: Deserializes a CSV-formatted string into a User object. Description: Deserializes a SerializedUser record into a User object.
Parameters: Parameters:
- record: const std::string&, serialized user record - serializedUser: const SerializedUser&, serialized user record
Returns: Returns:
- User*: Pointer to the deserialized User object - User*: Pointer to the deserialized User object
*/ */
User* User::deserialize(const std::string& record) User* User::deserialize(const SerializedUser& serializedUser)
{ {
std::string id, name, username, phone, password, email; return Factory::getObject<User>(
std::string userTypeString, stateString; serializedUser.id,
std::istringstream serializedUser(record); serializedUser.username,
getline(serializedUser, id, ','); serializedUser.password,
getline(serializedUser, username, ','); serializedUser.name,
getline(serializedUser, password, ','); serializedUser.phone,
getline(serializedUser, name, ','); serializedUser.email,
getline(serializedUser, phone, ','); serializedUser.userType,
getline(serializedUser, email, ','); serializedUser.status);
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);
} }
/* /*
@@ -14,6 +14,7 @@ Date: 19-May-2026
#include "Enums.h" #include "Enums.h"
class Notification; class Notification;
struct SerializedUser;
class User : public Observer class User : public Observer
{ {
@@ -51,7 +52,7 @@ public:
void addNotification(Notification* notification) override; void addNotification(Notification* notification) override;
void setRole(util::UserType role); void setRole(util::UserType role);
void setState(util::State status); void setState(util::State status);
std::string serialize() const; SerializedUser serialize() const;
static User* deserialize(const std::string&); static User* deserialize(const SerializedUser& serializedUser);
static std::string getHeaders(); static std::string getHeaders();
}; };
@@ -12,28 +12,28 @@ Date: 19-May-2026
namespace util namespace util
{ {
enum class UserType enum class UserType : int
{ {
ADMIN, ADMIN,
TECHNICIAN, TECHNICIAN,
CUSTOMER CUSTOMER
}; };
enum class PaymentMode enum class PaymentMode : int
{ {
ONLINE, ONLINE,
OFFLINE, OFFLINE,
NOTSET NOTSET
}; };
enum class PaymentStatus enum class PaymentStatus : int
{ {
PENDING, PENDING,
COMPLETED, COMPLETED,
PAID PAID
}; };
enum class ServiceJobStatus enum class ServiceJobStatus : int
{ {
PENDING, PENDING,
STARTED, STARTED,
@@ -42,7 +42,7 @@ namespace util
CANCELLED CANCELLED
}; };
enum class State enum class State : int
{ {
ACTIVE, ACTIVE,
INACTIVE INACTIVE