95 lines
2.2 KiB
C++
95 lines
2.2 KiB
C++
/*
|
|
File: ServiceManagementService.cpp
|
|
Description: Implements the ServiceManagementService class which manages service-related operations
|
|
in the Vehicle Service Management System. Provides functionality for attaching/detaching observers
|
|
and sending notifications to users regarding service events.
|
|
Author: Trenser
|
|
Date: 20-May-2026
|
|
*/
|
|
|
|
#include <stdexcept>
|
|
#include "ServiceManagementService.h"
|
|
#include "User.h"
|
|
#include "Factory.h"
|
|
#include "Timestamp.h"
|
|
|
|
util::Map<std::string, User*> ServiceManagementService::m_observers{};
|
|
|
|
/*
|
|
Function: attach
|
|
Description: Attaches a user as an observer to the ServiceManagementService for receiving notifications.
|
|
Parameters:
|
|
- user: Pointer to the User object to be attached.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void ServiceManagementService::attach(User* user)
|
|
{
|
|
if (user)
|
|
{
|
|
const std::string& userID = user->getId();
|
|
if (m_observers.find(userID) == -1)
|
|
{
|
|
m_observers[userID] = user;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: detach
|
|
Description: Detaches a user from the observer list of the ServiceManagementService.
|
|
Parameters:
|
|
- user: Pointer to the User object to be detached.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void ServiceManagementService::detach(User* user)
|
|
{
|
|
if (user)
|
|
{
|
|
const std::string& userID = user->getId();
|
|
if (m_observers.find(userID) != -1)
|
|
{
|
|
m_observers.remove(userID);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: sendNotification
|
|
Description: Sends a notification to a user if they are registered as an observer.
|
|
Parameters:
|
|
- user: Pointer to the User object to receive the notification.
|
|
- title: Title of the notification.
|
|
- message: Message content of the notification.
|
|
Returns:
|
|
- void
|
|
Throws:
|
|
- std::runtime_error if notification creation fails.
|
|
*/
|
|
void ServiceManagementService::sendNotification(User* user, const std::string& title, const std::string& message)
|
|
{
|
|
if (user)
|
|
{
|
|
if (m_observers.find(user->getId()) != -1)
|
|
{
|
|
Notification* notification =
|
|
Factory::getObject<Notification>(
|
|
user->getId(),
|
|
user,
|
|
"ServiceManagementService: " + title,
|
|
message,
|
|
util::Timestamp()
|
|
);
|
|
if (notification)
|
|
{
|
|
user->addNotification(notification);
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Failed to create notification");
|
|
}
|
|
}
|
|
}
|
|
}
|