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 All @@ -22,4 +24,4 @@ public static void main(String... args) throws IOException {
System.in.read();
ctx.close();
}
}
}
12 changes: 9 additions & 3 deletions eureka/discovery-server/src/main/resources/discovery-server.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
spring:
application:
name: discovery-service
# Configure this Discovery Server
#TODO here you add configurations for server

eureka:
client:
register-with-eureka: false
fetch-registry: false

server:
port: 3000

logging:
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
level:
root: INFO
org.springframework: DEBUG
com.apress.cems: DEBUG
com.apress.cems: DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ void contextLoads() {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ public interface PersonRepo extends JpaRepository<Person, Long> {
@Query("select p from Person p where p.hiringDate=:hd")
List<Person> findByHiringDate(@Param("hd") LocalDateTime date);
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.eureka.persons;

import com.eureka.persons.person.Person;
import com.eureka.persons.services.PersonService;
import java.util.Comparator;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import com.eureka.persons.ex.NotFoundException;
import com.eureka.persons.person.Person;
import com.eureka.persons.services.PersonService;

@RestController
@RequestMapping("/persons")
Expand All @@ -26,7 +36,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 +54,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 +71,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 +85,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 +105,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,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.EnableEurekaClient;

import java.io.IOException;

@EnableEurekaClient
@EntityScan(basePackages = "com.eureka.persons")
@SpringBootApplication
public class PersonsServer {
Expand All @@ -24,4 +26,4 @@ public static void main(String... args) throws IOException {
System.in.read();
ctx.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.eureka.persons.base;

import com.eureka.persons.util.DateProcessor;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.springframework.format.annotation.DateTimeFormat;

import com.eureka.persons.util.DateProcessor;
import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Getter;
import lombok.Setter;

@MappedSuperclass
@Getter
@Setter
Expand All @@ -24,6 +32,14 @@ public abstract class AbstractEntity implements Serializable {
@Column(updatable = false)
protected Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Version
protected int version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,58 @@ public String toString() {
return String.format("Person[username='%s', firstName='%s', lastName='%s', hiringDate='%s']\n",
username, firstName, lastName, hiringDate.toString());



}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public LocalDateTime getHiringDate() {
return hiringDate;
}

public void setHiringDate(LocalDateTime hiringDate) {
this.hiringDate = hiringDate;
}

public String getNewPassword() {
return newPassword;
}

public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ public void delete(Person person) {
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ private NumberGenerator() {
// prevent initialization fo this class
}
}

9 changes: 8 additions & 1 deletion 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 All @@ -36,6 +42,7 @@ logging:
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
level:
root: INFO
root: DEBUG
org.springframework: DEBUG
com.apress.cems: DEBUG

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}

}
}