From 415749cbc14476d0ed0ee4344b4684ac5d32635c Mon Sep 17 00:00:00 2001 From: MannyMb Date: Fri, 13 Aug 2021 23:47:09 -0400 Subject: [PATCH 1/2] fin --- pom.xml | 5 ++ src/main/java/io/zipcoder/crudapp/Person.java | 48 +++++++++++++++++ .../io/zipcoder/crudapp/PersonConfig.java | 19 +++++++ .../io/zipcoder/crudapp/PersonController.java | 53 +++++++++++++++++++ .../io/zipcoder/crudapp/PersonRepository.java | 9 ++++ .../io/zipcoder/crudapp/PersonService.java | 48 +++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 src/main/java/io/zipcoder/crudapp/Person.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonConfig.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/pom.xml b/pom.xml index 558c7a1..26921e6 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,11 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-war-plugin + 3.2.2 + 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/PersonConfig.java b/src/main/java/io/zipcoder/crudapp/PersonConfig.java new file mode 100644 index 0000000..248a358 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonConfig.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 // classes that run before the application is served +public class PersonConfig { + + @Autowired + private PersonService service; + + @PostConstruct + public void setup() { + service.create(new Person(10L, "Manny", "Mbanefo")); + 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..5701db3 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,53 @@ +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") +public class PersonController { // they wrap it up with a response entity and decorate the methods with annotations to expose them as endpoints + @Autowired + private PersonService service; + + @RequestMapping(method = RequestMethod.POST, value = "/create") + public ResponseEntity create(@RequestBody Person person) { + return new ResponseEntity<>(service.create(person), HttpStatus.CREATED); + } + + @RequestMapping(method = RequestMethod.GET, value = "/read/{id}") + public ResponseEntity read(@PathVariable Long id) { + if(service.read(id) != null) { + return new ResponseEntity<>(service.read(id), HttpStatus.OK); + } + return new ResponseEntity<>(service.read(id), HttpStatus.NOT_FOUND); + } + + @RequestMapping(method = RequestMethod.GET, value = "/readAll") + public ResponseEntity> readAll() { + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.PUT, value = "/update/{id}") + public ResponseEntity update( + @PathVariable Long id, + @RequestBody Person newPersonData) { + if(service.read(id) != null) { + return new ResponseEntity<>(service.update(id, newPersonData), HttpStatus.OK); + } return new ResponseEntity<>(service.create(newPersonData), HttpStatus.CREATED); + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/delete/{id}") + public ResponseEntity delete(@PathVariable Long id) { + return new ResponseEntity<>(service.delete(id), HttpStatus.NO_CONTENT); + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/delete") + public ResponseEntity delete(Person person) { + return new ResponseEntity<>(service.delete(person), HttpStatus.NO_CONTENT); + } + +} 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..535b0d1 --- /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 { + +} \ No newline at end of file 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..57fa6b1 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,48 @@ +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); + //.get(); + } + + 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 personInDd = read(id); + personInDd.setFirstName(newPersonData.getFirstName()); + personInDd.setLastName(newPersonData.getLastName()); + personInDd = repository.save(personInDd); + return personInDd; + } + + public Person delete(Long id) { + Person personInDb = read(id); + repository.delete(personInDb); + return personInDb; + } + + public Person delete(Person person) { + return delete(person.getId()); + } + +} From bf0b2c8d1a9a91f0eb65d078001613698997e266 Mon Sep 17 00:00:00 2001 From: MannyMb Date: Sat, 14 Aug 2021 00:09:36 -0400 Subject: [PATCH 2/2] testing --- src/main/java/io/zipcoder/crudapp/PersonController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/zipcoder/crudapp/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java index 5701db3..50ed917 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonController.java +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -49,5 +49,5 @@ public ResponseEntity delete(@PathVariable Long id) { public ResponseEntity delete(Person person) { return new ResponseEntity<>(service.delete(person), HttpStatus.NO_CONTENT); } - } +