Files
api/services/nginx/app/objects/customer_notes_o.php
T
Jepp9350 c240bbd94f Refactor database object trait and improve data handling
Added `delete`, `restore`, and other utility methods to enhance CRUD operations, including support for soft deletes. Introduced `objectChanged` hooks across objects for better cache or event handling, ensuring scalability and maintainability. Refactored and standardized object property handling while restructuring related methods.
2025-02-12 09:23:35 +01:00

128 lines
3.9 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use traits\db_object_t;
class customer_notes_o extends db
{
use db_object_t;
public object_property $customer_id;
public object_property $note;
public object_property $cashier_id;
public object_property $deleted_at;
public function structure(): void
{
$this->setTable('customer_notes');
}
public function objectChanged(): void
{
// Since the customer_notes object is not cached, there is no need to invalidate the cache
}
public function add(int $customer_id, string $note, int $cashier_id): customer_notes_o
{
global $db, $response;
try {
// Avoid SQL injection
$note = $db->escape_string($note);
// Create a new record in the database
$sql = "INSERT INTO $this->table (customer_id, note, cashier_id) VALUES ($customer_id, '$note', $cashier_id)";
$db->query($sql);
// Get the id of the new record
$this->id = $db->insert_id();
// Set the values of the object properties
$this->getObjectProperties();
// Clear the cache
redis->clear_customer_notes($customer_id);
} catch (\Exception $e) {
$response->error($e->getMessage());
}
return $this;
}
public function getObjectProperties(): void
{
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
$this->note = new object_property($this->table, $this->id, 'note', 'string', true);
$this->cashier_id = new object_property($this->table, $this->id, 'cashier_id', 'int', true);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
}
public function getCustomerNotesAsArray(int $user_id): array
{
global $response, $db;
// Check if the result is cached
$cached = redis->get_customer_notes($user_id);
if ($cached) {
$response->add_meta('cached', true);
return $cached;
}
$sql = "SELECT * FROM $this->table WHERE customer_id = $user_id AND deleted_at IS NULL";
$result = $db->query($sql);
$customer_notes = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$customer_notes[] = $row;
}
}
// Cache the result
redis->cache_customer_notes($user_id, $customer_notes);
return $customer_notes;
}
public function getCustomerNoteById(int $id): customer_notes_o
{
global $db;
// Get the record from the database
$sql = "SELECT * FROM $this->table WHERE id = $id";
$result = $db->query($sql);
if ($result->num_rows > 0) {
$this->id = $id;
$this->getObjectProperties();
}
return $this;
}
public function delete(int $id): void
{
global $db;
$this->id = $id;
$sql = "UPDATE $this->table SET deleted_at = NOW() WHERE id = $this->id";
$db->query($sql);
// Clear the cache
$customer_id = $this->getCustomerByNoteId($this->id);
redis->clear_customer_notes($customer_id);
}
public function getCustomerByNoteId(int $id): int
{
global $db;
$sql = "SELECT customer_id FROM $this->table WHERE id = $id";
$result = $db->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row['customer_id'];
}
return 0;
}
public function restore(int $id): void
{
global $db;
$this->id = $id;
$sql = "UPDATE $this->table SET deleted_at = NULL WHERE id = $this->id";
$db->query($sql);
// Clear the cache
$customer_id = $this->getCustomerByNoteId($this->id);
redis->clear_customer_notes($customer_id);
}
}