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..ec081f7 --- /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) + 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/PersonConfiguration.java b/src/main/java/io/zipcoder/crudapp/PersonConfiguration.java new file mode 100644 index 0000000..06ce2fc --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonConfiguration.java @@ -0,0 +1,19 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; + +@Configuration +public class PersonConfiguration { + @Autowired + private PersonService service; + + @PostConstruct + public void setup() { + service.create(new Person()); + service.create(new Person()); + service.create(new Person()); + } +} 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..0bee600 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,41 @@ +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 +@RequestMapping(value = "/person-controller") // serves up on localhost:8080/person-controller +public class PersonController { + @Autowired + private PersonService service; + + @PostMapping(value = "/create") + // exposed at localhost:8080/person-controller/create + public ResponseEntity create(@RequestBody Person person) { // `@RequestBody` allows us to inject JSON objects (representative of POJO instances) into our Spring server from our front-end client (mocked by Postman) + return new ResponseEntity<>(service.create(person), HttpStatus.CREATED); + } + + @GetMapping(value = "/read/{id}") + public ResponseEntity read(@PathVariable Long id) { + return new ResponseEntity<>(service.read(id), HttpStatus.OK); + } + + @GetMapping(value = "/read") + public ResponseEntity> readAll() { + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + @PutMapping(value = "/update/{id}") + public ResponseEntity update(@PathVariable Long id, @RequestBody Person newPersonData) { + return new ResponseEntity<>(service.update(id, newPersonData), HttpStatus.OK); + } + + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity delete(@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..1195706 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,6 @@ +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..aaa096e --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,40 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class PersonService { + @Autowired + private PersonRepository repository; + + public Person create(Person person) { return repository.save(person); } + + public Person read(Long id) { return repository.findOne(id); } + + public List readAll() { + Iterable personIterable = repository.findAll(); + List result = new ArrayList<>(); + personIterable.forEach(result::add); + return result; + } + + public Person update(Long id, Person newPersonData){ + Person personInDatabase = read(id); + personInDatabase.setFirstName(newPersonData.getFirstName()); + personInDatabase.setLastName(newPersonData.getLastName()); + return repository.save(personInDatabase); + } + + public Person delete(Person person){ + repository.delete(person); + return person; + } + + public Person delete(Long id){ + return delete(read(id)); + } +}