From c07d2079a223401e5b7b2c56f8cd44453761c088 Mon Sep 17 00:00:00 2001 From: sassanix <39465071+sassanix@users.noreply.github.com> Date: Sun, 2 Mar 2025 12:08:07 -0400 Subject: [PATCH] Fixing database Fixed API Connection Issue Changed the Nginx proxy configuration from proxy_pass http://localhost:5000; to proxy_pass http://127.0.0.1:5000; in the container's configuration This resolved the "ERR_CONNECTION_REFUSED" error by ensuring proper communication between Nginx and the Flask backend within the Docker container Fixed Database Serialization Error Updated the Python imports in app.py to properly include the date class: from datetime import datetime, timedelta, date Modified the type checking code to correctly identify date objects: isinstance(value, (datetime, date)) This resolved the "isinstance() arg 2 must be a type or tuple of types" error that was preventing the application from retrieving warranty data These changes resolved the connection issues between frontend and backend components, allowing the application to successfully add and display warranty information. --- Dockerfile | 2 +- backend/app.py | 4 ++-- backend/init.sql | 20 ++++++++------------ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 49c1745..780b6a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,7 +53,7 @@ RUN echo 'server {\n\ \n\ # Proxy API requests to backend\n\ location /api/ {\n\ - proxy_pass http://localhost:5000;\n\ + proxy_pass http://127.0.0.1:5000;\n\ proxy_set_header Host $host;\n\ proxy_set_header X-Real-IP $remote_addr;\n\ }\n\ diff --git a/backend/app.py b/backend/app.py index 426d02c..ebd65cf 100644 --- a/backend/app.py +++ b/backend/app.py @@ -2,7 +2,7 @@ from flask import Flask, request, jsonify, send_from_directory import psycopg2 from psycopg2 import pool import os -from datetime import datetime, timedelta +from datetime import datetime, timedelta, date from werkzeug.utils import secure_filename from flask_cors import CORS import logging @@ -98,7 +98,7 @@ def get_warranties(): warranty_dict = dict(zip(columns, row)) # Convert date objects to ISO format strings for JSON serialization for key, value in warranty_dict.items(): - if isinstance(value, datetime.date): + if isinstance(value, (datetime, date)): warranty_dict[key] = value.isoformat() warranties_list.append(warranty_dict) diff --git a/backend/init.sql b/backend/init.sql index 0ddd2f4..1a7997a 100644 --- a/backend/init.sql +++ b/backend/init.sql @@ -2,17 +2,13 @@ CREATE TABLE warranties ( id SERIAL PRIMARY KEY, - item_name VARCHAR(255) NOT NULL, - serial_number VARCHAR(255), - purchase_date DATE, - expiration_date DATE NOT NULL, - warranty_provider VARCHAR(255), - invoice_image VARCHAR(255), -- Filename or path to the uploaded invoice image - notes TEXT, -- Any additional notes about the warranty - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP + product_name VARCHAR(255) NOT NULL, + purchase_date DATE NOT NULL, + warranty_years INTEGER NOT NULL, + expiration_date DATE, + invoice_path TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); --- Optionally add indexes for faster querying (if needed) --- CREATE INDEX idx_expiration_date ON warranties (expiration_date); --- CREATE INDEX idx_item_name ON warranties (item_name); \ No newline at end of file +CREATE INDEX idx_expiration_date ON warranties(expiration_date); +CREATE INDEX idx_product_name ON warranties(product_name); \ No newline at end of file