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..92d70a9 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,49 @@ +package io.zipcoder.crudapp; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Person { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String firstName; + private String lastName; + + public Person() { + + } + + public Person(Long id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } +} 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..a28c50e --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,47 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + + +@RestController +public class PersonController { + + @Autowired + PersonRepository personRepository; + + @PostMapping("/people") + public Person createPerson(@RequestBody Person p) { + return personRepository.save(p); + } + + @GetMapping("/people/{id}") + public ResponseEntity getPerson(@PathVariable Long id) { + return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK); + } + + @GetMapping("/people") + public ResponseEntity> getPersonList() { + return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK); + } + + @PutMapping("/people/{id}") + public ResponseEntity updatePerson(@RequestBody Person person, @PathVariable Long id) { + if (person.getId() != null) { + return new ResponseEntity<>(personRepository.save(person), HttpStatus.OK); + } else { + return new ResponseEntity<>(createPerson(person), HttpStatus.OK); + } + } + + @DeleteMapping("/people/{id}") + public ResponseEntity deletePerson(@PathVariable Long id) { + personRepository.delete(id); + return new ResponseEntity<>(true,HttpStatus.NO_CONTENT); + } + +} 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..0bc7c28 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,10 @@ +package io.zipcoder.crudapp; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PersonRepository extends CrudRepository { + + +}