Implement Model Refactoring
<UserStory> 1949: Model Refactoring</UserStory> UserStory #1949 <Changes> Replaced CSV-based serialization and deserialization in the Invoice model with fixed-size SerializedRecord structures for shared memory storage. Implemented serialize() method to convert Invoice objects into SerializedInvoice records using direct struct field assignment and strcpy_s for string fields. Implemented deserialize() method to reconstruct Invoice objects directly from SerializedInvoice types instead of parsing CSV strings. Updated Invoice class interfaces to use SerializedInvoice types, removing legacy CSV serialization APIs including getHeaders() function. Added SerializedRecords.h dependency and forward declarations for SerializedInvoice structure in Invoice header. Minor formatting adjustments in DataStore.cpp (closing brace placement). </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar, please review </Review>
This commit is contained in:
@@ -562,4 +562,3 @@ bool DataStore::unlockDataStore()
|
|||||||
}
|
}
|
||||||
return ReleaseMutex(m_globalMutex) != 0;
|
return ReleaseMutex(m_globalMutex) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ Date: 19-May-2026
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include "SerializedRecords.h"
|
||||||
#include "Invoice.h"
|
#include "Invoice.h"
|
||||||
#include "Factory.h"
|
#include "Factory.h"
|
||||||
#include "InventoryItem.h"
|
#include "InventoryItem.h"
|
||||||
@@ -34,7 +35,8 @@ Invoice::Invoice()
|
|||||||
m_discountPercentage(0.0),
|
m_discountPercentage(0.0),
|
||||||
m_totalAmount(0.0),
|
m_totalAmount(0.0),
|
||||||
m_paymentMethod(util::PaymentMode()),
|
m_paymentMethod(util::PaymentMode()),
|
||||||
m_status(util::PaymentStatus()) {}
|
m_status(util::PaymentStatus()) {
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: Invoice
|
Function: Invoice
|
||||||
@@ -66,7 +68,7 @@ Invoice::Invoice(
|
|||||||
const util::Timestamp& paymentDate,
|
const util::Timestamp& paymentDate,
|
||||||
util::PaymentMode paymentMethod,
|
util::PaymentMode paymentMethod,
|
||||||
util::PaymentStatus status
|
util::PaymentStatus status
|
||||||
)
|
)
|
||||||
: m_id("INV" + std::to_string(++m_uid)),
|
: m_id("INV" + std::to_string(++m_uid)),
|
||||||
m_bookingId(bookingId),
|
m_bookingId(bookingId),
|
||||||
m_booking(booking),
|
m_booking(booking),
|
||||||
@@ -473,100 +475,50 @@ static util::Vector<std::string> getPartIDsAsVector(const std::string& partIDsSt
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Function: serialize
|
Function: serialize
|
||||||
Description: Serializes the invoice into a CSV-formatted string.
|
Description: Serializes the Invoice object into a SerializedInvoice record.
|
||||||
Parameters:
|
Parameters:
|
||||||
- None
|
- None
|
||||||
Returns:
|
Returns:
|
||||||
- std::string: Serialized invoice record
|
- SerializedInvoice: Serialized representation of the invoice
|
||||||
*/
|
*/
|
||||||
std::string Invoice::serialize() const
|
SerializedInvoice Invoice::serialize() const
|
||||||
{
|
{
|
||||||
std::ostringstream serializedInvoice;
|
SerializedInvoice serialized = {};
|
||||||
serializedInvoice << m_id << ','
|
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
||||||
<< m_bookingId << ','
|
strcpy_s(serialized.bookingId, sizeof(serialized.bookingId), m_bookingId.c_str());
|
||||||
<< m_invoiceDate.toString() << ','
|
strcpy_s(serialized.partIDs, sizeof(serialized.partIDs), getPartIDsAsString(m_partIDs).c_str());
|
||||||
<< m_laborCost << ','
|
serialized.invoiceDate = m_invoiceDate;
|
||||||
<< getPartIDsAsString(m_partIDs) << ','
|
serialized.laborCost = m_laborCost;
|
||||||
<< m_partsCost << ','
|
serialized.partsCost = m_partsCost;
|
||||||
<< m_discountPercentage << ','
|
serialized.discountPercentage = m_discountPercentage;
|
||||||
<< m_totalAmount << ','
|
serialized.totalAmount = m_totalAmount;
|
||||||
<< m_paymentDate.toString() << ','
|
serialized.paymentDate = m_paymentDate;
|
||||||
<< util::getPaymentModeString(m_paymentMethod) << ','
|
serialized.paymentMethod = m_paymentMethod;
|
||||||
<< util::getPaymentStatusString(m_status);
|
serialized.status = m_status;
|
||||||
return serializedInvoice.str();
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: deserialize
|
Function: deserialize
|
||||||
Description: Deserializes a CSV-formatted string into an Invoice object.
|
Description: Deserializes a SerializedInvoice record into an Invoice object.
|
||||||
Parameters:
|
Parameters:
|
||||||
- record: const std::string&, serialized invoice record
|
- serializedInvoice: const SerializedInvoice&, serialized invoice record
|
||||||
Returns:
|
Returns:
|
||||||
- Invoice*: Pointer to the deserialized Invoice object
|
- Invoice*: Pointer to the deserialized Invoice object
|
||||||
Throws:
|
|
||||||
- std::runtime_error if data is invalid
|
|
||||||
*/
|
*/
|
||||||
Invoice* Invoice::deserialize(const std::string& record)
|
Invoice* Invoice::deserialize(const SerializedInvoice& serializedInvoice)
|
||||||
{
|
{
|
||||||
std::string id, bookingId;
|
util::Vector<std::string> partIDs = getPartIDsAsVector(serializedInvoice.partIDs);
|
||||||
std::string invoiceDateString, laborCostString, partIDsString;
|
|
||||||
std::string partsCostString, discountPercentageString, totalAmountString;
|
|
||||||
std::string paymentDateString, paymentMethodString, statusString;
|
|
||||||
double laborCost, partsCost, discountPercentage, totalAmount;
|
|
||||||
std::istringstream serializedInvoice(record);
|
|
||||||
getline(serializedInvoice, id, ',');
|
|
||||||
getline(serializedInvoice, bookingId, ',');
|
|
||||||
getline(serializedInvoice, invoiceDateString, ',');
|
|
||||||
getline(serializedInvoice, laborCostString, ',');
|
|
||||||
getline(serializedInvoice, partIDsString, ',');
|
|
||||||
getline(serializedInvoice, partsCostString, ',');
|
|
||||||
getline(serializedInvoice, discountPercentageString, ',');
|
|
||||||
getline(serializedInvoice, totalAmountString, ',');
|
|
||||||
getline(serializedInvoice, paymentDateString, ',');
|
|
||||||
getline(serializedInvoice, paymentMethodString, ',');
|
|
||||||
getline(serializedInvoice, statusString, ',');
|
|
||||||
util::Timestamp invoiceDate;
|
|
||||||
util::Timestamp paymentDate;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
invoiceDate = util::Timestamp::fromString(invoiceDateString);
|
|
||||||
paymentDate = util::Timestamp::fromString(paymentDateString);
|
|
||||||
laborCost = std::stod(laborCostString);
|
|
||||||
partsCost = std::stod(partsCostString);
|
|
||||||
discountPercentage = std::stod(discountPercentageString);
|
|
||||||
totalAmount = std::stod(totalAmountString);
|
|
||||||
}
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Invalid invoice data");
|
|
||||||
}
|
|
||||||
util::Vector<std::string> partIDs = getPartIDsAsVector(partIDsString);
|
|
||||||
util::PaymentMode paymentMethod = util::getPaymentMode(paymentMethodString);
|
|
||||||
util::PaymentStatus status = util::getPaymentStatus(statusString);
|
|
||||||
return Factory::getObject<Invoice>(
|
return Factory::getObject<Invoice>(
|
||||||
id,
|
serializedInvoice.id,
|
||||||
bookingId,
|
serializedInvoice.bookingId,
|
||||||
invoiceDate,
|
serializedInvoice.invoiceDate,
|
||||||
partIDs,
|
partIDs,
|
||||||
laborCost,
|
serializedInvoice.laborCost,
|
||||||
partsCost,
|
serializedInvoice.partsCost,
|
||||||
discountPercentage,
|
serializedInvoice.discountPercentage,
|
||||||
totalAmount,
|
serializedInvoice.totalAmount,
|
||||||
paymentDate,
|
serializedInvoice.paymentDate,
|
||||||
paymentMethod,
|
serializedInvoice.paymentMethod,
|
||||||
status
|
serializedInvoice.status);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function: getHeaders
|
|
||||||
Description: Retrieves the CSV headers for invoice serialization.
|
|
||||||
Parameters:
|
|
||||||
- None
|
|
||||||
Returns:
|
|
||||||
- std::string: Header string ("ID,BookingID,InvoiceDate,LaborCost,PartIDs,PartsCost,DiscountPercentage,TotalAmount,PaymentDate,PaymentMethod,Status")
|
|
||||||
*/
|
|
||||||
std::string Invoice::getHeaders()
|
|
||||||
{
|
|
||||||
return "ID,BookingID,InvoiceDate,LaborCost,PartIDs,PartsCost,DiscountPercentage,TotalAmount,PaymentDate,PaymentMethod,Status";
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,6 @@ Author: Trenser
|
|||||||
Date: 19-May-2026
|
Date: 19-May-2026
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Map.h"
|
#include "Map.h"
|
||||||
@@ -16,6 +15,7 @@ Date: 19-May-2026
|
|||||||
|
|
||||||
class ServiceBooking;
|
class ServiceBooking;
|
||||||
class InventoryItem;
|
class InventoryItem;
|
||||||
|
struct SerializedInvoice;
|
||||||
|
|
||||||
class Invoice
|
class Invoice
|
||||||
{
|
{
|
||||||
@@ -41,7 +41,7 @@ public:
|
|||||||
ServiceBooking* booking,
|
ServiceBooking* booking,
|
||||||
const util::Timestamp& invoiceDate,
|
const util::Timestamp& invoiceDate,
|
||||||
double laborCost,
|
double laborCost,
|
||||||
const util::Map<std::string,InventoryItem*>& parts,
|
const util::Map<std::string, InventoryItem*>& parts,
|
||||||
double partsCost,
|
double partsCost,
|
||||||
double discountPercentage,
|
double discountPercentage,
|
||||||
double totalAmount,
|
double totalAmount,
|
||||||
@@ -87,7 +87,6 @@ public:
|
|||||||
void setPaymentDate(const util::Timestamp& paymentDate);
|
void setPaymentDate(const util::Timestamp& paymentDate);
|
||||||
void setPaymentMethod(util::PaymentMode paymentMethod);
|
void setPaymentMethod(util::PaymentMode paymentMethod);
|
||||||
void setStatus(util::PaymentStatus status);
|
void setStatus(util::PaymentStatus status);
|
||||||
std::string serialize() const;
|
SerializedInvoice serialize() const;
|
||||||
static Invoice* deserialize(const std::string&);
|
static Invoice* deserialize(const SerializedInvoice&);
|
||||||
static std::string getHeaders();
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user