diff --git a/pom.xml b/pom.xml index 558c7a1..3ce048c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.2.RELEASE + 2.5.3 @@ -22,8 +22,10 @@ UTF-8 UTF-8 1.8 - + + + org.springframework.boot @@ -50,7 +52,15 @@ spring-boot-starter-data-jpa - + + junit + junit + test + + + + + 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..c444b65 --- /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 Integer id; + private String firstName; + private String lastName; + + public Person() { + } + + public Person(Integer id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public Integer getId() { + return id; + } + + public void setId(Integer 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..a07ddbf --- /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.ArrayList; +import java.util.List; + +@RestController +@RequestMapping(value = "/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 = "/getPerson/{id}") + public ResponseEntity getPerson(@PathVariable Integer id){ + return new ResponseEntity<>(service.read(id),HttpStatus.OK); + } + @GetMapping(value = "/findAll") + public ResponseEntity> getPersonList(){ + return new ResponseEntity<>(service.readAll(),HttpStatus.OK); + } + + @PutMapping(value = "/update/{id}") + public ResponseEntity updatePerson(@PathVariable Integer id, @RequestBody Person newPersonData){ + return new ResponseEntity<>(service.update(id,newPersonData),HttpStatus.OK); + } + + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity DeletePerson(@PathVariable Integer 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..37f0cc5 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,8 @@ +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..528ba96 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,44 @@ +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(Integer id) { + return repository.findById(id).get(); + } + + public List readAll() { + Iterable personIterable = repository.findAll(); + List result = new ArrayList<>(); + personIterable.forEach(result::add); + return result; + } + + public Person update(Integer 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(Integer id) { + return delete(read(id)); + } +} diff --git a/src/main/resources/application-h2.properties b/src/main/resources/application-h2.properties index 74765cc..62026d7 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -1,4 +1,6 @@ spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle spring.datasource.platform=h2 spring.jpa.hibernate.ddl-auto=none -spring.datasource.continue-on-error=true \ No newline at end of file +spring.datasource.continue-on-error=true +spring.h2.console.enabled=true +spring.h2.console.view=/h2-console \ No newline at end of file