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..9e6e7d0 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 @@ -4,9 +4,11 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import java.io.IOException; +@EnableEurekaServer @SpringBootApplication public class DiscoveryServer { 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..9c040b5 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 @@ -1,5 +1,6 @@ package com.eureka.persons; +import com.eureka.persons.ex.NotFoundException; import com.eureka.persons.person.Person; import com.eureka.persons.services.PersonService; import org.springframework.http.HttpStatus; @@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; @RestController @@ -26,7 +28,15 @@ public PersonsController(PersonService personService) { @ResponseStatus(HttpStatus.OK) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List list() { - return new ArrayList<>(); + List people = personService.findAll(); + people.sort(new Comparator() { + @Override + public int compare(Person p1, Person p2) { + return p1.getId().compareTo(p2.getId()); + } + }); + + return people; } /** @@ -36,6 +46,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, "There was an error."); + } else { + personService.save(person); + } } /** @@ -48,7 +63,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 +77,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 +97,7 @@ 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..3ac1560 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,9 +5,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import java.io.IOException; +@EnableEurekaServer @EntityScan(basePackages = "com.eureka.persons") @SpringBootApplication public class PersonsServer { diff --git a/eureka/persons-server/src/main/resources/persons-server.yml b/eureka/persons-server/src/main/resources/persons-server.yml index b2b466d..4fd1f3e 100644 --- a/eureka/persons-server/src/main/resources/persons-server.yml +++ b/eureka/persons-server/src/main/resources/persons-server.yml @@ -26,6 +26,12 @@ server: # Discovery Server Access #TODO here you add configurations for eureka client +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + fetchRegistry: true + info: app: name: persons-server diff --git a/lab-6-api-gateway/service1/pom.xml b/lab-6-api-gateway/service1/pom.xml index 478fe3f..cc446b1 100644 --- a/lab-6-api-gateway/service1/pom.xml +++ b/lab-6-api-gateway/service1/pom.xml @@ -18,10 +18,10 @@ 2021.0.1 - + org.springframework.boot spring-boot-starter-actuator diff --git a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java index 27128b5..69ffa10 100644 --- a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java +++ b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java @@ -2,14 +2,24 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication +@EnableEurekaClient +@RequestMapping("api/greeting") public class Service1Application { public static void main(String[] args) { SpringApplication.run(Service1Application.class, args); } + @GetMapping + public String getGreeting(String name) { + return "Hello " + name; + } + // TODO // 1. define a GET endpoint /api/greeting which should accept a query parameter "name" // 2. return should be a string returning a greeting: Hello Brasov diff --git a/lab-6-api-gateway/service1/src/main/resources/application.yml b/lab-6-api-gateway/service1/src/main/resources/application.yml index 54b155f..8fcf20c 100644 --- a/lab-6-api-gateway/service1/src/main/resources/application.yml +++ b/lab-6-api-gateway/service1/src/main/resources/application.yml @@ -1,2 +1,9 @@ +spring: + application: + name: spring-cloud-eureka-client server: - port: 8081 \ No newline at end of file + port: 8081 +eureka: + client: + serviceUrl: + defaultZone: ${EUREKA_URI:http://localhost:3000/eureka} \ No newline at end of file