claude-3-5-sonnet-20241022· Anthropic LMArena Human Preference 140K
Utwórz aplikację webową w PHP i javascript, która pozwoli na dodawanie, edycję, usuwanie…
- User
Utwórz aplikację webową w PHP i javascript, która pozwoli na dodawanie, edycję, usuwanie i wyszukiwanie notatek, tekst może być formatowany za pomocą markdown. Dane mają być zapisywanie w bazie sqlite. Edycja i dodawanie notatek powinna się odbywać w oknie modalnym. Na końcu utwórz skrypt bash, który utworzy na serwerze wszystkie pliki tej aplikacji.
- Assistant
Stworzę kompletną aplikację do zarządzania notatkami. Rozbiję to na kilka plików i na końcu dodam skrypt bash.
- Struktura bazy danych (schema.sql):
CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );- Konfiguracja (config.php):
<?php define('DB_PATH', 'notes.db'); function getDB() { try { $db = new PDO('sqlite:' . DB_PATH); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $db; } catch(PDOException $e) { die("Connection failed: " . $e->getMessage()); } }- API do obsługi notatek (api.php):
<?php require_once 'config.php'; header('Content-Type: application/json'); $db = getDB(); switch($_SERVER['REQUEST_METHOD']) { case 'GET': if (isset($_GET['search'])) { $stmt = $db->prepare("SELECT * FROM notes WHERE title LIKE :search OR content LIKE :search"); $searchTerm = "%" . $_GET['search'] . "%"; $stmt->bindParam(':search', $searchTerm); } else { $stmt = $db->query("SELECT * FROM notes ORDER BY created_at DESC"); } echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); break; case 'POST': $data = json_decode(file_get_contents('php://input'), true); $stmt = $db->prepare("INSERT INTO notes (title, content) VALUES (:title, :content)"); $stmt->execute([ ':title' => $data['title'], ':content' => $data['content'] ]); echo json_encode(['id' => $db->lastInsertId()]); break; case 'PUT': $data = json_decode(file_get_contents('php://input'), true); $stmt = $db->prepare("UPDATE notes SET title = :title, content = :content, updated_at = CURRENT_TIMESTAMP WHERE id = :id"); $stmt->execute([ ':id' => $data['id'], ':title' => $data['title'], ':content' => $data['content'] ]); echo json_encode(['success' => true]); break; case 'DELETE': $id = $_GET['id']; $stmt = $db->prepare("DELETE FROM notes WHERE id = :id"); $stmt->execute([':id' => $id]); echo json_encode(['success' => true]); break; }- Strona główna (index.php):
<!DOCTYPE html> <html> <head> <title>Notes App</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/easymde/dist/easymde.min.css" rel="stylesheet"> </head> <body> <div class="container mt-4"> <h1>Notes</h1> <div class="row mb-3"> <div class="col"> <input type="text" id="search" class="form-control" placeholder="Search notes..."> </div> <div class="col-auto"> <button class="btn btn-primary" onclick="showNoteModal()">Add Note</button> </div> </div> <div id="notes-list" class="row"></div> </div> <!-- Modal --> <div class="modal fade" id="noteModal" tabindex="-1"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Note</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <input type="hidden" id="note-id"> <div class="mb-3"> <input type="text" class="form-control" id="note-title" placeholder="Title"> </div> <div class="mb-3"> <textarea id="note-content"></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary" onclick="saveNote()">Save</button> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/easymde/dist/easymde.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> <script src="script.js"></script> </body> </html>- JavaScript (script.js):
let editor; let noteModal; document.addEventListener('DOMContentLoaded', function() { editor = new EasyMDE({ element: document.getElementById('note-content'), spellChecker: false }); noteModal = new bootstrap.Modal(document.getElementById('noteModal')); loadNotes(); document.getElementById('search').addEventListener('input', debounce(loadNotes, 300)); }); function debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } async function loadNotes() { const searchTerm = document.getElementById('search').value; const response = await fetch(`api.php${searchTerm ? `?search=${encodeURIComponent(searchTerm)}` : ''}`); const notes = await response.json(); const notesList = document.getElementById('notes-list'); notesList.innerHTML = ''; notes.forEach(note => { const col = document.createElement('div'); col.className = 'col-md-4 mb-3'; col.innerHTML = ` <div class="card"> <div class="card-body"> <h5 class="card-title">${note.title}</h5> <div class="card-text">${marked.parse(note.content.substring(0, 100))}...</div> <button class="btn btn-sm btn-primary" onclick="editNote(${note.id})">Edit</button> <button class="btn btn-sm btn-danger" onclick="deleteNote(${note.id})">Delete</button> </div> </div> `; notesList.appendChild(col); }); } function showNoteModal(note = null) { document.getElementById('note-id').value = note ? note.id : ''; document.getElementById('note-title').value = note ? note.title : ''; editor.value(note ? note.content : ''); noteModal.show(); } async function editNote(id) { const response = await fetch(`api.php?id=${id}`); const notes = await response.json(); const note = notes.find(n => n.id === id); if (note) { showNoteModal(note); } } async function saveNote() { const id = document.getElementById('note-id').value; const title = document.getElementById('note-title').value; const content = editor.value(); const method = id ? 'PUT' : 'POST'; const data = { title, content }; if (id) data.id = id; await fetch('api.php', { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); noteModal.hide(); loadNotes(); } async function deleteNote(id) { if (confirm('Are you sure you want to delete this note?')) { await fetch(`api.php?id=${id}`, { method: 'DELETE' }); loadNotes(); } }- Skrypt instalacyjny (install.sh):
#!/bin/bash # Tworzenie struktury katalogów mkdir -p notes_app # Tworzenie plików cat > notes_app/config.php << 'EOF' <?php define('DB_PATH', 'notes.db'); function getDB() { try { $db = new PDO('sqlite:' . DB_PATH); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $db; } catch(PDOException $e) { die("Connection failed: " . $e->getMessage()); } } EOF # Kopiowanie pozostałych plików cp schema.sql notes_app/ cp api.php notes_app/ cp index.php notes_app/ cp script.js notes_app/ # Ustawianie uprawnień chmod 755 notes_app chmod 644 notes_app/* # Inicjalizacja bazy danych cd notes_app sqlite3 notes.db < schema.sql chmod 666 notes.db echo "Instalacja zakończona. Aplikacja znajduje się w katalogu notes_app/"Aby zainstalować aplikację:
- Zapisz wszystkie powyższe pliki w tym samym katalogu
- Nadaj uprawnienia wykonywania dla skryptu install.sh:
chmod +x install.sh- Uruchom skrypt:
./install.shAplikacja oferuje następujące funkcjonalności:
- Dodawanie nowych notatek
- Edycja istniejących notatek
- Usuwanie notatek
- Wyszukiwanie notatek po tytule i treści
- Formatowanie tekstu za pomocą Markdown
- Podgląd sformatowanego tekstu
- Responsywny interfejs użytkownika
Wymagania systemowe:
- PHP 7.0 lub nowszy
- SQLite3
- Serwer WWW (np. Apache, Nginx)
- Przeglądarka internetowa z włączonym JavaScript
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.