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..4f3578f --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -0,0 +1,57 @@ +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) + Long id; + String firstName; + String lastName; + //Integer age; + + public Person(){} + + public Person(Long id, String firstName, String lastName){//, Integer age) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + //this.age = age; + } + + 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; + } + +// public Integer getAge() { +// return age; +// } +// +// public void setAge(Integer age) { +// this.age = age; +// } +} 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..5826556 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonConfig.java @@ -0,0 +1,20 @@ +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, "Sitara","Rameez")); + service.create(new Person(2L, "Amal","Sitara-Rameez")); + service.create(new Person(3L, "Rameez", "Haja")); + } + +} \ No newline at end of file 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..b3fb170 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,42 @@ +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; + +@Controller +@RequestMapping(value = "/person-controller") +public class PersonController { + + @Autowired + private PersonService personService; + + @PostMapping(value = "/create") + public ResponseEntity create(@RequestBody Person person) { + return new ResponseEntity<>(personService.create(person), HttpStatus.CREATED); + } + @GetMapping(value ="/readAll") + public ResponseEntity> readAll() { + return new ResponseEntity<>(personService.readAll(), HttpStatus.OK); + } + @GetMapping(value = "/read{id}") + public ResponseEntity read(@PathVariable Long id){ + return new ResponseEntity<>(personService.read(id),HttpStatus.OK); + } + @PutMapping("/update/{id}") + public ResponseEntity update(@PathVariable Long id,@RequestBody Person person){ + return new ResponseEntity<>(personService.update(id,person),HttpStatus.OK); + } + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity delete(@PathVariable Long id){ + return new ResponseEntity<>(personService.delete(id),HttpStatus.OK); + } +// @DeleteMapping(value = "/delete/person") +// public ResponseEntity delete(@RequestBody Person person){ +// return new ResponseEntity<>(personService.delete(person),HttpStatus.OK); +// } +} \ No newline at end of file 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..608b9a8 --- /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 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..60c9678 --- /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.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 Iterable readAll(){ +// Iterable personIterable=repository.findAll(); +// List result=new ArrayList<>(); +// personIterable.forEach(result::add); +// return result; + return repository.findAll(); +} + +public Person update(Long id,Person newData) { + Person newP = read(id); + newP.setFirstName(newData.getFirstName()); + newP.setLastName(newData.getLastName()); + repository.save(newP); + return newP; + // Person newP=read(id); + //newData.setLastName(newP.getLastName()); + //repository.save(newData); +} +public Person delete(Person person){ + repository.delete(person); + return person; +} + public Person delete(Long id){ + return delete(read(id)); + + } +}