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..5639f04 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,50 @@ +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/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java new file mode 100644 index 0000000..a66d4bf --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,52 @@ +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.*; + +import java.util.List; + +@RequestMapping(value = "person-controller") +@RestController +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 = "/people") + public ResponseEntity> findAll () { + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.GET, value = "/people/{id}") + public ResponseEntity findOne ( + @PathVariable Long id + ) { + return new ResponseEntity<>(service.read(id), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.PUT, value = "/update/{id}") + public ResponseEntity update ( + @PathVariable Long id, + @RequestBody Person person + ) { + return new ResponseEntity<>(service.update(id, 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); + } +} 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..88c333a --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepo.java @@ -0,0 +1,8 @@ +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..dae8467 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,47 @@ +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 result = new ArrayList<>(); + personIterable.forEach(result::add); + return result; + } + + public Person update(Long id, Person personToUpdate) { + Person personInTheDb = read(id); + personInTheDb.setFirstName(personToUpdate.getFirstName()); + personInTheDb.setLastName(personToUpdate.getLastName()); + personInTheDb = repo.save(personInTheDb); + return personInTheDb; + } + + public Person delete(Long id) { + Person personInDb = read(id); + repo.delete(personInDb); + return personInDb; + } + + public Person delete(Person person) { + return delete(person.getId()); + } +} diff --git a/src/main/resources/application-h2.properties b/src/main/resources/application-h2.properties index 74765cc..786c9d6 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -1,4 +1,9 @@ -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 + +server.port=8080 +spring.datasource.url=jdbc:h2:mem:testdb +spring.h2.console.enabled=true +spring.h2.console.view=/h2-console \ No newline at end of file