diff --git a/main.py b/main.py index 98c73cd..1372a24 100644 --- a/main.py +++ b/main.py @@ -40,8 +40,8 @@ def get_connection(): dbname="parsed_url", user="postgres", password="qwertyqwerty123123", - # host="127.0.0.1" - host ="45.129.78.228" + host="127.0.0.1" + # host ="45.129.78.228" ) @app.post("/save_parsed_data", summary="Сохранить данные парсинга") @@ -76,7 +76,7 @@ def save_parsed_data(data: ParsedData): conn.close() -@app.post("/update_viewed_status", summary="Сохранить данные парсинга") +@app.post("/update_viewed_status", summary="Обновляет поле viewed") def update_viewed_status(url: str, viewed: bool): """ Обновляет поле 'viewed' записи в БД по заданному URL. @@ -107,6 +107,36 @@ def update_viewed_status(url: str, viewed: bool): return {"status": "success", "message": "Статус просмотра успешно обновлен"} +@app.post("/update_status_status", summary="Обновляет поле status") +def update_status_status(url: str, status: bool): + """ + Обновляет поле 'status' записи в БД по заданному URL. + """ + conn = None + try: + conn = get_connection() + with conn.cursor() as cursor: + cursor.execute( + """ + UPDATE url + SET status = %s + WHERE url = %s + """, + (status, url) + ) + if cursor.rowcount == 0: + # Если запись не найдена + raise HTTPException(status_code=404, detail="Запись с указанным URL не найдена") + conn.commit() + except Exception as e: + if conn: + conn.rollback() + raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}") + finally: + if conn: + conn.close() + + return {"status": "success", "message": "Статус просмотра успешно обновлен"} @app.get("/check_url_exists") @@ -185,6 +215,6 @@ def get_records(): # get_records() # Запуск сервера для теста -if __name__ == "__main__": - uvicorn.run("main:app", port=8002, reload=True) +# if __name__ == "__main__": +# uvicorn.run("main:app", port=8002, reload=True)