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 @@ -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);
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
Expand Up @@ -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")
Expand All @@ -26,7 +28,17 @@ public PersonsController(PersonService personService) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> list = personService.findAll();

list.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2)
{
return o1.getId().compareTo(o2.getId());
}
});

return list;
}

/**
Expand All @@ -36,6 +48,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, "Something went wrong!");
} else{
personService.save(person);
}
}

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

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

/**
Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion eureka/persons-server/src/main/resources/persons-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down