37 The method to get every services contained in the db
39 title =
"get_services"
43 self.
disp.log_debug(f
"Token = {token}", title)
59 if not services_data
or isinstance(services_data, int):
62 message=
"services not found.",
69 content_type=CONST.CONTENT_TYPE,
72 self.
disp.log_debug(f
"Retrieved data {services_data}", title)
73 for i, service
in enumerate(services_data):
74 if "api_key" in service:
79 service[
"created_at"])
82 message=services_data,
88 content_type=CONST.CONTENT_TYPE,
94 The method to get a service by it's id
96 title =
"Get service by id"
100 self.
disp.log_debug(f
"Token = {token}", title)
115 f
"id='{service_id}'",
118 if isinstance(service_data, int):
123 for i, service
in enumerate(service_data):
124 if "api_key" in service:
129 service[
"created_at"]
131 if len(service_data) == 1:
132 service_data = service_data[0]
133 self.
disp.log_debug(f
"Service found: {service_data}", title)
136 message=service_data,
142 content_type=CONST.CONTENT_TYPE,
148 The function to get and filter every services by specifics tag
150 title =
"get_services_by_tag"
154 self.
disp.log_debug(f
"Token = {token}", title)
166 if not tags
or tags ==
"":
171 tags_list = tags.split(
":")
176 filtered_services: list[dict] = []
177 for i, service
in enumerate(services_data):
178 if "api_key" in service:
182 for _, element
in enumerate(tags_list):
183 if element
in service[
"tags"]:
184 filtered_services.append(service)
185 if not filtered_services:
188 message=f
"No services found with the given tags '{tags}'.",
193 return HCI.not_found(
195 content_type=CONST.CONTENT_TYPE,
198 for i, service
in enumerate(filtered_services):
200 service[
"created_at"])
201 msg = f
"Services with guven tags '{tags}': "
202 msg += f
"{filtered_services}"
203 self.
disp.log_debug(msg, title)
206 message=filtered_services,
212 content_type=CONST.CONTENT_TYPE,
218 The function to get and filter every services by the most recent to the oldest
220 title =
"get_recent_services"
224 self.
disp.log_debug(f
"Token = {token}", title)
240 recent_services = sorted(
241 service_data, key=
lambda x: x[
"created_at"], reverse=
True
243 if not recent_services:
246 message=
"No recent services found.",
251 return HCI.not_found(
253 content_type=CONST.CONTENT_TYPE,
256 for i, service
in enumerate(recent_services):
257 if "api_key" in service:
262 service[
"created_at"])
263 self.
disp.log_debug(f
"Recent services: {recent_services}", title)
266 message=recent_services,
272 content_type=CONST.CONTENT_TYPE,
278 The function to get a service id by the name
280 title =
"get_service_id_by_name"
284 self.
disp.log_debug(f
"Token = {token}", title)
301 if isinstance(retrieved_id, int):
306 service_id = str(retrieved_id[0][
"id"])
315 content_type=CONST.CONTENT_TYPE,
321 Create a new service (Only for admin account)
323 title: str =
"create_service"
341 self.
disp.log_debug(f
"Service name: {name}", title)
347 if isinstance(response, int)
is False:
353 if not request_body
or not all(key
in request_body
for key
in (
"url",
"api_key",
"category",
"type",
"tags",
"colour",
"description")):
358 self.
disp.log_debug(f
"Request body: {request_body}", title)
362 request_body[
"api_key"],
363 request_body[
"category"],
365 request_body[
"type"],
366 request_body[
"tags"],
369 request_body[
"colour"],
370 request_body[
"description"]
372 self.
disp.log_debug(f
"Generated data: {data}", title)
375 if isinstance(columns, int):
381 self.
disp.log_debug(f
"Columns: {columns}", title)
389 message=
"The new service is created successfully.",
395 content_type=CONST.CONTENT_TYPE,
401 Update a service data (Only for admin account)
403 title: str =
"update_service"
421 self.
disp.log_debug(f
"Service id: {service_id}", title)
427 msg = f
"Failed to retrieve data from '{CONST.TAB_SERVICES}'"
429 self.
disp.log_error(msg, title)
437 if not request_body
or not all(key
in request_body
for key
in (
"name",
"url",
"api_key",
"category",
"tags",
"colour",
"description")):
442 self.
disp.log_debug(f
"Request body: {request_body}", title)
444 request_body[
"name"],
446 request_body[
"api_key"],
447 request_body[
"category"],
448 request_body[
"tags"],
449 request_body[
"colour"],
450 request_body[
"description"]
452 self.
disp.log_debug(f
"Generated data: {data}", title)
456 if isinstance(columns, int):
457 msg =
"Failed to retrieve columns from "
458 msg += f
"'{CONST.TAB_SERVICES}' table."
459 self.
disp.log_error(msg, title)
469 self.
disp.log_debug(f
"Columns: {columns}", title)
476 msg = f
"Failed to update data in '{CONST.TAB_SERVICES}"
478 self.
disp.log_error(msg, title)
485 message=
"The service is updated successfully.",
491 content_type=CONST.CONTENT_TYPE,
495 async def patch_service(self, request: Request, service_id: str) -> Response:
497 Update a service value (Only for admin account)
499 title: str =
"update_service"
517 self.
disp.log_debug(f
"Service id: {service_id}", title)
523 msg = f
"Failed to retrieve data from '{CONST.TAB_SERVICES}'"
525 self.
disp.log_error(msg, title)
538 self.
disp.log_debug(f
"Request body: {request_body}", title)
539 if "name" in request_body:
548 if "url" in request_body:
557 if "api_key" in request_body:
566 if "category" in request_body:
575 if "tags" in request_body:
584 if "colour" in request_body:
593 if "description" in request_body:
604 message=
"The service value is updated successfully.",
610 content_type=CONST.CONTENT_TYPE,
616 The function to delete a service from the database
618 title: str =
"delete_oauth_provider"
624 self.
disp.log_debug(f
"Service id: {service_id}", title)
628 self.
disp.log_debug(f
"Token gotten: {token}", title)
630 self.
disp.log_error(
"You're not admin.", title)
640 if isinstance(retrived_data, int):
645 provider_name = retrived_data[0][
"name"]
648 if retrived_data[0][
"oauth"] == 1:
650 CONST.TAB_ACTIVE_OAUTHS,
651 f
"service_id='{service_id}'"
658 CONST.TAB_USER_OAUTH_CONNECTION,
659 f
"provider_name='{provider_name}'"
675 message=
"The service has been deleted successfully.",
681 content_type=CONST.CONTENT_TYPE,