commit afc68149858921b838a83e1e3b7c423b7fd7f64b Author: s490129 Date: Thu Jun 13 22:37:36 2024 +0200 Upload files to "/" diff --git a/SQLiteToProlog.class b/SQLiteToProlog.class new file mode 100644 index 0000000..6e98dab Binary files /dev/null and b/SQLiteToProlog.class differ diff --git a/prolog.pl b/prolog.pl new file mode 100644 index 0000000..11638e1 --- /dev/null +++ b/prolog.pl @@ -0,0 +1,12 @@ +has_duplicates(List) :- + member(X, List), + select(X, List, Rest), + member(X, Rest), !. +collect_todos(Ys) :- + findall(Y, todo(_, Y), Ys). +todos_have_duplicates :- + collect_todos(Ys), + has_duplicates(Ys). +todo(24, 'koko'). +todo(25, 'xasda'). +todo(26, 'dsadsa'). diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..7a776d6 --- /dev/null +++ b/readme.txt @@ -0,0 +1,8 @@ +sudo apt update +sudo apt install nodejs npm +cd ~/projParadyg +npm install express +node server.js + +javac -cp ".:lib/sqlite-jdbc-3.45.3.0.jar:lib/slf4j-api-2.1.0-alpha1.jar" -encoding UTF-8 SQLiteToProlog.java +java -cp ".:lib/sqlite-jdbc-3.45.3.0.jar:lib/slf4j-api-2.1.0-alpha1.jar" SQLiteToProlog diff --git a/script.js b/script.js new file mode 100644 index 0000000..c081052 --- /dev/null +++ b/script.js @@ -0,0 +1,70 @@ +$(document).ready(function(){ + const apiUrl = 'http://localhost:3000'; + + // funkcja która dodaje zadania do listy + const appendTaskToList = (task) => { + $("#todoList").append("
  • " + task.task + "
  • "); + }; + + // funkcja która zwraca błąd gdy nie można dodać zadania + const handleAjaxError = (message) => () => alert(message); + + // pobieranie zdań + const fetchTasks = () => { + $.ajax({ + url: `${apiUrl}/tasks`, + method: 'GET', + success: function(tasks) { + tasks.forEach(appendTaskToList); + }, + error: handleAjaxError("Error fetching tasks") + }); + }; + + // funkcja która dodaje zadanie + const addTask = (task) => { + if (task !== "") { + $.ajax({ + url: `${apiUrl}/addTask`, + method: 'POST', + contentType: 'application/json', + data: JSON.stringify({ task: task }), + success: function(response) { + appendTaskToList(response); + $("#todoInput").val(""); + }, + error: handleAjaxError("Error adding task") + }); + } else { + alert("Enter task"); + } + }; + + // funkcja która usuwa zadanie + const deleteTask = (taskId, listItem) => { + $.ajax({ + url: `${apiUrl}/deleteTask/${taskId}`, + method: 'DELETE', + success: function(response) { + listItem.remove(); + }, + error: handleAjaxError("Error deleting task") + }); + }; + + // pobieranie i wyświetlanie zadań + fetchTasks(); + + // dodawanie nowego zadania + $("#addBtn").click(function(){ + const task = $("#todoInput").val(); + addTask(task); + }); + + // usuwanie zadania + $(document).on("click", ".rmBtn", function(){ + const listItem = $(this).parent(); + const taskId = listItem.data('id'); + deleteTask(taskId, listItem); + }); +}); diff --git a/server.js b/server.js new file mode 100644 index 0000000..cbfced9 --- /dev/null +++ b/server.js @@ -0,0 +1,65 @@ +const express = require('express'); +const bodyParser = require('body-parser'); +const sqlite3 = require('sqlite3').verbose(); +const app = express(); +const port = 3000; + +// Ustawienia bodyParser do obsługi danych w formacie JSON +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +// Połączenie z bazą danych SQLite +const db = new sqlite3.Database('tasks.db'); + +// tworzenie tabeli tasks, jeśli jeszcze nie istnieje +db.serialize(() => { + db.run("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT)"); +}); + +// Obsługa CORS, czyli ograniczenie żądań stron internetowych z innej domeny +app.use((req, res, next) => { + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); + res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS'); + next(); +}); + +// Dodawanie zadania +app.post('/addTask', (req, res) => { + const task = req.body.task; + if (task) { + db.run("INSERT INTO tasks (task) VALUES (?)", task, function(err) { + if (err) { + return res.status(500).json({ error: err.message }); + } + res.json({ id: this.lastID, task: task }); + }); + } else { + res.status(400).json({ error: "No task provided" }); + } +}); + +// Usuwanie zadania +app.delete('/deleteTask/:id', (req, res) => { + const id = req.params.id; + db.run("DELETE FROM tasks WHERE id = ?", id, function(err) { + if (err) { + return res.status(500).json({ error: err.message }); + } + res.json({ success: true }); + }); +}); + +// Pobieranie wszystkich zadań +app.get('/tasks', (req, res) => { + db.all("SELECT * FROM tasks", [], (err, rows) => { + if (err) { + return res.status(500).json({ error: err.message }); + } + res.json(rows); + }); +}); + +app.listen(port, () => { + console.log(`Server running at http://localhost:${port}/`); +});