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..0094937 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,48 @@ +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) + Long id; + String firstName; + 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..f295ece --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,39 @@ +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.*; + +@RestController +@RequestMapping("/person") +public class PersonController { + + @Autowired + PersonService personService; + + @PostMapping("/create") + public ResponseEntity create(@RequestBody Person person) { + return new ResponseEntity<>(personService.create(person), HttpStatus.CREATED); + } + + @GetMapping("/people") + public ResponseEntity> findAll() { + return new ResponseEntity<>(personService.findAll(), HttpStatus.OK); + } + + @GetMapping("/{id}") + public ResponseEntity findById(@PathVariable Long id) { + return new ResponseEntity<>(personService.findById(id), HttpStatus.OK); + } + + @PutMapping("/update/{id}") + public ResponseEntity update(@PathVariable Long id, @RequestBody Person person) { + return new ResponseEntity<>(personService.update(id, person), HttpStatus.OK); + } + + @DeleteMapping("/delete/{id}") + public ResponseEntity deleteById(@PathVariable Long id) { + return new ResponseEntity<>(personService.deleteById(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..3ed2ba7 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,9 @@ +package io.zipcoder.crudapp; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +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..480bb3f --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,39 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class PersonService { + + @Autowired + PersonRepository repository; + + public Person create(Person person) { + return repository.save(person); + } + + public Iterable findAll() { + return repository.findAll(); + } + + public Person findById(Long id) { + return repository.findOne(id); + } + + public Person update(Long id, Person newPersonData) { + Person person = repository.findOne(id); + person.setFirstName(newPersonData.getFirstName()); + person.setLastName(newPersonData.getLastName()); + return repository.save(person); + } + + public Person deleteById(Long id) { + Person person = repository.findOne(id); + repository.delete(id); + return person; + } + +}