feat: Enhance data models and sorting logic for investors and projects
This commit is contained in:
+58
-20
@@ -81,20 +81,38 @@ def read_investors(
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="Project not found")
|
||||
|
||||
# Get paginated results
|
||||
investors = (
|
||||
db.query(InvestorTable)
|
||||
.options(
|
||||
selectinload(InvestorTable.portfolio_companies),
|
||||
selectinload(InvestorTable.team_members),
|
||||
selectinload(InvestorTable.sectors),
|
||||
selectinload(InvestorTable.funds).selectinload(FundTable.investment_stages),
|
||||
selectinload(InvestorTable.funds).selectinload(FundTable.sectors),
|
||||
# When project_id is provided, we need to get all investors first to sort by compatibility score
|
||||
# Otherwise, we can paginate at the database level
|
||||
if project is not None:
|
||||
# Get all investors (we'll sort by compatibility score, then paginate)
|
||||
all_investors = (
|
||||
db.query(InvestorTable)
|
||||
.options(
|
||||
selectinload(InvestorTable.portfolio_companies),
|
||||
selectinload(InvestorTable.team_members),
|
||||
selectinload(InvestorTable.sectors),
|
||||
selectinload(InvestorTable.funds).selectinload(FundTable.investment_stages),
|
||||
selectinload(InvestorTable.funds).selectinload(FundTable.sectors),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
# We'll paginate after sorting by compatibility score
|
||||
investors = all_investors
|
||||
else:
|
||||
# Get paginated results (no sorting needed)
|
||||
investors = (
|
||||
db.query(InvestorTable)
|
||||
.options(
|
||||
selectinload(InvestorTable.portfolio_companies),
|
||||
selectinload(InvestorTable.team_members),
|
||||
selectinload(InvestorTable.sectors),
|
||||
selectinload(InvestorTable.funds).selectinload(FundTable.investment_stages),
|
||||
selectinload(InvestorTable.funds).selectinload(FundTable.sectors),
|
||||
)
|
||||
.offset(offset)
|
||||
.limit(page_size)
|
||||
.all()
|
||||
)
|
||||
.offset(offset)
|
||||
.limit(page_size)
|
||||
.all()
|
||||
)
|
||||
|
||||
# Transform to InvestmentResponse format (one row per investor-fund combination)
|
||||
investment_responses = []
|
||||
@@ -122,10 +140,10 @@ def read_investors(
|
||||
else None
|
||||
)
|
||||
|
||||
# Get top 3 sectors from fund (id and name only)
|
||||
# Get top 3 sectors from fund (id and name only) - sorted alphabetically
|
||||
fund_sectors = [
|
||||
SectorMinimal(id=sector.id, name=sector.name)
|
||||
for sector in (fund.sectors[:3] if fund.sectors else [])
|
||||
for sector in sorted(fund.sectors[:3] if fund.sectors else [], key=lambda s: s.name)
|
||||
]
|
||||
|
||||
investment_response = InvestmentResponse(
|
||||
@@ -166,6 +184,12 @@ def read_investors(
|
||||
)
|
||||
investment_responses.append(investment_response)
|
||||
|
||||
# Sort by compatibility score (descending) when project_id is provided
|
||||
if project is not None:
|
||||
investment_responses.sort(key=lambda x: x.compatibility_score, reverse=True)
|
||||
# Apply pagination after sorting
|
||||
investment_responses = investment_responses[offset:offset + page_size]
|
||||
|
||||
# Calculate total pages
|
||||
total_pages = (total_count + page_size - 1) // page_size
|
||||
|
||||
@@ -257,9 +281,16 @@ def filter_investors(
|
||||
# Get total count before pagination
|
||||
total_count = query.count()
|
||||
|
||||
# Calculate offset and apply pagination
|
||||
offset = (page - 1) * page_size
|
||||
funds = query.offset(offset).limit(page_size).all()
|
||||
# When project_id is provided, we need to get all funds first to sort by compatibility score
|
||||
# Otherwise, we can paginate at the database level
|
||||
if project is not None:
|
||||
# Get all funds (we'll sort by compatibility score, then paginate)
|
||||
all_funds = query.all()
|
||||
funds = all_funds
|
||||
else:
|
||||
# Calculate offset and apply pagination (no sorting needed)
|
||||
offset = (page - 1) * page_size
|
||||
funds = query.offset(offset).limit(page_size).all()
|
||||
|
||||
# Transform to InvestmentResponse format (one row per fund)
|
||||
investment_responses = []
|
||||
@@ -286,10 +317,10 @@ def filter_investors(
|
||||
else None
|
||||
)
|
||||
|
||||
# Get top 3 sectors from fund (id and name only)
|
||||
# Get top 3 sectors from fund (id and name only) - sorted alphabetically
|
||||
fund_sectors = [
|
||||
SectorMinimal(id=sector.id, name=sector.name)
|
||||
for sector in (fund.sectors[:3] if fund.sectors else [])
|
||||
for sector in sorted(fund.sectors[:3] if fund.sectors else [], key=lambda s: s.name)
|
||||
]
|
||||
|
||||
investment_response = InvestmentResponse(
|
||||
@@ -308,6 +339,13 @@ def filter_investors(
|
||||
)
|
||||
investment_responses.append(investment_response)
|
||||
|
||||
# Sort by compatibility score (descending) when project_id is provided
|
||||
if project is not None:
|
||||
investment_responses.sort(key=lambda x: x.compatibility_score, reverse=True)
|
||||
# Apply pagination after sorting
|
||||
offset = (page - 1) * page_size
|
||||
investment_responses = investment_responses[offset:offset + page_size]
|
||||
|
||||
# Calculate total pages
|
||||
total_pages = (total_count + page_size - 1) // page_size
|
||||
|
||||
|
||||
Reference in New Issue
Block a user