diff --git a/pom.xml b/pom.xml index 558c7a1..3d8aafe 100644 --- a/pom.xml +++ b/pom.xml @@ -55,8 +55,9 @@ - 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/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java new file mode 100644 index 0000000..c9f4997 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,50 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + + +@Controller +@RequestMapping(value = "/person_controller") +public class PersonController { + @Autowired + private PersonService service; + + @RequestMapping(method = RequestMethod.POST, value = "/create") + public ResponseEntity createPerson(@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); + } +} 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..da611df --- /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 java.util.Map; + +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..0dee858 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,46 @@ +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); + } + + 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 personInDb= read(id); + personInDb.setFirstName(newPersonData.getFirstName()); + personInDb.setLastName(newPersonData.getLastName()); + return repository.save(personInDb); + } + + public Person delete(Long id){ + Person personInDb = read(id); + repository.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..6a72727 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -1,4 +1,5 @@ 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.view=/h2-console 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