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..0bd55f6 --- /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..30a9c82 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,47 @@ +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 +public class PersonController { + + @Autowired + private PersonService service; + + + + @PostMapping(value = "/create") + public ResponseEntity create(@RequestBody Person person){ + return new ResponseEntity<>(service.create(person), HttpStatus.CREATED); + } + + + @GetMapping(value = "/read/{id}") + public ResponseEntity read(@PathVariable Long id){ + return new ResponseEntity<>(service.read(id), HttpStatus.OK); + } + + + @GetMapping(value = "/all") + public ResponseEntity> readAll() { + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + + @PutMapping("/update/{id}") + public ResponseEntity update(@PathVariable Long id, @RequestBody Person newPersonData) { + return new ResponseEntity<>(service.update(id, newPersonData), HttpStatus.OK); + } + + + @DeleteMapping("/delete/{id}") + public ResponseEntity delete(@PathVariable Long id){ + return new ResponseEntity<>(service.delete(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..978d237 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,7 @@ +package io.zipcoder.crudapp; + +import org.springframework.data.repository.CrudRepository; + +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..bfa7431 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,49 @@ +package io.zipcoder.crudapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +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); + newPersonData.setId(personInDB.getId()); + return repository.save(newPersonData); + + } + + public Person delete(Person person){ + //return delete(person.getId()); + repository.delete(person); + return person; + } + + public Person delete(Long id){ + return delete(id); + } + + +}