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/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..8627cd7 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,46 @@ +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.ArrayList; + +@Controller +public class PersonController { + + + 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); + } + + @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..7942670 --- /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.Component; +import org.springframework.stereotype.Repository; + +@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 new file mode 100644 index 0000000..2377380 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,43 @@ +package io.zipcoder.crudapp; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + + + private PersonRepository repository; + + @Autowired + public PersonService(PersonRepository repository){ + this.repository = repository; + } + + + public Iterable findAllPeople () { + return repository.findAll(); + } + + public Person findOnePerson (Integer id){ + return repository.findById(id).get(); + } + + 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); + return repository.save(person); + } + + public Boolean deletePerson (Integer id) { + repository.deleteById(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() { - } - -}