Skip to content

Latest commit

 

History

History
157 lines (148 loc) · 7.15 KB

README.md

File metadata and controls

157 lines (148 loc) · 7.15 KB

Optional

🛠️ Requisitos

📝 Actividad

Buscar el detalle de una cuenta utilizando opcionales

Esta actividad continua a la descrita en la clase anterior: README

  1. Dentro de nuestra función main agregamos un nuevo endpoint llamado getAccountsByName

     // Consultar todas las cuentas y buscarla por nombre utilizando Optional por si no es encontrada
        server.createContext("/api/getAccountByName", (exchange -> {
            LOGGER.info(msgProcPeticion);
            Instant inicioDeEjecucion = Instant.now();
            BankAccountBO bankAccountBO = new BankAccountBOImpl();
            String responseText = "";
            /** Validates the type of http request  */
            if ("GET".equals(exchange.getRequestMethod())) {
                LOGGER.info("LearningJava - Procesando peticion HTTP de tipo GET");
                List<BankAccountDTO> accounts = bankAccountBO.getAccounts();
                // Aquí implementaremos nuestro código de filtrar las cuentas por nombre utilizando optional
    
    
                JSONArray json = new JSONArray(accounts);
                responseText = json.toString();
                exchange.getResponseHeaders().add("Content-type", "application/json");
                exchange.sendResponseHeaders(200, responseText.getBytes().length);
            } else {
                /** 405 Method Not Allowed */
                exchange.sendResponseHeaders(405, -1);
            }
            OutputStream output = exchange.getResponseBody();
            Instant finalDeEjecucion = Instant.now();
            /**
             * Always remember to close the resources you open.
             * Avoid memory leaks
             */
            LOGGER.info("LearningJava - Cerrando recursos ...");
            String total = new String(String.valueOf(Duration.between(inicioDeEjecucion, finalDeEjecucion).toMillis()).concat(" segundos."));
            LOGGER.info("Tiempo de respuesta: ".concat(total));
            output.write(responseText.getBytes());
            output.flush();
            output.close();
            exchange.close();
        }));
  2. Agregamos una función llamada getParameterValue en el cual buscaremos nuestro parámetro que enviaremos por el url

        private static Optional<String> getParameterValue(Map<String, String> param, String paramName) {
         String val = param.get(paramName);
         if (val != null && val != "") {
             return Optional.ofNullable(val);
         }
         return Optional.ofNullable("NA");
     }
  3. Creamos una variable la cual será un mapa hash en donde almacenaremos todos nuestros parametros de entrada

         Map<String, String> params = splitQuery(exchange.getRequestURI());
  4. Creamos una variable de tipo Optional en la cual almacenaremos el nombre de la variable y sino existe se inicializará por defecto con un valor de NA

             Optional<String> Optionalnombre = getParameterValue(params, "name");
  5. Recorremos las cuentas y buscamos la cuenta por su nombre

    String nombre = Optionalnombre.get();
                List<BankAccountDTO> accountsFiltered = bankAccountBO.getAccounts();
                accountsFiltered.clear();
                for (int i = 0; i < accounts.size(); i++) {
                    if (accounts.get(i).getAccountName().indexOf(nombre) >= 0) {
                        accountsFiltered.add(accounts.get(i));
                        break;
                    }
                }
                JSONArray json = new JSONArray(accountsFiltered);
  6. Nuestro código debe verse de la siguiente manera:

    // Consultar todas las cuentas y buscarla por nombre utilizando Optional por si no es encontrada
        server.createContext("/api/getAccountByName", (exchange -> {
            LOGGER.info(msgProcPeticion);
            Instant inicioDeEjecucion = Instant.now();
            BankAccountBO bankAccountBO = new BankAccountBOImpl();
            String responseText = "";
            /** Validates the type of http request  */
            if ("GET".equals(exchange.getRequestMethod())) {
                LOGGER.info("LearningJava - Procesando peticion HTTP de tipo GET");
                List<BankAccountDTO> accounts = bankAccountBO.getAccounts();
                // Aquí implementaremos nuestro código de filtrar las cuentas por nombre utilizando optional
                Map<String, String> params = splitQuery(exchange.getRequestURI());
                Optional<String> Optionalnombre = getParameterValue(params, "name");
                String nombre = Optionalnombre.get();
                List<BankAccountDTO> accountsFiltered = bankAccountBO.getAccounts();
                accountsFiltered.clear();
                for (int i = 0; i < accounts.size(); i++) {
                    if (accounts.get(i).getAccountName().indexOf(nombre) >= 0) {
                        accountsFiltered.add(accounts.get(i));
                        break;
                    }
                }
                JSONArray json = new JSONArray(accountsFiltered);
                responseText = json.toString();
                exchange.getResponseHeaders().add("Content-type", "application/json");
                exchange.sendResponseHeaders(200, responseText.getBytes().length);
            } else {
                /** 405 Method Not Allowed */
                exchange.sendResponseHeaders(405, -1);
            }
            OutputStream output = exchange.getResponseBody();
            Instant finalDeEjecucion = Instant.now();
            /**
             * Always remember to close the resources you open.
             * Avoid memory leaks
             */
            LOGGER.info("LearningJava - Cerrando recursos ...");
            String total = new String(String.valueOf(Duration.between(inicioDeEjecucion, finalDeEjecucion).toMillis()).concat(" segundos."));
            LOGGER.info("Tiempo de respuesta: ".concat(total));
            output.write(responseText.getBytes());
            output.flush();
            output.close();
            exchange.close();
        }));

💻 Requests

Usando postman vamos a usar el siguiente endpoint.

localhost:8080/api/getAccountByName?name=Dummy Account 4

✅ 200 Response

    {
        "country": "France",
        "accountActive": false,
        "accountName": "Dummy Account 4",
        "accountType": "NOMINA",
        "lastUsage": "2022-10-24T21:24:49.969182",
        "accountNumber": -2960804615309273939,
        "accountBalance": 5941.990576892239,
        "creationDate": "2022-08-24T21:24:49.968520",
        "user": "[email protected]"
    }

📚 Recursos