Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +9,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
Expand All @@ -26,7 +28,15 @@ public PersonsController(PersonService personService) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> people = personService.findAll();
people.sort(new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getId().compareTo(p2.getId());
}
});

return people;
}

/**
Expand All @@ -36,6 +46,11 @@ public List<Person> 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);
}
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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)));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions eureka/persons-server/src/main/resources/persons-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lab-6-api-gateway/service1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencies>
<!-- <dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
spring:
application:
name: spring-cloud-eureka-client
server:
port: 8081
port: 8081
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:3000/eureka}