first commit
This commit is contained in:
92
models/Notes.php
Normal file
92
models/Notes.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
class Notes
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Database::getInstance()->getConnection();
|
||||
$this->initTable();
|
||||
}
|
||||
|
||||
private function initTable()
|
||||
{
|
||||
// Tworzenie tabeli notatek jeśli nie istnieje
|
||||
$sql = "CREATE TABLE IF NOT EXISTS notes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
)";
|
||||
|
||||
try {
|
||||
$this->db->exec($sql);
|
||||
} catch (PDOException $e) {
|
||||
// Tabela już istnieje
|
||||
}
|
||||
}
|
||||
|
||||
public function getAllByUser($userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM notes
|
||||
WHERE user_id = ?
|
||||
ORDER BY updated_at DESC
|
||||
");
|
||||
$stmt->execute([$userId]);
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function getById($id, $userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM notes
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
$stmt->execute([$id, $userId]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function create($userId, $title, $content)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
INSERT INTO notes (user_id, title, content)
|
||||
VALUES (?, ?, ?)
|
||||
");
|
||||
return $stmt->execute([$userId, $title, $content]);
|
||||
}
|
||||
|
||||
public function update($id, $userId, $title, $content)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
UPDATE notes
|
||||
SET title = ?, content = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([$title, $content, $id, $userId]);
|
||||
}
|
||||
|
||||
public function delete($id, $userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
DELETE FROM notes
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([$id, $userId]);
|
||||
}
|
||||
|
||||
public function getCount($userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT COUNT(*) as count FROM notes
|
||||
WHERE user_id = ?
|
||||
");
|
||||
$stmt->execute([$userId]);
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return $result['count'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user