feat: implement researcher and publication management with sync functionality
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.models import Publication
|
||||
|
||||
|
||||
class PublicationRepository:
|
||||
|
||||
@staticmethod
|
||||
def get_by_put_code(db: Session, researcher_id: str, put_code: int):
|
||||
"""
|
||||
Devuelve una publicación existente por put_code (único en ORCID).
|
||||
"""
|
||||
return (
|
||||
db.query(Publication)
|
||||
.filter(
|
||||
Publication.researcher_id == researcher_id,
|
||||
Publication.put_code == put_code
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create(db: Session, researcher_id: str, data: dict):
|
||||
"""
|
||||
Crea una nueva publicación normalizada.
|
||||
"""
|
||||
pub = Publication(
|
||||
researcher_id=researcher_id,
|
||||
put_code=data["put_code"],
|
||||
title=data["title"],
|
||||
journal=data["journal"],
|
||||
doi=data["doi"],
|
||||
pub_year=data["pub_year"],
|
||||
type=data["type"],
|
||||
hash_fingerprint=data["hash_fingerprint"]
|
||||
)
|
||||
db.add(pub)
|
||||
db.commit()
|
||||
db.refresh(pub)
|
||||
return pub
|
||||
|
||||
@staticmethod
|
||||
def update(db: Session, publication: Publication, data: dict):
|
||||
"""
|
||||
Actualiza una publicación existente si ORCID ha cambiado algo.
|
||||
"""
|
||||
publication.title = data["title"]
|
||||
publication.journal = data["journal"]
|
||||
publication.doi = data["doi"]
|
||||
publication.pub_year = data["pub_year"]
|
||||
publication.type = data["type"]
|
||||
publication.hash_fingerprint = data["hash_fingerprint"]
|
||||
|
||||
db.commit()
|
||||
db.refresh(publication)
|
||||
return publication
|
||||
|
||||
@staticmethod
|
||||
def list_by_researcher(db: Session, researcher_id: str):
|
||||
"""
|
||||
Lista todas las publicaciones de un investigador.
|
||||
"""
|
||||
return (
|
||||
db.query(Publication)
|
||||
.filter(Publication.researcher_id == researcher_id)
|
||||
.order_by(Publication.pub_year.desc().nullslast())
|
||||
.all()
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.models import Researcher
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
|
||||
class ResearcherRepository:
|
||||
|
||||
@staticmethod
|
||||
def get_by_orcid(db: Session, orcid_id: str):
|
||||
return db.query(Researcher).filter(Researcher.orcid_id == orcid_id).first()
|
||||
|
||||
@staticmethod
|
||||
def create(db: Session, orcid_id: str, name: str = None):
|
||||
researcher = Researcher(orcid_id=orcid_id, name=name)
|
||||
db.add(researcher)
|
||||
db.commit()
|
||||
db.refresh(researcher)
|
||||
return researcher
|
||||
|
||||
@staticmethod
|
||||
def update_last_sync(db: Session, researcher: Researcher):
|
||||
researcher.last_sync_at = func.now()
|
||||
db.commit()
|
||||
db.refresh(researcher)
|
||||
return researcher
|
||||
@@ -0,0 +1,28 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.models import SyncJob
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
|
||||
class SyncJobRepository:
|
||||
|
||||
@staticmethod
|
||||
def start_job(db: Session, researcher_id: str):
|
||||
job = SyncJob(
|
||||
researcher_id=researcher_id,
|
||||
status="running",
|
||||
started_at=func.now()
|
||||
)
|
||||
db.add(job)
|
||||
db.commit()
|
||||
db.refresh(job)
|
||||
return job
|
||||
|
||||
@staticmethod
|
||||
def finish_job(db: Session, job: SyncJob, new_records: int, updated_records: int):
|
||||
job.status = "finished"
|
||||
job.new_records = new_records
|
||||
job.updated_records = updated_records
|
||||
job.finished_at = func.now()
|
||||
db.commit()
|
||||
db.refresh(job)
|
||||
return job
|
||||
Reference in New Issue
Block a user