commit a767aa707946dfc8eae1f8b2f07e83d21734f5c3
Author: Captain_Trojan <sejakm@students.zcu.cz>
Date:   Sun May 2 16:11:36 2021 +0200

    Re #8702 - Minor bug and flow fixes.

diff --git a/src/controllers/certificates_controller.py b/src/controllers/certificates_controller.py
index d7d5d32..f2a913d 100644
--- a/src/controllers/certificates_controller.py
+++ b/src/controllers/certificates_controller.py
@@ -32,6 +32,7 @@ NOT_AFTER = "notAfter"
 NOT_BEFORE = "notBefore"
 COMMON_NAME = "CN"
 ID = "id"
+CA = "CA"
 USAGE = "usage"
 SUBJECT = "subject"
 VALIDITY_DAYS = "validityDays"
@@ -115,7 +116,7 @@ class CertController:
 
             key = self.key_service.create_new_key()                                      # TODO pass key
 
-            if TYPE not in body or body[TYPE] is None:                                  # if issuer omitted (legal) or none
+            if CA not in body or body[CA] is None:                                  # if issuer omitted (legal) or none
                 cert = self.certificate_service.create_root_ca(                          # create a root CA
                     key,
                     subject,
@@ -123,7 +124,7 @@ class CertController:
                     days=body[VALIDITY_DAYS]
                 )
             else:
-                issuer = self.certificate_service.get_certificate(body[TYPE])              # get base issuer info
+                issuer = self.certificate_service.get_certificate(body[CA])              # get base issuer info
 
                 if issuer is None:                                                  # if such issuer does not exist
                     Logger.error(f"No certificate authority with such unique ID exists 'ID = {key.private_key_id}'.")
@@ -280,7 +281,11 @@ class CertController:
         target_usages = {v for v in CertController.INVERSE_USAGE_KEY_MAP.keys()}
         target_cn_substring = None
         issuer_id = -1
+
+        unfiltered = True
+
         if PER_PAGE in data:
+            unfiltered = False
             page = data.get(PAGE, 0)
             per_page = data[PER_PAGE]
         else:
@@ -288,6 +293,7 @@ class CertController:
             per_page = None
 
         if FILTERING in data:                                                   # if the 'filtering' field exists
+            unfiltered = False
             if isinstance(data[FILTERING], dict):                               # and it is also a 'dict'
 
                 # noinspection DuplicatedCode
@@ -310,7 +316,7 @@ class CertController:
                         try:
                             target_usages = {CertController.USAGE_KEY_MAP[v] for v in data[FILTERING][USAGE]}
                         except KeyError as e:
-                            Logger.error(f"Invalid request, wrong parameters '{FILTERING}.{TYPE}' - '{e}'.")
+                            Logger.error(f"Invalid request, wrong parameters '{FILTERING}.{USAGE}' - '{e}'.")
                             return E_WRONG_PARAMETERS, C_BAD_REQUEST
                     else:
                         Logger.error(f"Invalid request, wrong parameters '{FILTERING}.{USAGE}'.")
@@ -331,30 +337,30 @@ class CertController:
                 Logger.error(f"Invalid request, wrong parameters '{FILTERING}'.")
                 return E_WRONG_PARAMETERS, C_BAD_REQUEST
 
-            if issuer_id >= 0:                                                  # if filtering by an issuer
-                try:
-                                                                                # get his children, filtered
-                    certs = self.certificate_service.get_certificates_issued_by_filter(
-                        issuer_id=issuer_id,
-                        target_types=target_types,
-                        target_usages=target_usages,
-                        target_cn_substring=target_cn_substring,
-                        page=page,
-                        per_page=per_page
-                    )
-                except CertificateNotFoundException:                            # if id does not exist
-                    Logger.error(f"No such certificate found 'ID = {issuer_id}'.")
-                    return E_NO_CERTIFICATES_FOUND, C_NOT_FOUND                 # throw
-            else:
-                certs = self.certificate_service.get_certificates_filter(
+        if unfiltered:                                                      # if not filtering
+            certs = self.certificate_service.get_certificates()
+        elif issuer_id >= 0:                                                # if filtering by an issuer
+            try:
+                                                                            # get his children, filtered
+                certs = self.certificate_service.get_certificates_issued_by_filter(
+                    issuer_id=issuer_id,
                     target_types=target_types,
                     target_usages=target_usages,
                     target_cn_substring=target_cn_substring,
                     page=page,
                     per_page=per_page
                 )
+            except CertificateNotFoundException:                            # if id does not exist
+                Logger.error(f"No such certificate found 'ID = {issuer_id}'.")
+                return E_NO_CERTIFICATES_FOUND, C_NOT_FOUND                 # throw
         else:
-            certs = self.certificate_service.get_certificates()
+            certs = self.certificate_service.get_certificates_filter(
+                target_types=target_types,
+                target_usages=target_usages,
+                target_cn_substring=target_cn_substring,
+                page=page,
+                per_page=per_page
+            )
 
         if certs is None:
             Logger.error(f"Internal server error (unknown origin).")
@@ -362,7 +368,7 @@ class CertController:
         elif len(certs) == 0:
             # TODO check log level
             Logger.warning(f"No such certificate found (empty list).")
-            return E_NO_CERTIFICATES_FOUND, C_NO_DATA
+            return {"success": True, "data": []}, C_SUCCESS
         else:
             ret = []
             for c in certs:
@@ -556,7 +562,7 @@ class CertController:
             NOT_BEFORE: datetime.strptime(c.valid_from, DATETIME_FORMAT).date(),
             NOT_AFTER: datetime.strptime(c.valid_to, DATETIME_FORMAT).date(),
             USAGE: {CertController.INVERSE_USAGE_KEY_MAP[k]: v for k, v in c.usages.items()},
-            TYPE: c_issuer.certificate_id
+            CA: c_issuer.certificate_id
         }
 
     def get_private_key_of_a_certificate(self, id):
