From 2c534a21ca5fa3249960ecd5513f05b9e2f29728 Mon Sep 17 00:00:00 2001 From: s490122 Date: Fri, 21 Jun 2024 20:28:31 +0200 Subject: [PATCH] Upload files to "/" --- TaskManager.java | 31 +++++++++++++++ app.js | 83 +++++++++++++++++++++++++++++++++++++++ index.html | 20 ++++++++++ serwer.py | 89 +++++++++++++++++++++++++++++++++++++++++ styles.css | 100 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 323 insertions(+) create mode 100644 TaskManager.java create mode 100644 app.js create mode 100644 index.html create mode 100644 serwer.py create mode 100644 styles.css diff --git a/TaskManager.java b/TaskManager.java new file mode 100644 index 0000000..ef31c27 --- /dev/null +++ b/TaskManager.java @@ -0,0 +1,31 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class TaskManager { + public static void main(String[] args) { + try { + URL url = new URL("http://localhost:8000/get_tasks"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + + int status = con.getResponseCode(); + if (status == 200) { + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer content = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + con.disconnect(); + System.out.println("Tasks: " + content.toString()); + } else { + System.out.println("GET request failed with status: " + status); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/app.js b/app.js new file mode 100644 index 0000000..d1bdd23 --- /dev/null +++ b/app.js @@ -0,0 +1,83 @@ +// Funkcja do dodawania nowego zadania +async function addTask() { + const taskDescription = document.getElementById('new-task').value; + + if (!taskDescription) { + alert('Opis zadania nie może być pusty!'); + return; + } + + try { + const response = await fetch('http://localhost:8000/add_task', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ task_description: taskDescription }) + }); + + if (!response.ok) { + throw new Error('Odpowiedź sieci nie była OK'); + } + + const result = await response.json(); + console.log('Zadanie dodane:', result); + document.getElementById('new-task').value = ''; + loadTasks(); // Ponowne załadowanie zadań po dodaniu nowego + } catch (error) { + console.error('Błąd podczas dodawania zadania:', error); + } +} + +// Funkcja do ładowania zadań +async function loadTasks() { + try { + const response = await fetch('http://localhost:8000/get_tasks'); + + if (!response.ok) { + throw new Error('Odpowiedź sieci nie była OK'); + } + + const tasks = await response.json(); + const taskList = document.getElementById('task-list'); + taskList.innerHTML = ''; + tasks.forEach(task => { + const listItem = document.createElement('li'); + listItem.textContent = task.task_description; + + // Przycisk do usuwania zadania + const deleteButton = document.createElement('button'); + deleteButton.textContent = 'Usuń'; + deleteButton.onclick = () => deleteTask(task.id); + + listItem.appendChild(deleteButton); + taskList.appendChild(listItem); + }); + } catch (error) { + console.error('Błąd podczas ładowania zadań:', error); + } +} + +// Funkcja do usuwania zadania +async function deleteTask(taskId) { + try { + const response = await fetch(`http://localhost:8000/delete_task/${taskId}`, { + method: 'DELETE' + }); + + if (!response.ok) { + throw new Error('Odpowiedź sieci nie była OK'); + } + + const result = await response.json(); + console.log('Zadanie usunięte:', result); + loadTasks(); // Ponowne załadowanie zadań po usunięciu + } catch (error) { + console.error('Błąd podczas usuwania zadania:', error); + } +} + +// Nasłuchiwanie na załadowanie DOM +document.addEventListener('DOMContentLoaded', () => { + loadTasks(); +}); diff --git a/index.html b/index.html new file mode 100644 index 0000000..0f8ee31 --- /dev/null +++ b/index.html @@ -0,0 +1,20 @@ + + + + + + To-Do App + + + +
+

To-Do List

+
+ + +
+ +
+ + + diff --git a/serwer.py b/serwer.py new file mode 100644 index 0000000..80a57a8 --- /dev/null +++ b/serwer.py @@ -0,0 +1,89 @@ +from wsgiref.simple_server import make_server +import json +import pyodbc + +# Konfiguracja połączenia z bazą danych +conn = pyodbc.connect( + 'DRIVER={ODBC Driver 17 for SQL Server};' + 'SERVER=mssql-2017.labs.wmi.amu.edu.pl;' + 'DATABASE=dbad_s490127;' + 'UID=dbad_s490127;' + 'PWD=Bjf5w2Hhco' +) +cursor = conn.cursor() + +def application(environ, start_response): + path = environ.get('PATH_INFO', '') # Pobranie ścieżki żądania + method = environ.get('REQUEST_METHOD', '') # Pobranie metody HTTP + + response_headers = [ + ('Content-Type', 'application/json'), + ('Access-Control-Allow-Origin', '*'), + ('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE'), + ('Access-Control-Allow-Headers', 'Content-Type') + ] + + # Obsługa preflight requests dla CORS + if method == 'OPTIONS': + start_response('200 OK', response_headers) + return [b''] + + # Obsługa dodawania nowego zadania + if path == '/add_task' and method == 'POST': + try: + # Odczytanie i sparsowanie treści żądania + request_body_size = int(environ.get('CONTENT_LENGTH', 0)) + request_body = environ['wsgi.input'].read(request_body_size) + data = json.loads(request_body) + task_description = data.get('task_description', '') + + # Dodanie zadania do bazy danych, jeśli opis jest podany + if task_description: + cursor.execute("INSERT INTO Tasks (task_description) OUTPUT INSERTED.id VALUES (?)", (task_description,)) + task_id = cursor.fetchone()[0] # Pobranie ID nowo dodanego zadania + conn.commit() # Zatwierdzenie zmian w bazie danych + response = json.dumps({'message': 'Task added successfully', 'id': task_id}) + status = '200 OK' + else: + response = json.dumps({'message': 'Task description is required'}) # Brak opisu zadania + status = '400 Bad Request' + except Exception as e: + response = json.dumps({'message': str(e)}) # Obsługa błędu + status = '500 Internal Server Error' + + # Obsługa pobierania wszystkich zadań + elif path == '/get_tasks' and method == 'GET': + try: + cursor.execute("SELECT id, task_description FROM Tasks") # Pobranie zadań z bazy danych + tasks = cursor.fetchall() + tasks_list = [{'id': task[0], 'task_description': task[1]} for task in tasks] # Przygotowanie listy zadań + response = json.dumps(tasks_list) # Konwersja listy zadań do formatu JSON + status = '200 OK' + except Exception as e: + response = json.dumps({'message': str(e)}) # Obsługa błędu + status = '500 Internal Server Error' + + # Obsługa usuwania konkretnego zadania + elif path.startswith('/delete_task/') and method == 'DELETE': + try: + task_id = int(path.split('/')[-1]) # Wyodrębnienie ID zadania z URL + cursor.execute("DELETE FROM Tasks WHERE id = ?", (task_id,)) # Usunięcie zadania z bazy danych + conn.commit() # Zatwierdzenie zmian w bazie danych + response = json.dumps({'message': 'Task deleted successfully'}) + status = '200 OK' + except Exception as e: + response = json.dumps({'message': str(e)}) # Obsługa błędu + status = '500 Internal Server Error' + + # Obsługa nieznanych ścieżek + else: + response = json.dumps({'message': 'Not Found'}) # Nieznana ścieżka + status = '404 Not Found' + + start_response(status, response_headers) # Wysłanie odpowiedzi HTTP + return [response.encode('utf-8')] + +if __name__ == "__main__": + httpd = make_server('', 8000, application) # Utworzenie serwera na porcie 8000 + print("Serving on port 8000...") + httpd.serve_forever() # Rozpoczęcie obsługi żądań diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..577efa9 --- /dev/null +++ b/styles.css @@ -0,0 +1,100 @@ +/* Reset some default browser styles */ +body, h1, ul, li, button, input { + margin: 0; + padding: 0; + box-sizing: border-box; + font-family: Arial, sans-serif; +} + +body { + background-color: #f4f4f9; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; +} + +.container { + background: #fff; + padding: 20px; + border-radius: 10px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + width: 300px; +} + +h1 { + font-size: 24px; + margin-bottom: 20px; + text-align: center; + color: #333; +} + +.input-container { + display: flex; + justify-content: space-between; + margin-bottom: 20px; +} + +input[type="text"] { + flex: 1; + padding: 10px; + font-size: 16px; + border: 1px solid #ddd; + border-radius: 5px; + margin-right: 10px; +} + +button { + padding: 10px; + font-size: 16px; + border: none; + border-radius: 5px; + background-color: #007bff; + color: #fff; + cursor: pointer; + transition: background-color 0.3s; +} + +button:hover { + background-color: #0056b3; +} + +ul { + list-style: none; +} + +li { + background: #f9f9f9; + padding: 10px; + border: 1px solid #ddd; + border-radius: 5px; + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 10px; +} + +li.completed { + text-decoration: line-through; + color: #999; +} + +li button { + margin-left: 10px; +} + +li button:first-of-type { + background-color: #28a745; +} + +li button:first-of-type:hover { + background-color: #218838; +} + +li button:last-of-type { + background-color: #dc3545; +} + +li button:last-of-type:hover { + background-color: #c82333; +}