Added CSV header support and persistence for employees
<SRS> SRS02 : Employee Management </SRS> <Changes> - Added header handling in FileManager load by skipping first line during deserialization - Added support for writing headers using T::getHeaders() in FileManager save - Implemented getHeaders() for Employee and GeneralEmployee models - Added saveEmployees functionality in EmployeeManagementService - Added persistStates method in ZenvyController - Added deserialization failure check with exception handling - Minor formatting cleanup in FileIO </Changes> <Review> Smitha Mohan </Review>
This commit is contained in:
@@ -22,7 +22,6 @@ void FileIO::writeAllLines(const std::string& path,
|
||||
std::ofstream file(path, std::ios::trunc);
|
||||
if (!file.is_open())
|
||||
throw std::runtime_error("Failed to open file " + path);
|
||||
|
||||
for (const auto& line : lines)
|
||||
file << line << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <stdexcept>
|
||||
#include "FileIO.h"
|
||||
|
||||
template <typename T> using objects = std::map<std::string, std::shared_ptr<T>>;
|
||||
@@ -20,9 +21,19 @@ objects<T> FileManager<T>::load()
|
||||
{
|
||||
objects<T> records;
|
||||
auto lines = FileIO::readAllLines(m_filePath);
|
||||
bool isHeader = true;
|
||||
for (const auto& record : lines)
|
||||
{
|
||||
if (isHeader)
|
||||
{
|
||||
isHeader = false;
|
||||
continue;
|
||||
}
|
||||
auto object = T::deserialize(record);
|
||||
if (!object)
|
||||
{
|
||||
throw std::runtime_error("Failed to deserialize record");
|
||||
}
|
||||
records[object->getId()] = object;
|
||||
}
|
||||
return records;
|
||||
@@ -32,7 +43,7 @@ template <typename T>
|
||||
void FileManager<T>::save(const objects<T>& records)
|
||||
{
|
||||
std::vector<std::string> lines;
|
||||
|
||||
lines.push_back(T::getHeaders());
|
||||
for (const auto& recordPair : records)
|
||||
{
|
||||
lines.push_back(recordPair.second->serialize());
|
||||
|
||||
Reference in New Issue
Block a user