From a7daefe8c8e92e326af645965c382783f3631a9d Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 14 Aug 2021 21:17:36 -0400 Subject: [PATCH 1/3] completed --- src/main/java/io/zipcoder/crudapp/Person.java | 57 +++++++++++++++++++ .../io/zipcoder/crudapp/PersonController.java | 42 ++++++++++++++ .../io/zipcoder/crudapp/PersonRepository.java | 8 +++ .../io/zipcoder/crudapp/PersonService.java | 49 ++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 src/main/java/io/zipcoder/crudapp/Person.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonController.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonRepository.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonService.java 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..651d9d6 --- /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/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java new file mode 100644 index 0000000..0474f02 --- /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 ="/people") + public ResponseEntity> readAll() { + return new ResponseEntity<>(personService.readAll(), HttpStatus.OK); + } + @GetMapping(value = "/{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)); + + } +} From 44b9e6ebbbadd3e515b8cf08a1398293a8a4a52f Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 14 Aug 2021 21:19:18 -0400 Subject: [PATCH 2/3] completed i think --- src/main/java/io/zipcoder/crudapp/PersonController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/zipcoder/crudapp/PersonController.java b/src/main/java/io/zipcoder/crudapp/PersonController.java index 0474f02..b3fb170 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonController.java +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -19,11 +19,11 @@ public class PersonController { public ResponseEntity create(@RequestBody Person person) { return new ResponseEntity<>(personService.create(person), HttpStatus.CREATED); } - @GetMapping(value ="/people") + @GetMapping(value ="/readAll") public ResponseEntity> readAll() { return new ResponseEntity<>(personService.readAll(), HttpStatus.OK); } - @GetMapping(value = "/{id}") + @GetMapping(value = "/read{id}") public ResponseEntity read(@PathVariable Long id){ return new ResponseEntity<>(personService.read(id),HttpStatus.OK); } From b4b0b17600ddd4b6d1e315e8a5fb9bc8cc6bb8d1 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 14 Aug 2021 21:32:52 -0400 Subject: [PATCH 3/3] lab complete --- src/main/java/io/zipcoder/crudapp/Person.java | 20 +++++++++---------- .../io/zipcoder/crudapp/PersonConfig.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/zipcoder/crudapp/PersonConfig.java diff --git a/src/main/java/io/zipcoder/crudapp/Person.java b/src/main/java/io/zipcoder/crudapp/Person.java index 651d9d6..4f3578f 100644 --- a/src/main/java/io/zipcoder/crudapp/Person.java +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -12,15 +12,15 @@ public class Person { Long id; String firstName; String lastName; - Integer age; + //Integer age; public Person(){} - public Person(Long id, String firstName, String lastName, Integer age) { + public Person(Long id, String firstName, String lastName){//, Integer age) { this.id = id; this.firstName = firstName; this.lastName = lastName; - this.age = age; + //this.age = age; } public Long getId() { @@ -47,11 +47,11 @@ public void setLastName(String lastName) { this.lastName = lastName; } - public Integer getAge() { - return age; - } - - public void setAge(Integer age) { - this.age = age; - } +// 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