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..81a1b98 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,49 @@ +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..3b6be33 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonConfig.java @@ -0,0 +1,21 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; + +@Configuration +public class PersonConfig { + @Autowired + private PersonService service; + + @PostConstruct + public void setup(){ + service.create(new Person(1L, "Raymond","Fitzgerald")); + service.create(new Person(2L, "Stephanie","Fitzgerald")); + service.create(new Person(3L, "Kelly", "Fitzgerald")); + service.create(new Person(4L, "Beans","Fitzgerald")); + } + +} 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..4c08d2d --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,56 @@ +package io.zipcoder.crudapp; + +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.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.List; + +@Controller +@RequestMapping(value = "/person_controller") +public class PersonController { + @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 = "/readAll") + public ResponseEntity> readAll(){ + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + @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); + } else{ + return new ResponseEntity<>(service.read(id), HttpStatus.NOT_FOUND); + } + } + + @RequestMapping(method = RequestMethod.PUT, value = "/update/{id}") + public ResponseEntity update(@PathVariable Long id, @RequestBody Person person){ + if(service.read(id) != null) { + return new ResponseEntity<>(service.update(id, person), HttpStatus.OK); + }else{ + return new ResponseEntity<>(service.create(person), 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(@RequestBody Person person){ + return new ResponseEntity<>(service.delete(person),HttpStatus.NO_CONTENT); + } +} diff --git a/src/main/java/io/zipcoder/crudapp/PersonRepo.java b/src/main/java/io/zipcoder/crudapp/PersonRepo.java new file mode 100644 index 0000000..ad4559a --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepo.java @@ -0,0 +1,10 @@ +package io.zipcoder.crudapp; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository + +public interface PersonRepo 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..8b37c2c --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,42 @@ +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 PersonRepo repo; + + public Person create(Person person){ + return repo.save(person); + } + public Person read(Long id){ + return repo.findOne(id); + } + public List readAll(){ + Iterable personIterable = repo.findAll(); + List personList = new ArrayList<>(); + personIterable.forEach(personList::add); + return personList; + } + public Person update(Long id, Person person){ + Person newPerson = read(id); + newPerson.setFirstName(person.getFirstName()); + newPerson.setLastName(person.getLastName()); + newPerson = repo.save(newPerson); + return newPerson; + } + public Person delete(Long id){ + Person newPerson = read(id); + repo.delete(newPerson); + return newPerson; + } + public Person delete(Person person){ + return delete(person.getId()); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index dde67ef..45078f1 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,3 @@ spring.profiles.active=h2 -spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect \ No newline at end of file +spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect