Skip to content

Commit

Permalink
get groups by entry
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaelicke committed Jan 8, 2025
1 parent 9e8631b commit 16330f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions metacatalog_api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def entries_locations(ids: int | List[int] = None, limit: int = None, offset: in
return result


def groups(id: int = None, title: str = None, description: str = None, type: str = None, with_metadata: bool = False, limit: int = None, offset: int = None):
def groups(id: int = None, title: str = None, description: str = None, type: str = None, entry_id: int = None, with_metadata: bool = False, limit: int = None, offset: int = None):
with connect() as session:
if id is not None or (title is not None and '%' not in title):
group = db.get_group(session, id=id, title=title, with_metadata=with_metadata)
return group
else:
groups = db.get_groups(session, title=title, description=description, type=type, limit=limit, offset=offset)
groups = db.get_groups(session, title=title, description=description, type=type, entry_id=entry_id, limit=limit, offset=offset)
return groups


Expand Down
4 changes: 3 additions & 1 deletion metacatalog_api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def get_grouptypes(session: Session) -> list[models.EntryGroupType]:
return [models.EntryGroupType.model_validate(t) for t in types]


def get_groups(session: Session, title: str = None, description: str = None, type: str = None, limit: int = None, offset: int = None, with_metadata: bool = False) -> list[models.EntryGroup] | list[models.EntryGroupWithMetadata]:
def get_groups(session: Session, title: str = None, description: str = None, type: str = None, entry_id: int = None, limit: int = None, offset: int = None, with_metadata: bool = False) -> list[models.EntryGroup] | list[models.EntryGroupWithMetadata]:
sql = select(models.EntryGroupTable)

if title is not None:
Expand All @@ -505,6 +505,8 @@ def get_groups(session: Session, title: str = None, description: str = None, typ
sql = sql.where(col(models.EntryGroupTable.description).ilike(description))
if type is not None:
sql = sql.join(models.EntryGroupTypeTable).where(models.EntryGroupTypeTable.name == type)
if entry_id is not None:
sql = sql.join(models.NMGroupsEntries).where(models.NMGroupsEntries.entry_id == entry_id)

if limit is not None:
sql = sql.limit(limit)
Expand Down
10 changes: 10 additions & 0 deletions metacatalog_api/router/api/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ def get_groups(title: str = None, description: str = None, type: str = None, lim
return groups


@read_router.get('/entries/{entry_id}/groups')
@read_router.get('/entries/{entry_id}/groups.json')
def get_groups_by_entry(entry_id: int) -> list[models.EntryGroup]:
try:
groups = core.groups(entry_id=entry_id, with_metadata=False)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
return groups


@read_router.get('/groups/{group_id}')
@read_router.get('/groups/{group_id}.json')
def get_group(group_id) -> models.EntryGroupWithMetadata:
Expand Down

0 comments on commit 16330f1

Please sign in to comment.