diff --git a/src/main/java/io/zipcoder/crudapp/Person.java b/src/main/java/io/zipcoder/crudapp/Person.java new file mode 100644 index 0000000..a783387 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,74 @@ +package io.zipcoder.crudapp; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Objects; + + +@Entity +public class Person { + + + @Id @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + + private String firstName; + private String lastName; + + public Person() { + + } + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + + } + + 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 Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return Objects.equals(id, person.id) && + Objects.equals(firstName, person.firstName) && + Objects.equals(lastName, person.lastName); + } + + @Override + public int hashCode() {return Objects.hash(id, firstName, lastName); } + + @Override + public String toString() { + return "Person{" + " id = " + this.id + ", first name ='" + this.firstName + '\'' + + ", last name'" + this.lastName + '\'' + '}'; + } + +} diff --git a/src/main/java/io/zipcoder/crudapp/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java new file mode 100644 index 0000000..7f8b7d2 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,43 @@ +package io.zipcoder.crudapp; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +public class PersonController { + + private PersonService service; + + + public PersonController(PersonService service) { + this.service = service; + } + @GetMapping("/people") + public ResponseEntity> getPersonList() { + return new ResponseEntity<>(service.index(), HttpStatus.OK); + } + @GetMapping("/people/{id}") + public ResponseEntity getPerson(@PathVariable Long id) { + return new ResponseEntity<>(service.show(id), HttpStatus.OK); + } + @PostMapping("/people") + public ResponseEntity createPerson (Person person) { + return new ResponseEntity<>(service.create(person), HttpStatus.CREATED); + } + @PutMapping("/people/{id}") + public ResponseEntity updatePerson(@PathVariable Long id, Person person) { + return new ResponseEntity<>(service.update(id, person), HttpStatus.OK); + } + @DeleteMapping("/people/{id}") + public ResponseEntity deletePerson(@PathVariable Long id) { + return new ResponseEntity<>(service.delete(id), HttpStatus.OK); + } + + + + + + + +} diff --git a/src/main/java/io/zipcoder/crudapp/PersonRepository.java b/src/main/java/io/zipcoder/crudapp/PersonRepository.java new file mode 100644 index 0000000..978d237 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,7 @@ +package io.zipcoder.crudapp; + +import org.springframework.data.repository.CrudRepository; + +public interface PersonRepository extends CrudRepository { + +} diff --git a/src/main/java/io/zipcoder/crudapp/PersonService.java b/src/main/java/io/zipcoder/crudapp/PersonService.java new file mode 100644 index 0000000..31e0601 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,30 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +@Service +public class PersonService { + @Autowired + private PersonRepository repository; + + public PersonService(PersonRepository repository) {this.repository = repository;} + + public Iterable index() {return repository.findAll();} + + public Person show(Long id) {return repository.findOne(id);} + + public Person create (Person person) {return repository.save(person);} + + public Person update(Long id, Person newPersonData) { + Person originalPerson = repository.findOne(id); + originalPerson.setFirstName(newPersonData.getFirstName()); + originalPerson.setLastName(newPersonData.getLastName()); + return repository.save(originalPerson); + } + + public Boolean delete(Long id) { + repository.delete(id); + return true; + } + +}