Skip to content

Commit

Permalink
Merge pull request #12 from pingpingy1/main
Browse files Browse the repository at this point in the history
[feat] 모든 endpoint 구현 완료
  • Loading branch information
pingpingy1 authored Nov 29, 2023
2 parents f1857cd + f868b33 commit a0faa16
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 351 deletions.
188 changes: 52 additions & 136 deletions routers/ageHist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from model.AgeHist import (
AgeHistDataTypes,
AgeHistMethodTypes,
LocalAgeHistData,
MetroAgeHistData,
NationalAgeHistData,
)
Expand All @@ -15,154 +16,69 @@
async def getNationalAgeHistData(
ageHistType: AgeHistDataTypes, year: int, method: AgeHistMethodTypes
) -> BasicResponse.ErrorResponse | NationalAgeHistData:
# histogram = await MongoDB.client.stats_db["age_hist"].find_one(
# {
# "councilorType": "national_councilor",
# "is_elected": ageHistType == AgeHistDataTypes.elected,
# "year": year,
# "method": method,
# }
# )

# if histogram is None:
# return BasicResponse.ErrorResponse.model_validate(
# {
# "error": "NoDataError",
# "code": BasicResponse.NO_DATA_ERROR,
# "message": "No data retrieved with the provided input.",
# }
# )

# return NationalAgeHistData.model_validate({"data": histogram["data"]})
return NationalAgeHistData.model_validate(
histogram = await MongoDB.client.stats_db["age_hist"].find_one(
{
"data": [
{
"minAge": 21,
"maxAge": 22,
"count": 75,
"ageGroup": 0,
},
{
"minAge": 22,
"maxAge": 23,
"count": 87,
"ageGroup": 1,
},
{
"minAge": 29,
"maxAge": 30,
"count": 104,
"ageGroup": 2,
},
{
"minAge": 45,
"maxAge": 46,
"count": 354,
"ageGroup": 2,
},
{
"minAge": 46,
"maxAge": 47,
"count": 463,
"ageGroup": 3,
},
{
"minAge": 63,
"maxAge": 64,
"count": 240,
"ageGroup": 4,
},
]
"councilorType": "national_councilor",
"is_elected": ageHistType == AgeHistDataTypes.elected,
"year": year,
"method": method,
}
)

if histogram is None:
return BasicResponse.ErrorResponse.model_validate(
{
"error": "NoDataError",
"code": BasicResponse.NO_DATA_ERROR,
"message": "No data retrieved with the provided input.",
}
)

return NationalAgeHistData.model_validate({"data": histogram["data"]})


@router.get("/{metroId}")
async def getMetroAgeHistData(
metroId: int, ageHistType: AgeHistDataTypes, year: int, method: AgeHistMethodTypes
) -> BasicResponse.ErrorResponse | MetroAgeHistData:
# if (
# await MongoDB.client.district_db["metro_district"].find_one(
# {"metroId": metroId}
# )
# is None
# ):
# return BasicResponse.ErrorResponse.model_validate(
# {
# "error": "RegionCodeError",
# "code": BasicResponse.REGION_CODE_ERR,
# "message": f"No metro district with metroId {metroId}.",
# }
# )

# histogram = await MongoDB.client.stats_db["age_hist"].find_one(
# {
# "level": 1,
# "councilorType": "metro_councilor",
# "is_elected": ageHistType == AgeHistDataTypes.elected,
# "year": year,
# "method": method,
# "metroId": metroId,
# }
# )

# if histogram is None:
# return BasicResponse.ErrorResponse.model_validate(
# {
# "error": "NoDataError",
# "code": BasicResponse.NO_DATA_ERROR,
# "message": "No data retrieved with the provided input.",
# }
# )

# return MetroAgeHistData.model_validate(
# {"metroId": metroId, "data": histogram["data"]}
# )
return MetroAgeHistData.model_validate(
if (
await MongoDB.client.district_db["metro_district"].find_one(
{"metroId": metroId}
)
is None
):
return BasicResponse.ErrorResponse.model_validate(
{
"error": "RegionCodeError",
"code": BasicResponse.REGION_CODE_ERR,
"message": f"No metro district with metroId {metroId}.",
}
)

histogram = await MongoDB.client.stats_db["age_hist"].find_one(
{
"level": 1,
"councilorType": "metro_councilor",
"is_elected": ageHistType == AgeHistDataTypes.elected,
"year": year,
"method": method,
"metroId": metroId,
"data": [
{
"minAge": 21,
"maxAge": 22,
"count": 75,
"ageGroup": 0,
},
{
"minAge": 22,
"maxAge": 23,
"count": 87,
"ageGroup": 1,
},
{
"minAge": 29,
"maxAge": 30,
"count": 104,
"ageGroup": 2,
},
{
"minAge": 45,
"maxAge": 46,
"count": 354,
"ageGroup": 2,
},
{
"minAge": 46,
"maxAge": 47,
"count": 463,
"ageGroup": 3,
},
{
"minAge": 63,
"maxAge": 64,
"count": 240,
"ageGroup": 4,
},
],
}
)

if histogram is None:
return BasicResponse.ErrorResponse.model_validate(
{
"error": "NoDataError",
"code": BasicResponse.NO_DATA_ERROR,
"message": "No data retrieved with the provided input.",
}
)

return MetroAgeHistData.model_validate(
{"metroId": metroId, "data": histogram["data"]}
)


@router.get("/{metroId}/{localId}")
async def getLocalAgeHistData(
Expand All @@ -171,7 +87,7 @@ async def getLocalAgeHistData(
ageHistType: AgeHistDataTypes,
year: int,
method: AgeHistMethodTypes,
) -> BasicResponse.ErrorResponse | MetroAgeHistData:
) -> BasicResponse.ErrorResponse | LocalAgeHistData:
if (
await MongoDB.client.district_db["local_district"].find_one(
{"metroId": metroId, "localId": localId}
Expand Down Expand Up @@ -207,6 +123,6 @@ async def getLocalAgeHistData(
}
)

return MetroAgeHistData.model_validate(
return LocalAgeHistData.model_validate(
{"metroId": metroId, "localId": localId, "data": histogram["data"]}
)
3 changes: 0 additions & 3 deletions routers/scrapResultLocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,12 @@ async def getLocalTemplateData(
group["count"]
for group in history_candidate[idx]["data"]
),
# "candidateCount": 0,
"candidateDiversityIndex": history_candidate[idx][
"diversityIndex"
],
"candidateDiversityRank": history_candidate[idx][
"diversityRank"
],
# "candidateDiversityIndex": 0.0,
# "candidateDiversityRank": 0,
"electedDiversityIndex": history_elected[idx][
"diversityIndex"
],
Expand Down
Loading

0 comments on commit a0faa16

Please sign in to comment.