From cf9319a35c92e0df43b4087a6e4cc7ddea6340cc Mon Sep 17 00:00:00 2001 From: xzxjesse Date: Wed, 7 Aug 2024 18:02:57 -0300 Subject: [PATCH 1/7] =?UTF-8?q?cria=C3=A7=C3=A3o=20da=20estrutura=20inicia?= =?UTF-8?q?l=20>>=20Co-authored-by:=20gabrielhrlima=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/recordController.py | 19 +++++++++++++++++++ src/domain/recordSchema.py | 11 +++++++++++ src/model/recordModel.py | 10 ++++++++++ src/repository/recordRepository.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 src/controller/recordController.py create mode 100644 src/domain/recordSchema.py create mode 100644 src/model/recordModel.py create mode 100644 src/repository/recordRepository.py diff --git a/src/controller/recordController.py b/src/controller/recordController.py new file mode 100644 index 0000000..3da8ebf --- /dev/null +++ b/src/controller/recordController.py @@ -0,0 +1,19 @@ +from fastapi import APIRouter, HTTPException, Depends, Query +from sqlalchemy.orm import Session +from domain import recordSchema +from database import get_db +from repository import recordRepository +from starlette.responses import JSONResponse + +Record = APIRouter( + prefix="/record" +) + +@Record.post("/") +def add_to_record(record: recordSchema.RecordCreate, db: Session = Depends(get_db)): + return recordRepository.create_record(db=db, record=record) + +@Record.get("/{user_id}") +def check_record(record: recordSchema.RecordGet, db: Session = Depends(get_db)): + videos = recordRepository.get_record(db=db, record=record) + return {"videos": videos} diff --git a/src/domain/recordSchema.py b/src/domain/recordSchema.py new file mode 100644 index 0000000..1ca3bf4 --- /dev/null +++ b/src/domain/recordSchema.py @@ -0,0 +1,11 @@ +from pydantic import BaseModel + +class RecordBase(BaseModel): + user_id: str + videos: dict + +class RecordCreate(RecordBase): + pass + +class RecordGet(BaseModel): + user_id: str diff --git a/src/model/recordModel.py b/src/model/recordModel.py new file mode 100644 index 0000000..68881bb --- /dev/null +++ b/src/model/recordModel.py @@ -0,0 +1,10 @@ +import uuid +from sqlalchemy import Column, String, JSON +from database import Base + +class Record(Base): + __tablename__ = 'record' + id = Column(String, primary_key=True, index=True, default=lambda: str(uuid.uuid4())) + user_id = Column(String, index=True, nullable=False) + videos = Column(JSON, index=True, nullable=False) + \ No newline at end of file diff --git a/src/repository/recordRepository.py b/src/repository/recordRepository.py new file mode 100644 index 0000000..31408f2 --- /dev/null +++ b/src/repository/recordRepository.py @@ -0,0 +1,28 @@ +from sqlalchemy.orm import Session + +from domain import recordSchema +from model import recordModel +from fastapi import HTTPException + +def create_record(db: Session, record: recordSchema.RecordCreate): + db_record = recordModel.Record( + user_id=record.user_id.strip(), + videos=record.videos + ) + db.add(db_record) + db.commit() + db.refresh(db_record) + print(f"Created record: user_id={db_record.user_id}, videos={db_record.video_id}") + return db_record + +def get_record(db: Session, record: recordSchema.RecordGet): + user_id = record.user_id.strip() + record_entry = db.query(recordModel.Record).filter( + recordModel.Record.user_id == user_id + ).first() + print(f"Query Result: {record_entry}") + if record_entry: + print(f"Check Record Founds") + return record_entry.videos + print(f"Check Record Not Found") + return False From 3244060a0378b955bfa8daeb27064210ed494bdb Mon Sep 17 00:00:00 2001 From: gabrielhrlima Date: Wed, 7 Aug 2024 18:31:30 -0300 Subject: [PATCH 2/7] Correcoe na estrutura Co-authored-by: xzxjesse --- src/controller/recordController.py | 2 +- src/repository/savedVideosRepository.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controller/recordController.py b/src/controller/recordController.py index 3da8ebf..8d203f1 100644 --- a/src/controller/recordController.py +++ b/src/controller/recordController.py @@ -13,7 +13,7 @@ def add_to_record(record: recordSchema.RecordCreate, db: Session = Depends(get_db)): return recordRepository.create_record(db=db, record=record) -@Record.get("/{user_id}") +@Record.get("/get_record") def check_record(record: recordSchema.RecordGet, db: Session = Depends(get_db)): videos = recordRepository.get_record(db=db, record=record) return {"videos": videos} diff --git a/src/repository/savedVideosRepository.py b/src/repository/savedVideosRepository.py index 1e63518..fd37561 100644 --- a/src/repository/savedVideosRepository.py +++ b/src/repository/savedVideosRepository.py @@ -1,4 +1,4 @@ -from sqlalchemy.orm import Session + from sqlalchemy.orm import Session from domain import savedVideosSchema from model import savedVideosModel From 535990a1ff30fca09feca8514f4d4f8a45cd49f5 Mon Sep 17 00:00:00 2001 From: gabrielhrlima Date: Wed, 7 Aug 2024 18:35:27 -0300 Subject: [PATCH 3/7] Correcoe na estrutura Co-authored-by: xzxjesse --- src/controller/recordController.py | 3 ++- src/main.py | 3 ++- src/model/recordModel.py | 2 +- src/repository/recordRepository.py | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/controller/recordController.py b/src/controller/recordController.py index 8d203f1..f76185b 100644 --- a/src/controller/recordController.py +++ b/src/controller/recordController.py @@ -14,6 +14,7 @@ def add_to_record(record: recordSchema.RecordCreate, db: Session = Depends(get_d return recordRepository.create_record(db=db, record=record) @Record.get("/get_record") -def check_record(record: recordSchema.RecordGet, db: Session = Depends(get_db)): +def check_record(user_id: str =Query(...) , db: Session = Depends(get_db)): + record = recordSchema.RecordGet(user_id =user_id) videos = recordRepository.get_record(db=db, record=record) return {"videos": videos} diff --git a/src/main.py b/src/main.py index 457634d..f5d560d 100755 --- a/src/main.py +++ b/src/main.py @@ -8,7 +8,7 @@ load_dotenv() -from controller import commentController, scheduleController, savedVideosController +from controller import commentController, scheduleController, savedVideosController, recordController from controller.savedVideosController import WatchLater @@ -38,6 +38,7 @@ #app.include_router(prefix="/api", router=commentController.comment) app.include_router(prefix="/api", router=scheduleController.schedule) app.include_router(prefix="/api", router=savedVideosController.favorite) +app.include_router(prefix="/api", router=recordController.Record) @app.get("/") async def root(): diff --git a/src/model/recordModel.py b/src/model/recordModel.py index 68881bb..0af72ae 100644 --- a/src/model/recordModel.py +++ b/src/model/recordModel.py @@ -6,5 +6,5 @@ class Record(Base): __tablename__ = 'record' id = Column(String, primary_key=True, index=True, default=lambda: str(uuid.uuid4())) user_id = Column(String, index=True, nullable=False) - videos = Column(JSON, index=True, nullable=False) + videos = Column(JSON, nullable=False) \ No newline at end of file diff --git a/src/repository/recordRepository.py b/src/repository/recordRepository.py index 31408f2..e59563f 100644 --- a/src/repository/recordRepository.py +++ b/src/repository/recordRepository.py @@ -12,7 +12,7 @@ def create_record(db: Session, record: recordSchema.RecordCreate): db.add(db_record) db.commit() db.refresh(db_record) - print(f"Created record: user_id={db_record.user_id}, videos={db_record.video_id}") + print(f"Created record: user_id={db_record.user_id}") return db_record def get_record(db: Session, record: recordSchema.RecordGet): From d9894c006ca3ff30e0f3db5e60e6cc3480cdece7 Mon Sep 17 00:00:00 2001 From: victorleaoo Date: Wed, 7 Aug 2024 18:38:29 -0300 Subject: [PATCH 4/7] Correcao de identacao do codigo --- src/repository/savedVideosRepository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repository/savedVideosRepository.py b/src/repository/savedVideosRepository.py index fd37561..1e63518 100644 --- a/src/repository/savedVideosRepository.py +++ b/src/repository/savedVideosRepository.py @@ -1,4 +1,4 @@ - from sqlalchemy.orm import Session +from sqlalchemy.orm import Session from domain import savedVideosSchema from model import savedVideosModel From 27dae8a2e9031d878e7cb75ce561609fabdf3549 Mon Sep 17 00:00:00 2001 From: gabrielhrlima Date: Thu, 8 Aug 2024 20:28:07 -0300 Subject: [PATCH 5/7] Modifica funcao para alterar o historico --- src/controller/recordController.py | 7 ++++++- src/repository/recordRepository.py | 25 +++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/controller/recordController.py b/src/controller/recordController.py index f76185b..ec0dea1 100644 --- a/src/controller/recordController.py +++ b/src/controller/recordController.py @@ -11,7 +11,12 @@ @Record.post("/") def add_to_record(record: recordSchema.RecordCreate, db: Session = Depends(get_db)): - return recordRepository.create_record(db=db, record=record) + record_user = recordSchema.RecordGet(user_id =record.user_id) + videos = recordRepository.get_record(db = db , record = record_user) + if videos: + return recordRepository.create_record(db=db, record=record, is_creat= False) + + return recordRepository.create_record(db=db, record=record, is_creat= True) @Record.get("/get_record") def check_record(user_id: str =Query(...) , db: Session = Depends(get_db)): diff --git a/src/repository/recordRepository.py b/src/repository/recordRepository.py index e59563f..bc05069 100644 --- a/src/repository/recordRepository.py +++ b/src/repository/recordRepository.py @@ -4,12 +4,25 @@ from model import recordModel from fastapi import HTTPException -def create_record(db: Session, record: recordSchema.RecordCreate): - db_record = recordModel.Record( - user_id=record.user_id.strip(), - videos=record.videos - ) - db.add(db_record) +def create_record(db: Session, record: recordSchema.RecordCreate, is_creat): + if is_creat == True: + db_record = recordModel.Record( + user_id=record.user_id.strip(), + videos=record.videos + ) + db.add(db_record) + else: + db_record = db.query(recordModel.Record).filter(recordModel.Record.user_id == record.user_id).first() + + id_video = list(record.videos.keys())[0] + timestamp_video = list(record.videos.values())[0] + + video_append = db_record.videos.copy() + video_append[id_video] = timestamp_video + print(video_append) + + db_record.videos = video_append + db.commit() db.refresh(db_record) print(f"Created record: user_id={db_record.user_id}") From 50aea5202195f4461119eaabbd227f77d17c9e54 Mon Sep 17 00:00:00 2001 From: victorleaoo Date: Thu, 8 Aug 2024 20:45:03 -0300 Subject: [PATCH 6/7] Correcoes de padroes de variavel --- src/controller/recordController.py | 6 +++--- src/domain/recordSchema.py | 3 ++- src/repository/recordRepository.py | 21 ++++++++++----------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/controller/recordController.py b/src/controller/recordController.py index ec0dea1..bbf4b57 100644 --- a/src/controller/recordController.py +++ b/src/controller/recordController.py @@ -12,11 +12,11 @@ @Record.post("/") def add_to_record(record: recordSchema.RecordCreate, db: Session = Depends(get_db)): record_user = recordSchema.RecordGet(user_id =record.user_id) - videos = recordRepository.get_record(db = db , record = record_user) + videos = recordRepository.get_record(db = db, record = record_user) if videos: - return recordRepository.create_record(db=db, record=record, is_creat= False) + return recordRepository.create_record(db=db, record=record, is_create= False) - return recordRepository.create_record(db=db, record=record, is_creat= True) + return recordRepository.create_record(db=db, record=record, is_create= True) @Record.get("/get_record") def check_record(user_id: str =Query(...) , db: Session = Depends(get_db)): diff --git a/src/domain/recordSchema.py b/src/domain/recordSchema.py index 1ca3bf4..439e550 100644 --- a/src/domain/recordSchema.py +++ b/src/domain/recordSchema.py @@ -1,8 +1,9 @@ from pydantic import BaseModel +from typing import Dict class RecordBase(BaseModel): user_id: str - videos: dict + videos: Dict[str, str] class RecordCreate(RecordBase): pass diff --git a/src/repository/recordRepository.py b/src/repository/recordRepository.py index bc05069..0f4ef08 100644 --- a/src/repository/recordRepository.py +++ b/src/repository/recordRepository.py @@ -4,28 +4,27 @@ from model import recordModel from fastapi import HTTPException -def create_record(db: Session, record: recordSchema.RecordCreate, is_creat): - if is_creat == True: +def create_record(db: Session, record: recordSchema.RecordCreate, is_create): + if is_create == True: db_record = recordModel.Record( user_id=record.user_id.strip(), videos=record.videos ) db.add(db_record) else: - db_record = db.query(recordModel.Record).filter(recordModel.Record.user_id == record.user_id).first() + db_record = db.query(recordModel.Record).filter(recordModel.Record.user_id == record.user_id).first() - id_video = list(record.videos.keys())[0] - timestamp_video = list(record.videos.values())[0] + id_video = list(record.videos.keys())[0] + timestamp_video = list(record.videos.values())[0] - video_append = db_record.videos.copy() - video_append[id_video] = timestamp_video - print(video_append) - - db_record.videos = video_append + video_append = db_record.videos.copy() + video_append[id_video] = timestamp_video + + db_record.videos = video_append db.commit() db.refresh(db_record) - print(f"Created record: user_id={db_record.user_id}") + print(f"Created/Updated record: user_id={db_record.user_id}") return db_record def get_record(db: Session, record: recordSchema.RecordGet): From b47d5ec4d63e704b2b2cc63c4ff7b432047af787 Mon Sep 17 00:00:00 2001 From: xzxjesse Date: Fri, 9 Aug 2024 18:18:11 -0300 Subject: [PATCH 7/7] Adiciona os testes do backend --- tests/junit.xml | 2 +- tests/test_record.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/test_record.py diff --git a/tests/junit.xml b/tests/junit.xml index 60c9578..99a4e9d 100644 --- a/tests/junit.xml +++ b/tests/junit.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/test_record.py b/tests/test_record.py new file mode 100644 index 0000000..2b0bbf4 --- /dev/null +++ b/tests/test_record.py @@ -0,0 +1,44 @@ +import pytest, sys, os + +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src'))) + +from fastapi.testclient import TestClient +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from src.database import Base, get_db +from src.main import app + +# Crie um banco de dados de teste em memória +SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" +engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) +TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +# Dependência para usar o banco de dados de teste +def override_get_db(): + try: + db = TestingSessionLocal() + yield db + finally: + db.close() + +app.dependency_overrides[get_db] = override_get_db + +client = TestClient(app) + +@pytest.fixture(scope="module") +def setup_database(): + Base.metadata.create_all(bind=engine) + yield + Base.metadata.drop_all(bind=engine) + +def test_add_to_record(setup_database): + response = client.post("/api/record/", json={"user_id": "user123", "videos": {"video_id": "timestamp"}}) + assert response.status_code == 200 + assert response.json()["user_id"] == "user123" + assert response.json()["videos"] == {"video_id": "timestamp"} + +def test_check_record(setup_database): + response = client.get("/api/record/get_record/", params={"user_id": "user123"}) + assert response.status_code == 200 + assert "videos" in response.json() + assert response.json()["videos"] == {"video_id": "timestamp"}