Skip to content

Commit 389795d

Browse files
committed
feat: 🚧 make paginated endpoint with parameters length and start #192
1 parent 93649c1 commit 389795d

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:
@@ -429,6 +430,46 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
429430
max_count=limit,
430431
)
431432

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

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,34 @@ def get(self):
331331
"""Find the summary of worked time"""
332332
conditions = summary_attribs_parser.parse_args()
333333
return time_entries_dao.get_worked_time(conditions)
334+
335+
336+
paginated_attribs_parser = ns.parser()
337+
paginated_attribs_parser.add_argument(
338+
'length',
339+
required=True,
340+
type=int,
341+
help="(Filter) The number of rows the endpoint should return.",
342+
location='args',
343+
)
344+
345+
paginated_attribs_parser.add_argument(
346+
'start',
347+
required=True,
348+
type=int,
349+
help="(Filter) The number of rows to be removed from the query. (aka offset)",
350+
location='args',
351+
)
352+
353+
354+
@ns.route('/paginated')
355+
@ns.response(HTTPStatus.OK, 'Time Entries paginated')
356+
@ns.response(HTTPStatus.NOT_FOUND, 'Time entry not found')
357+
class PaginatedTimeEntry(Resource):
358+
@ns.expect(paginated_attribs_parser)
359+
@ns.doc('list_time_entries_paginated')
360+
@ns.marshal_list_with(time_entry)
361+
def get(self):
362+
"""List all time entries paginated"""
363+
conditions = paginated_attribs_parser.parse_args()
364+
return time_entries_dao.get_all_paginated(conditions)

0 commit comments

Comments
 (0)