Skip to content

Latest commit

 

History

History
155 lines (145 loc) · 7.01 KB

README.md

File metadata and controls

155 lines (145 loc) · 7.01 KB

Genericos

🛠️ Requisitos

📝 Actividad

Buscar todas las cuentas creadas por un usuario usando Genéricos

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

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

    // Consultar todas las cuentas y filtrarlas por usuario
        server.createContext("/api/getAccountsByUser", (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();
                List<BankAccountDTO> accountsFiltered = bankAccountBO.getAccounts();
                accountsFiltered.clear();
    
                // Aquí implementaremos nuestro código de filtrar las cuentas por usuario usando genericos
               
                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();
        }));
  2. Creamos una función llamada getParameterValueObject la cual regresará cualquier tipo de valor que tengamos como parametro de entrada por la url

         private static Optional<Object> getParameterValueObject(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<Object> Optionaluser= getParameterValueObject(params, "user");
  5. Recorremos las cuentas y buscamos la cuenta por usuario

                Object user = Optionaluser.get();
                for (int i = 0; i < accounts.size(); i++) {
                    if (accounts.get(i).getUser().indexOf(user.toString()) >= 0) {
                        accountsFiltered.add(accounts.get(i));
                    }
                }
  6. Nuestro código debe verse de la siguiente manera:

    // Consultar todas las cuentas y filtrarlas por usuario
        server.createContext("/api/getAccountsByUser", (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();
                List<BankAccountDTO> accountsFiltered = bankAccountBO.getAccounts();
                accountsFiltered.clear();
    
                // Aquí implementaremos nuestro código de filtrar las cuentas por usuario
                Map<String, String> params = splitQuery(exchange.getRequestURI());
                Optional<Object> Optionaluser = getParameterValueObject(params, "user");
                Object user = Optionaluser.get();
               for (int i = 0; i < accounts.size(); i++) {
                    if (accounts.get(i).getUser().indexOf(user.toString()) >= 0) {
                        accountsFiltered.add(accounts.get(i));
                    }
                }
                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();
        }));
  7. 💻 Requests

    Usando postman vamos a usar el siguiente endpoint.

    localhost:8080/api/getAccountsByUser?[email protected]

    ✅ 200 Response

        {
            "country": "Mexico",
            "accountActive": true,
            "accountName": "Dummy Account 8",
            "accountType": "AHORRO",
            "lastUsage": "2022-10-24T21:59:31.362217",
            "accountNumber": -3842697179278231263,
            "accountBalance": 8684.415704926496,
            "creationDate": "2022-10-17T21:59:31.361696",
            "user": "[email protected]"
        }

    📚 Recursos