Skip to content

Commit 5a851e7

Browse files
committed
feat: 🚧 make paginated endpoint with parameters length and start #192
1 parent b5857c6 commit 5a851e7

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def find_all(
211211
custom_sql_conditions=custom_sql_conditions,
212212
custom_params=custom_params,
213213
max_count=kwargs.get("max_count", None),
214+
offset=kwargs.get("offset", 0),
214215
)
215216

216217
if time_entries:
@@ -430,6 +431,46 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
430431
max_count=limit,
431432
)
432433

434+
def get_all_paginated(self, conditions: dict = None, **kwargs) -> list:
435+
event_ctx = self.create_event_context("read-many")
436+
conditions.update({"owner_id": event_ctx.user_id})
437+
438+
# TODO: abstract this method with a function or a class method
439+
custom_query = []
440+
if "user_id" in conditions:
441+
if event_ctx.is_admin:
442+
conditions.pop("owner_id")
443+
custom_query = (
444+
[]
445+
if conditions.get("user_id") == "*"
446+
else [
447+
create_custom_query_from_str(
448+
conditions.get("user_id"), "c.owner_id"
449+
)
450+
]
451+
)
452+
conditions.pop("user_id")
453+
else:
454+
abort(
455+
HTTPStatus.FORBIDDEN, "You don't have enough permissions."
456+
)
457+
date_range = self.handle_date_filter_args(args=conditions)
458+
459+
length = conditions.get("length", None)
460+
conditions.pop("length", None)
461+
462+
start = conditions.get("start", None)
463+
conditions.pop("start", None)
464+
465+
return self.repository.find_all(
466+
event_ctx,
467+
conditions=conditions,
468+
custom_sql_conditions=custom_query,
469+
date_range=date_range,
470+
max_count=length,
471+
offset=start,
472+
)
473+
433474
def get(self, id):
434475
event_ctx = self.create_event_context("read")
435476

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,34 @@ def get(self):
345345
"""Find the summary of worked time"""
346346
conditions = summary_attribs_parser.parse_args()
347347
return time_entries_dao.get_worked_time(conditions)
348+
349+
350+
paginated_attribs_parser = ns.parser()
351+
paginated_attribs_parser.add_argument(
352+
'length',
353+
required=True,
354+
type=int,
355+
help="(Filter) The number of rows the endpoint should return.",
356+
location='args',
357+
)
358+
359+
paginated_attribs_parser.add_argument(
360+
'start',
361+
required=True,
362+
type=int,
363+
help="(Filter) The number of rows to be removed from the query. (aka offset)",
364+
location='args',
365+
)
366+
367+
368+
@ns.route('/paginated')
369+
@ns.response(HTTPStatus.OK, 'Time Entries paginated')
370+
@ns.response(HTTPStatus.NOT_FOUND, 'Time entry not found')
371+
class PaginatedTimeEntry(Resource):
372+
@ns.expect(paginated_attribs_parser)
373+
@ns.doc('list_time_entries_paginated')
374+
@ns.marshal_list_with(time_entry)
375+
def get(self):
376+
"""List all time entries paginated"""
377+
conditions = paginated_attribs_parser.parse_args()
378+
return time_entries_dao.get_all_paginated(conditions)

0 commit comments

Comments
 (0)