added new endpoint for searching single dfg project by id
This commit is contained in:
@@ -13,6 +13,7 @@ from inse.projects.model import (
|
||||
EuProjectPage,
|
||||
)
|
||||
from inse.models.eu_project import EuProject
|
||||
from inse.models.dfg_project_details import DfgProjectDetails
|
||||
|
||||
|
||||
settings = get_settings()
|
||||
@@ -124,7 +125,7 @@ class BundProjectsSearchClient(ProjectsSearchClient):
|
||||
raise ValueError("Elasticsearch INSE API key is missing in settings.")
|
||||
super().__init__(
|
||||
base_url=settings.elasticsearch_inse_url,
|
||||
index="projects_bund_v1",
|
||||
index="projects_bund",
|
||||
api_key=settings.elasticsearch_inse_api_key,
|
||||
)
|
||||
|
||||
@@ -236,6 +237,24 @@ class BundProjectsSearchClient(ProjectsSearchClient):
|
||||
response = await self.make_search_request(query)
|
||||
return self.response_to_bund_project_page(response, page, size)
|
||||
|
||||
async def get_bund_projects_by_institution_name(
|
||||
self,
|
||||
institution_names: List[str],
|
||||
page: int,
|
||||
size: int,
|
||||
sort_by: str = "topic",
|
||||
sort_direction: str = "asc",
|
||||
) -> BundProjectPage:
|
||||
query = self.bund_projects_query(
|
||||
page=page,
|
||||
size=size,
|
||||
institution_names=institution_names,
|
||||
sort_by=sort_by,
|
||||
sort_direction=sort_direction,
|
||||
)
|
||||
response = await self.make_search_request(query)
|
||||
return self.response_to_bund_project_page(response, page, size)
|
||||
|
||||
|
||||
class EuProjectsSearchClient(ProjectsSearchClient):
|
||||
def __init__(self):
|
||||
@@ -605,3 +624,19 @@ class DfgProjectsSearchClient(ProjectsSearchClient):
|
||||
)
|
||||
response = await self.make_search_request(query)
|
||||
return self.response_to_dfg_project_page(response, page, size)
|
||||
|
||||
async def get_dfg_project_by_id(self, project_id: str) -> Optional[DfgProjectDetails]:
|
||||
base_query = self.dfg_projects_query(page=0, size=1)
|
||||
query = {
|
||||
"query": {"ids": {"values": [project_id]}},
|
||||
"_source": True
|
||||
}
|
||||
|
||||
response = await self.make_search_request(query)
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
|
||||
if hits:
|
||||
hit = hits[0]
|
||||
details = DfgProjectDetails.from_elastic_hit(hit=hit)
|
||||
return details
|
||||
return None
|
||||
|
||||
@@ -3,6 +3,7 @@ import logging
|
||||
from typing import Annotated, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, status
|
||||
from fastapi import APIRouter, Depends, Query, Path, status
|
||||
|
||||
from inse.projects.model import (
|
||||
BundProjectPage,
|
||||
@@ -10,6 +11,7 @@ from inse.projects.model import (
|
||||
EuProjectPage,
|
||||
InstitutionFunderSummary,
|
||||
)
|
||||
from inse.models.dfg_project_details import DfgProjectDetails
|
||||
from inse.tags import Tags
|
||||
from inse.projects import service as projects_service
|
||||
from inse.projects.projects_search_client import (
|
||||
@@ -88,6 +90,33 @@ async def get_bund_projects(
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/dfg/{id}",
|
||||
summary="Get a single DFG project.",
|
||||
description="Returns a single DFG project based on its ID.",
|
||||
operation_id="getSingleDfgProject",
|
||||
responses={
|
||||
status.HTTP_200_OK: {
|
||||
"description": "Successful Response",
|
||||
"model": DfgProjectDetails,
|
||||
},
|
||||
status.HTTP_404_NOT_FOUND: {"description": "Project not found"},
|
||||
},
|
||||
response_model_by_alias=False,
|
||||
)
|
||||
async def get_single_dfg_project(
|
||||
dfg_search_client: Annotated[
|
||||
DfgProjectsSearchClient, Depends(get_dfg_search_client)
|
||||
],
|
||||
id: Annotated[str, Path(description="The ID of the DFG project to retrieve.")],
|
||||
) -> DfgProjectDetails:
|
||||
logger.debug(f"Received request to get single DFG project for id={id}")
|
||||
return await projects_service.get_single_dfg_project(
|
||||
dfg_search_client=dfg_search_client,
|
||||
project_id=id,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/institution/bund/funding-over-time",
|
||||
summary="Get funding over time for bund projects.",
|
||||
|
||||
@@ -13,6 +13,7 @@ from inse.projects.model import (
|
||||
EuProjectPage,
|
||||
InstitutionFunderSummary,
|
||||
)
|
||||
from inse.models.dfg_project_details import DfgProjectDetails
|
||||
from inse.institutions.institutions_search_client import (
|
||||
InstitutionsSearchClient,
|
||||
)
|
||||
@@ -186,3 +187,15 @@ async def get_institution_bund_funding_over_time(
|
||||
funding_per_year=sorted_funding_per_year,
|
||||
annual_average_over_period=annual_average_over_period,
|
||||
)
|
||||
|
||||
|
||||
async def get_single_dfg_project(
|
||||
dfg_search_client: DfgProjectsSearchClient,
|
||||
project_id: str,
|
||||
) -> DfgProjectDetails:
|
||||
logger.debug(f"Fetching single DFG project for id={project_id}")
|
||||
|
||||
project = await dfg_search_client.get_dfg_project_by_id(project_id)
|
||||
if not project:
|
||||
raise NotFoundException(f"DFG project with id {project_id} not found")
|
||||
return project
|
||||
|
||||
Reference in New Issue
Block a user