From f16f2328c3c4b98f9b6ae358d96c379dc6094ddd Mon Sep 17 00:00:00 2001 From: andyradoi22 Date: Sun, 10 Apr 2022 20:56:52 +0300 Subject: [PATCH] Tema 5 - Radoi Andrei --- .../example/discovery/DiscoveryServer.java | 2 ++ .../src/main/resources/discovery-server.yml | 6 ++++ .../com/eureka/persons/PersonsController.java | 31 +++++++++++++++++-- .../com/eureka/persons/PersonsServer.java | 2 ++ .../src/main/resources/persons-server.yml | 8 ++++- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java index 15d783c..bece018 100644 --- a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java +++ b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java @@ -6,8 +6,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.IOException; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication +@EnableEurekaServer public class DiscoveryServer { private static Logger logger = LoggerFactory.getLogger(DiscoveryServer.class); diff --git a/eureka/discovery-server/src/main/resources/discovery-server.yml b/eureka/discovery-server/src/main/resources/discovery-server.yml index b55b43b..153b5b2 100644 --- a/eureka/discovery-server/src/main/resources/discovery-server.yml +++ b/eureka/discovery-server/src/main/resources/discovery-server.yml @@ -3,6 +3,12 @@ spring: name: discovery-service # Configure this Discovery Server #TODO here you add configurations for server +server: + port: 3000 +eureka: + client: + registerWithEureka: false + fetchRegistry: false logging: pattern: diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java index ff4acb5..fd5f6b0 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java @@ -6,9 +6,11 @@ import org.springframework.http.MediaType; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import com.eureka.persons.ex.NotFoundException; import java.util.ArrayList; import java.util.List; +import java.util.Comparator; @RestController @RequestMapping("/persons") @@ -26,7 +28,17 @@ public PersonsController(PersonService personService) { @ResponseStatus(HttpStatus.OK) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List list() { - return new ArrayList<>(); + List list = personService.findAll(); + + list.sort(new Comparator() { + @Override + public int compare(Person o1, Person o2) + { + return o1.getId().compareTo(o2.getId()); + } + }); + + return list; } /** @@ -36,6 +48,11 @@ public List list() { @ResponseStatus(HttpStatus.CREATED) @PostMapping public void create(@RequestBody Person person, BindingResult result) { + if(result.hasErrors()){ + throw new PersonsException(HttpStatus.BAD_REQUEST, "Something went wrong!"); + } else{ + personService.save(person); + } } /** @@ -48,7 +65,7 @@ public void create(@RequestBody Person person, BindingResult result) { @ResponseStatus(HttpStatus.OK) @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public Person show(@PathVariable Long id) { - return new Person(); + return personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)); } /** @@ -62,6 +79,15 @@ public Person show(@PathVariable Long id) { @ResponseStatus(HttpStatus.NO_CONTENT) @PutMapping("/{id}") public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { + Person person = personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)); + + person.setUsername(updatedPerson.getUsername()); + person.setFirstName(updatedPerson.getFirstName()); + person.setLastName(updatedPerson.getLastName()); + person.setPassword(updatedPerson.getPassword()); + person.setHiringDate(updatedPerson.getHiringDate()); + person.setNewPassword(updatedPerson.getNewPassword()); + personService.save(person); } /** @@ -73,5 +99,6 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { + personService.delete(personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id))); } } \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java index f2098ae..c23be15 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java @@ -5,11 +5,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import java.io.IOException; @EntityScan(basePackages = "com.eureka.persons") @SpringBootApplication +@EnableEurekaClient public class PersonsServer { private static Logger logger = LoggerFactory.getLogger(PersonsServer.class); diff --git a/eureka/persons-server/src/main/resources/persons-server.yml b/eureka/persons-server/src/main/resources/persons-server.yml index b2b466d..db27074 100644 --- a/eureka/persons-server/src/main/resources/persons-server.yml +++ b/eureka/persons-server/src/main/resources/persons-server.yml @@ -25,7 +25,13 @@ server: # Discovery Server Access #TODO here you add configurations for eureka client - +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + healthcheck: + enabled: true + info: app: name: persons-server