Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres-DB-Integration #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Postgres-DB-Integration #2

wants to merge 3 commits into from

Conversation

FacuRossi
Copy link
Contributor

No description provided.

* Created by Facundo on 1/29/2018.
*/
@Configuration
public class ServiceConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

es necesaria esta clase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Es una buena practica no usar el autowired directamente en el service, despues cuando haces los UT no te levanta todo el contexto de spring. Pero sigo la forma en lo hiciste vos y lo saco leo

* Created by Facundo on 1/29/2018.
*/
@RestController
@RequestMapping("/customers")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

le pongamos "/api/customers" para seguir la misma convención de Users

ResponseEntity<List<Customer>> responseEntity;
List<Customer> customers = customerService.getCustomers();
if (customers == null || customers.isEmpty()) {
responseEntity = new ResponseEntity<>(HttpStatus.NOT_FOUND);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Es mejor devolver una lista vacía. Un NOT FOUND puede confundir con que el web service no existe

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Se podría mandar un OK o un NO_CONTENT.

ResponseEntity<Customer> responseEntity;
Customer customerStored = customerService.addCustomer(customer);
if (customerStored == null) {
responseEntity = new ResponseEntity<>(HttpStatus.NOT_FOUND);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si esto es Null en realidad debería explotar. Es un caso no esperado. InternalServerError. Sacá el If directamente

public ResponseEntity<Customer> updateCustomer(@RequestBody Customer customer) {
ResponseEntity<Customer> responseEntity;
Customer customerStored = customerService.updateCustomer(customer);
if (customerStored == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem POST

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si se puede evitar el comodin (*) en los import mejor. Evitar su uso es una buena practica, no se gana en performance pero se evita la ambiguedad de impotar clases que no se utilizan.

this.customerService = customerService;
}

@RequestMapping(method = RequestMethod.GET, produces = "application/JSON")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"application/JSON" no es el content-type estandar del iana (http://www.iana.org/assignments/media-types/media-types.xhtml). Conviene directamente usar los tipos provistos por la clase org.springframework.http.MediaType para evitar estos errores:
MediaType.APPLICATION_JSON_VALUE

}

@RequestMapping(method = RequestMethod.PUT,produces = "application/JSON")
public ResponseEntity<Customer> updateCustomer(@RequestBody Customer customer) {
Copy link
Contributor

@hvalenti hvalenti Feb 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si se envia un jsonBody con un ID que no existe, falla con un 500 en vez de un 404.
Me parece que queda mejor haciendo que la URL incluya el ID que se quiere modificar, ej: /api/customer/
Y en el request body los datos que se quieren modificar. Entre el controller/service tienen que hacerse responsables de:

  1. buscar el customer por el ID solicitado
  2. devolver 404 si no existe
  3. actualizar el customer si existe

@@ -1,2 +1,7 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Para que esto ande de una con el script "start.bat" es necesario que la URL de la BD apunte a el host "db", ej:
jdbc:postgresql://db:5432/template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants