Auto stash before merge of "main" and "origin/main"
This commit is contained in:
69
src/Settings/Settings.php
Normal file
69
src/Settings/Settings.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Settings;
|
||||
|
||||
use Hpz937\BillReminder\Database\DatabaseInterface;
|
||||
use Exception;
|
||||
|
||||
class Settings
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct(DatabaseInterface $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function saveSettings($userId, $notificationPeriod)
|
||||
{
|
||||
// Check if settings already exist for the user
|
||||
if ($this->settingsExist($userId)) {
|
||||
// Update existing settings
|
||||
$sql = "UPDATE settings SET notification_period = :notification_period WHERE user_id = :user_id";
|
||||
} else {
|
||||
// Insert new settings
|
||||
$sql = "INSERT INTO settings (user_id, notification_period) VALUES (:user_id, :notification_period)";
|
||||
}
|
||||
|
||||
$params = [
|
||||
':user_id' => $userId,
|
||||
':notification_period' => $notificationPeriod,
|
||||
];
|
||||
|
||||
try {
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getSettings($userId)
|
||||
{
|
||||
$sql = "SELECT * FROM settings WHERE user_id = :user_id";
|
||||
$params = [':user_id' => $userId];
|
||||
|
||||
try {
|
||||
$settings = $this->db->query($sql, $params);
|
||||
return $settings ? $settings[0] : null;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function settingsExist($userId)
|
||||
{
|
||||
$sql = "SELECT id FROM settings WHERE user_id = :user_id";
|
||||
$params = [':user_id' => $userId];
|
||||
|
||||
try {
|
||||
$result = $this->db->query($sql, $params);
|
||||
return !empty($result);
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user