Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add sort to Event list #34

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dandelion/api/api_v1/endpoints/rsi_clcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from dandelion import crud, models, schemas
from dandelion.api import deps
from dandelion.schemas.utils import Sort

router = APIRouter()

Expand All @@ -44,6 +45,7 @@
},
)
def get_all(
sort_dir: Sort = Query(Sort.desc, alias="sortDir", description="Sort by ID(asc/desc)"),
info: Optional[int] = Query(None, alias="info", description="UseCase type"),
page_num: int = Query(1, alias="pageNum", ge=1, description="Page number"),
page_size: int = Query(10, alias="pageSize", ge=-1, description="Page size"),
Expand All @@ -55,6 +57,7 @@ def get_all(
db,
skip=skip,
limit=page_size,
sort=sort_dir,
info=info,
)
return schemas.RSICLCs(total=total, data=[clc.to_all_dict() for clc in data])
3 changes: 3 additions & 0 deletions dandelion/api/api_v1/endpoints/rsi_cwms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from dandelion import crud, models, schemas
from dandelion.api import deps
from dandelion.schemas.utils import Sort

router = APIRouter()

Expand Down Expand Up @@ -48,6 +49,7 @@ def get_all(
collision_type: Optional[int] = Query(
None, alias="collisionType", description="Collision Type"
),
sort_dir: Sort = Query(Sort.desc, alias="sortDir", description="Sort by ID(asc/desc)"),
page_num: int = Query(1, alias="pageNum", ge=1, description="Page number"),
page_size: int = Query(10, alias="pageSize", ge=-1, description="Page size"),
db: Session = Depends(deps.get_db),
Expand All @@ -58,6 +60,7 @@ def get_all(
db,
skip=skip,
limit=page_size,
sort=sort_dir,
event_type=event_type,
collision_type=collision_type,
)
Expand Down
3 changes: 3 additions & 0 deletions dandelion/api/api_v1/endpoints/rsi_dnps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from dandelion import crud, models, schemas
from dandelion.api import deps
from dandelion.schemas.utils import Sort

router = APIRouter()

Expand All @@ -45,6 +46,7 @@
)
def get_all(
info: Optional[int] = Query(None, alias="info", description="UseCase type"),
sort_dir: Sort = Query(Sort.desc, alias="sortDir", description="Sort by ID(asc/desc)"),
page_num: int = Query(1, alias="pageNum", ge=1, description="Page number"),
page_size: int = Query(10, alias="pageSize", ge=-1, description="Page size"),
db: Session = Depends(deps.get_db),
Expand All @@ -55,6 +57,7 @@ def get_all(
db,
skip=skip,
limit=page_size,
sort=sort_dir,
info=info,
)
return schemas.RSIDNPs(total=total, data=[dnp.to_all_dict() for dnp in data])
10 changes: 9 additions & 1 deletion dandelion/api/api_v1/endpoints/rsi_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from dandelion import crud, models, schemas
from dandelion.api import deps
from dandelion.schemas.utils import Sort

router = APIRouter()
LOG: LoggerAdapter = log.getLogger(__name__)
Expand Down Expand Up @@ -83,13 +84,20 @@ def get_all(
address: Optional[str] = Query(
None, alias="address", description="Filter by address. Fuzzy prefix query is supported"
),
sort_dir: Sort = Query(Sort.desc, alias="sortDir", description="Sort by ID(asc/desc)"),
page_num: int = Query(1, alias="pageNum", ge=1, description="Page number"),
page_size: int = Query(10, alias="pageSize", ge=-1, description="Page size"),
db: Session = Depends(deps.get_db),
current_user: models.User = Depends(deps.get_current_user),
) -> schemas.RSIEvents:
skip = page_size * (page_num - 1)
total, data = crud.rsi_event.get_multi_with_total(
db, skip=skip, limit=page_size, event_type=event_type, area_code=area_code, address=address
db,
skip=skip,
limit=page_size,
sort=sort_dir,
event_type=event_type,
area_code=area_code,
address=address,
)
return schemas.RSIEvents(total=total, data=[rsi_event.to_all_dict() for rsi_event in data])
3 changes: 3 additions & 0 deletions dandelion/api/api_v1/endpoints/rsi_sdss.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from dandelion import crud, models, schemas
from dandelion.api import deps
from dandelion.schemas.utils import Sort

router = APIRouter()

Expand All @@ -47,6 +48,7 @@ def get_all(
equipment_type: Optional[int] = Query(
None, alias="equipmentType", description="Equipment Type"
),
sort_dir: Sort = Query(Sort.desc, alias="sortDir", description="Sort by ID(asc/desc)"),
page_num: int = Query(1, alias="pageNum", ge=1, description="Page number"),
page_size: int = Query(10, alias="pageSize", ge=-1, description="Page size"),
db: Session = Depends(deps.get_db),
Expand All @@ -57,6 +59,7 @@ def get_all(
db,
skip=skip,
limit=page_size,
sort=sort_dir,
equipment_type=equipment_type,
)
return schemas.RSISDSs(total=total, data=[sds.to_all_dict() for sds in data])
4 changes: 3 additions & 1 deletion dandelion/api/api_v1/endpoints/rsm_participants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from dandelion import crud, models, schemas
from dandelion.api import deps
from dandelion.schemas.utils import Sort

router = APIRouter()
LOG: LoggerAdapter = log.getLogger(__name__)
Expand All @@ -48,14 +49,15 @@
)
def get_all(
ptc_type: Optional[str] = Query(None, alias="ptcType", description="Filter by ptcType"),
sort_dir: Sort = Query(Sort.desc, alias="sortDir", description="Sort by ID(asc/desc)"),
page_num: int = Query(1, alias="pageNum", ge=1, description="Page number"),
page_size: int = Query(10, alias="pageSize", ge=-1, description="Page size"),
db: Session = Depends(deps.get_db),
current_user: models.User = Depends(deps.get_current_user),
) -> schemas.RSMParticipants:
skip = page_size * (page_num - 1)
total, data = crud.rsm_participant.get_multi_with_total(
db, skip=skip, limit=page_size, ptc_type=ptc_type
db, skip=skip, limit=page_size, sort=sort_dir, ptc_type=ptc_type
)
return schemas.RSMParticipants(
total=total, data=[rsm_participant.to_dict() for rsm_participant in data]
Expand Down
7 changes: 6 additions & 1 deletion dandelion/crud/crud_rsi_clc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dandelion.crud.base import CRUDBase
from dandelion.models import RSICLC
from dandelion.schemas import RSICLCCreate
from dandelion.schemas.utils import Sort


class CRUDRSICLC(CRUDBase[RSICLC, RSICLCCreate, RSICLCCreate]):
Expand All @@ -40,13 +41,17 @@ def get_multi_with_total(
*,
skip: int = 0,
limit: int = 10,
sort: Sort = Sort.desc,
info: Optional[int] = None,
) -> Tuple[int, List[RSICLC]]:
query_ = db.query(self.model)
if info is not None:
query_ = query_.filter(self.model.info == info)
total = query_.count()
query_ = query_.order_by(desc(self.model.id))
if sort == Sort.asc:
query_ = query_.order_by(self.model.id)
else:
query_ = query_.order_by(desc(self.model.id))
if limit != -1:
query_ = query_.offset(skip).limit(limit)
data = query_.all()
Expand Down
7 changes: 6 additions & 1 deletion dandelion/crud/crud_rsi_cwm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dandelion.crud.base import CRUDBase
from dandelion.models import RSICWM
from dandelion.schemas import RSICWMCreate
from dandelion.schemas.utils import Sort


class CRUDRSICWM(CRUDBase[RSICWM, RSICWMCreate, RSICWMCreate]):
Expand All @@ -40,6 +41,7 @@ def get_multi_with_total(
*,
skip: int = 0,
limit: int = 10,
sort: Sort = Sort.desc,
event_type: Optional[int] = 0,
collision_type: Optional[int],
) -> Tuple[int, List[RSICWM]]:
Expand All @@ -49,7 +51,10 @@ def get_multi_with_total(
if collision_type is not None:
query_ = query_.filter(self.model.collision_type == collision_type)
total = query_.count()
query_ = query_.order_by(desc(self.model.id))
if sort == Sort.asc:
query_ = query_.order_by(self.model.id)
else:
query_ = query_.order_by(desc(self.model.id))
if limit != -1:
query_ = query_.offset(skip).limit(limit)
data = query_.all()
Expand Down
7 changes: 6 additions & 1 deletion dandelion/crud/crud_rsi_dnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dandelion.crud.base import CRUDBase
from dandelion.models import RSIDNP
from dandelion.schemas import RSIDNPCreate
from dandelion.schemas.utils import Sort


class CRUDRSIDNP(CRUDBase[RSIDNP, RSIDNPCreate, RSIDNPCreate]):
Expand All @@ -40,13 +41,17 @@ def get_multi_with_total(
*,
skip: int = 0,
limit: int = 10,
sort: Sort = Sort.desc,
info: Optional[int] = None,
) -> Tuple[int, List[RSIDNP]]:
query_ = db.query(self.model)
if info is not None:
query_ = query_.filter(self.model.info == info)
total = query_.count()
query_ = query_.order_by(desc(self.model.id))
if sort == Sort.asc:
query_ = query_.order_by(self.model.id)
else:
query_ = query_.order_by(desc(self.model.id))
if limit != -1:
query_ = query_.offset(skip).limit(limit)
data = query_.all()
Expand Down
7 changes: 6 additions & 1 deletion dandelion/crud/crud_rsi_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dandelion.crud.base import CRUDBase
from dandelion.models import RSU, RSIEvent
from dandelion.schemas import RSIEventCreate, RSIEventUpdate
from dandelion.schemas.utils import Sort


class CRUDRSIEvent(CRUDBase[RSIEvent, RSIEventCreate, RSIEventUpdate]):
Expand All @@ -43,6 +44,7 @@ def get_multi_with_total(
*,
skip: int = 0,
limit: int = 10,
sort: Sort = Sort.desc,
event_type: Optional[int] = None,
area_code: Optional[str] = None,
address: Optional[str] = None,
Expand All @@ -55,7 +57,10 @@ def get_multi_with_total(
if address is not None:
query_ = query_.filter(self.model.address.like(f"{address}%"))
total = query_.count()
query_ = query_.order_by(desc(self.model.id))
if sort == Sort.asc:
query_ = query_.order_by(self.model.id)
else:
query_ = query_.order_by(desc(self.model.id))
if limit != -1:
query_ = query_.offset(skip).limit(limit)
data = query_.all()
Expand Down
7 changes: 6 additions & 1 deletion dandelion/crud/crud_rsi_sds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dandelion.crud.base import CRUDBase
from dandelion.models import RSISDS
from dandelion.schemas import RSISDSCreate
from dandelion.schemas.utils import Sort


class CRUDRSISDS(CRUDBase[RSISDS, RSISDSCreate, RSISDSCreate]):
Expand All @@ -40,13 +41,17 @@ def get_multi_with_total(
*,
skip: int = 0,
limit: int = 10,
sort: Sort = Sort.desc,
equipment_type: Optional[int] = None,
) -> Tuple[int, List[RSISDS]]:
query_ = db.query(self.model)
if equipment_type is not None:
query_ = query_.filter(self.model.equipment_type == equipment_type)
total = query_.count()
query_ = query_.order_by(desc(self.model.id))
if sort == Sort.asc:
query_ = query_.order_by(self.model.id)
else:
query_ = query_.order_by(desc(self.model.id))
if limit != -1:
query_ = query_.offset(skip).limit(limit)
data = query_.all()
Expand Down
7 changes: 6 additions & 1 deletion dandelion/crud/crud_rsm_participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from dandelion.crud.base import CRUDBase
from dandelion.models import Participants
from dandelion.schemas import RSMParticipantCreate, RSMParticipantUpdate
from dandelion.schemas.utils import Sort


class CRUDRSMParticipant(CRUDBase[Participants, RSMParticipantCreate, RSMParticipantUpdate]):
Expand All @@ -33,13 +34,17 @@ def get_multi_with_total(
*,
skip: int = 0,
limit: int = 10,
sort: Sort = Sort.desc,
ptc_type: Optional[str] = None,
) -> Tuple[int, List[Participants]]:
query_ = db.query(self.model)
if ptc_type is not None:
query_ = query_.filter(self.model.ptc_type == ptc_type)
total = query_.count()
query_ = query_.order_by(desc(self.model.id))
if sort == Sort.asc:
query_ = query_.order_by(self.model.id)
else:
query_ = query_.order_by(desc(self.model.id))
if limit != -1:
query_ = query_.offset(skip).limit(limit)
data = query_.all()
Expand Down
2 changes: 2 additions & 0 deletions dandelion/schemas/rsi_clc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -47,6 +48,7 @@ class RSICLCCreate(RSICLCBase):

class RSICLCInDBBase(RSICLCBase):
id: int = Field(..., alias="id", description="CLC ID")
create_time: datetime = Field(..., alias="createTime", description="Create Time")

class Config:
orm_mode = True
Expand Down
2 changes: 2 additions & 0 deletions dandelion/schemas/rsi_cwm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -64,6 +65,7 @@ class RSICWMCreate(RSICWMBase):

class RSICWMInDBBase(RSICWMBase):
id: int = Field(..., alias="id", description="CWM ID")
create_time: datetime = Field(..., alias="createTime", description="Create Time")

class Config:
orm_mode = True
Expand Down
2 changes: 2 additions & 0 deletions dandelion/schemas/rsi_dnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -59,6 +60,7 @@ class RSIDNPCreate(RSIDNPBase):

class RSIDNPInDBBase(RSIDNPBase):
id: int = Field(..., alias="id", description="DNP ID")
create_time: datetime = Field(..., alias="createTime", description="Create Time")

class Config:
orm_mode = True
Expand Down
2 changes: 2 additions & 0 deletions dandelion/schemas/rsi_sds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -42,6 +43,7 @@ class RSISDSCreate(RSISDSBase):

class RSISDSInDBBase(RSISDSBase):
id: int = Field(..., alias="id", description="SDS ID")
create_time: datetime = Field(..., alias="createTime", description="Create Time")

class Config:
orm_mode = True
Expand Down
6 changes: 6 additions & 0 deletions dandelion/schemas/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class Sort(str, Enum):
asc = "asc"
desc = "desc"
Loading