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..242938c --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,50 @@ +package io.zipcoder.crudapp; +import javax.persistence.*; + + +@Entity +public class Person { + @Id + @GeneratedValue + 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 Person(String firstName, String lastName) { + this(null, firstName, 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..28942d8 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,48 @@ +package io.zipcoder.crudapp; + +import org.springframework.stereotype.Controller; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.beans.factory.annotation.Autowired; + + +@Controller +public class PersonController { + + private PersonService service; + + @Autowired + public PersonController(PersonService service) { + this.service = service;} + + @GetMapping("/people") + public ResponseEntity> showAll() { + return new ResponseEntity<>(service.showAll(), HttpStatus.OK); + } + + @GetMapping("/people/{id}") + public ResponseEntity show(@PathVariable Long id) { + return new ResponseEntity<>(service.show(id), HttpStatus.OK); + } + + @PostMapping("/people") + public ResponseEntity create(@RequestBody Person person) { + return new ResponseEntity<>(service.create(person), HttpStatus.CREATED); + } + + @PutMapping("/people/{id}") + public ResponseEntity update(@PathVariable Long id, @RequestBody Person person) { + return new ResponseEntity<>(service.update(id, person), HttpStatus.OK); + } + + @DeleteMapping("/people/{id}") + public ResponseEntity destroy(@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..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..ce17b82 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,33 @@ +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 showAll() { + return repository.findAll();} + + public Person show(Long id){ + return repository.findOne(id);} //todo something to do with get method + + public Person create(Person person) { + return repository.save(person);} + + public Person update(Long id, Person newPersonData) { + Person originalPerson = repository.findOne(id); //todo something to do with get method + originalPerson.setFirstName(newPersonData.getFirstName()); + originalPerson.setLastName(newPersonData.getLastName()); + return repository.save(originalPerson); + } + + public Boolean delete(Long id) { + repository.delete(id); + return true; + } +}