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..460d747 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 { @@ -22,4 +24,4 @@ public static void main(String... args) throws IOException { System.in.read(); ctx.close(); } -} +} \ No newline at end of file diff --git a/eureka/discovery-server/src/main/resources/discovery-server.yml b/eureka/discovery-server/src/main/resources/discovery-server.yml index b55b43b..df053f2 100644 --- a/eureka/discovery-server/src/main/resources/discovery-server.yml +++ b/eureka/discovery-server/src/main/resources/discovery-server.yml @@ -1,8 +1,14 @@ 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: @@ -10,4 +16,4 @@ logging: level: root: INFO org.springframework: DEBUG - com.apress.cems: DEBUG + com.apress.cems: DEBUG \ No newline at end of file diff --git a/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java b/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java index f102053..7f8ac34 100644 --- a/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java +++ b/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java @@ -11,3 +11,4 @@ void contextLoads() { } } + diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java index 5e7b169..20bd699 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java @@ -34,3 +34,4 @@ public interface PersonRepo extends JpaRepository { @Query("select p from Person p where p.hiringDate=:hd") List findByHiringDate(@Param("hd") LocalDateTime date); } + 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..9e3808e 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,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") @@ -26,7 +36,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 +54,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 +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)); } /** @@ -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); } /** @@ -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))); } } \ 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..095cc7f 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.EnableEurekaClient; import java.io.IOException; +@EnableEurekaClient @EntityScan(basePackages = "com.eureka.persons") @SpringBootApplication public class PersonsServer { @@ -24,4 +26,4 @@ public static void main(String... args) throws IOException { System.in.read(); ctx.close(); } -} +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java index 199103b..448a274 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java @@ -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 @@ -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; diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java index 9064df8..045341a 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java @@ -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; + } + + + +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java index f1c2fec..bf06307 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java @@ -40,3 +40,4 @@ public void delete(Person person) { } } + diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java b/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java index 92b1c40..8067ce1 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java @@ -16,3 +16,4 @@ private NumberGenerator() { // prevent initialization fo this 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..d18f8fe 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 @@ -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 + diff --git a/eureka/src/main/java/com/example/eureka/EurekaApplication.java b/eureka/src/main/java/com/example/eureka/EurekaApplication.java index 20c67a9..738ee28 100644 --- a/eureka/src/main/java/com/example/eureka/EurekaApplication.java +++ b/eureka/src/main/java/com/example/eureka/EurekaApplication.java @@ -10,4 +10,4 @@ public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } -} +} \ No newline at end of file