From fe7392fed63b1ffa886e3df1da194f4261e69e61 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Thu, 5 Dec 2019 23:36:50 -0500 Subject: [PATCH 1/3] need to work on delete controller --- src/main/java/io/zipcoder/crudapp/Person.java | 53 +++++++++++++++++++ .../io/zipcoder/crudapp/PersonController.java | 48 +++++++++++++++++ .../io/zipcoder/crudapp/PersonRepository.java | 8 +++ .../io/zipcoder/crudapp/PersonService.java | 38 +++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 src/main/java/io/zipcoder/crudapp/Person.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonController.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonRepository.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonService.java 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..ea26599 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,53 @@ +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) + Integer id; + String firstName; + String lastName; + + public Person () {} + + public Person(Integer id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public Person(String firstName, String lastName) { + 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..3aa61b8 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,48 @@ +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; + +@RestController +public class PersonController { + private PersonService service; + + @Autowired + public PersonController(PersonService service) { + this.service = service; + } + + @PostMapping("/people") + public ResponseEntity createPerson (@RequestBody Person person){ + return new ResponseEntity<>(service.createPerson(person), HttpStatus.CREATED); + } + + @GetMapping("/people/{id}") + public ResponseEntity getPerson (@PathVariable Integer id){ + return new ResponseEntity<>(service.findOnePerson(id), HttpStatus.OK); + } + + @GetMapping("/people") + public ResponseEntity> getPersonList () { + return new ResponseEntity<>(service.findAllPeople(), HttpStatus.OK); + } + + + @PutMapping("/people") + public ResponseEntity updatePerson (@RequestBody Person person){ + return new ResponseEntity<>(service.updatePerson(person), HttpStatus.OK); + } + + @DeleteMapping("/people/{id}") + public ResponseEntity deletePerson(@PathVariable Integer id){ + return new ResponseEntity<>(service.deletePerson(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..16bb2fa --- /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.Component; + +@Component +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..835ebc2 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,38 @@ +package io.zipcoder.crudapp; + + +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + + private PersonRepository repository; + //private Person person; + + public PersonService(PersonRepository repository) { + this.repository = repository; + } + + public Iterable findAllPeople () { + return repository.findAll(); + } + + public Person findOnePerson (Integer id){ + return repository.findOne(id); + } + + public Person createPerson (Person person){ + return repository.save(person); + } + + public Person updatePerson (Person person) { + Person personInDB = repository.findOne(person.getId()); + personInDB.setFirstName(person.getFirstName()); + personInDB.setLastName(person.getLastName()); + return repository.save(personInDB); + } + + public void deletePerson (Integer id) { + repository.delete(id); + } +} From f47b655d9ba71a690384f36c9bd82d3978726d68 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Fri, 6 Dec 2019 14:32:10 -0500 Subject: [PATCH 2/3] all classes made and postman tests run --- .../io/zipcoder/crudapp/PersonController.java | 14 +++---------- .../io/zipcoder/crudapp/PersonRepository.java | 3 ++- .../io/zipcoder/crudapp/PersonService.java | 20 +++++++++---------- .../crudapp/CRUDApplicationTests.java | 16 --------------- 4 files changed, 15 insertions(+), 38 deletions(-) delete mode 100644 src/test/java/io/zipcoder/crudapp/CRUDApplicationTests.java diff --git a/src/main/java/io/zipcoder/crudapp/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java index 3aa61b8..89ea468 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonController.java +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -3,18 +3,15 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; -@RestController +@Controller public class PersonController { - private PersonService service; - @Autowired - public PersonController(PersonService service) { - this.service = service; - } + private PersonService service; @PostMapping("/people") public ResponseEntity createPerson (@RequestBody Person person){ @@ -31,7 +28,6 @@ public ResponseEntity> getPersonList () { return new ResponseEntity<>(service.findAllPeople(), HttpStatus.OK); } - @PutMapping("/people") public ResponseEntity updatePerson (@RequestBody Person person){ return new ResponseEntity<>(service.updatePerson(person), HttpStatus.OK); @@ -40,9 +36,5 @@ public ResponseEntity updatePerson (@RequestBody Person person){ @DeleteMapping("/people/{id}") public ResponseEntity deletePerson(@PathVariable Integer id){ return new ResponseEntity<>(service.deletePerson(id), HttpStatus.OK); - - } - - } diff --git a/src/main/java/io/zipcoder/crudapp/PersonRepository.java b/src/main/java/io/zipcoder/crudapp/PersonRepository.java index 16bb2fa..7942670 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonRepository.java +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -2,7 +2,8 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Repository; -@Component +@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 index 835ebc2..c24c2a4 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonService.java +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -1,17 +1,15 @@ package io.zipcoder.crudapp; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PersonService { - private PersonRepository repository; - //private Person person; + @Autowired + private PersonRepository repository; - public PersonService(PersonRepository repository) { - this.repository = repository; - } public Iterable findAllPeople () { return repository.findAll(); @@ -26,13 +24,15 @@ public Person createPerson (Person person){ } public Person updatePerson (Person person) { - Person personInDB = repository.findOne(person.getId()); - personInDB.setFirstName(person.getFirstName()); - personInDB.setLastName(person.getLastName()); - return repository.save(personInDB); +// Person personInDB = repository.findOne(person.getId()); +// personInDB.setFirstName(person.getFirstName()); +// personInDB.setLastName(person.getLastName()); +// return repository.save(personInDB); + return repository.save(person); } - public void deletePerson (Integer id) { + public Boolean deletePerson (Integer id) { repository.delete(id); + return true; } } diff --git a/src/test/java/io/zipcoder/crudapp/CRUDApplicationTests.java b/src/test/java/io/zipcoder/crudapp/CRUDApplicationTests.java deleted file mode 100644 index c911c8c..0000000 --- a/src/test/java/io/zipcoder/crudapp/CRUDApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.zipcoder.crudapp; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class CRUDApplicationTests { - - @Test - public void contextLoads() { - } - -} From 9de251e7c29eaee347c619ba047f2b0ab82b4426 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Wed, 11 Dec 2019 21:32:15 -0500 Subject: [PATCH 3/3] update --- pom.xml | 2 +- .../java/io/zipcoder/crudapp/PersonController.java | 8 +++++++- src/main/java/io/zipcoder/crudapp/PersonService.java | 11 ++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 558c7a1..8953703 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.2.RELEASE + 2.1.2.RELEASE diff --git a/src/main/java/io/zipcoder/crudapp/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java index 89ea468..8627cd7 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonController.java +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -10,9 +10,15 @@ @Controller public class PersonController { - @Autowired + + private PersonService service; + @Autowired + private PersonController(PersonService service){ + this.service = service; + } + @PostMapping("/people") public ResponseEntity createPerson (@RequestBody Person person){ return new ResponseEntity<>(service.createPerson(person), HttpStatus.CREATED); diff --git a/src/main/java/io/zipcoder/crudapp/PersonService.java b/src/main/java/io/zipcoder/crudapp/PersonService.java index c24c2a4..2377380 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonService.java +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -7,16 +7,21 @@ @Service public class PersonService { - @Autowired + private PersonRepository repository; + @Autowired + public PersonService(PersonRepository repository){ + this.repository = repository; + } + public Iterable findAllPeople () { return repository.findAll(); } public Person findOnePerson (Integer id){ - return repository.findOne(id); + return repository.findById(id).get(); } public Person createPerson (Person person){ @@ -32,7 +37,7 @@ public Person updatePerson (Person person) { } public Boolean deletePerson (Integer id) { - repository.delete(id); + repository.deleteById(id); return true; } }