From b443bb2d1c4e6cdd857bb15b8b62fdf9970e13a5 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Tue, 17 Aug 2021 18:50:05 -0400 Subject: [PATCH 1/2] Completed --- src/main/java/io/zipcoder/crudapp/Person.java | 48 +++++++++++++++++ .../io/zipcoder/crudapp/PersonConfig.java | 19 +++++++ .../io/zipcoder/crudapp/PersonController.java | 51 +++++++++++++++++++ .../io/zipcoder/crudapp/PersonRepository.java | 6 +++ .../io/zipcoder/crudapp/PersonService.java | 48 +++++++++++++++++ 5 files changed, 172 insertions(+) create mode 100644 src/main/java/io/zipcoder/crudapp/Person.java create mode 100644 src/main/java/io/zipcoder/crudapp/PersonConfig.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..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/PersonConfig.java b/src/main/java/io/zipcoder/crudapp/PersonConfig.java new file mode 100644 index 0000000..9d5a7c2 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonConfig.java @@ -0,0 +1,19 @@ +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(6L, "Bob", "Billy")); + service.create(new Person()); + service.create(new Person()); + } +} 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..9949c68 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonController.java @@ -0,0 +1,51 @@ +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.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.List; + +@Controller +@RequestMapping(value = "/person-controller") +public class PersonController { + @Autowired + private PersonService service; + + @RequestMapping(method = RequestMethod.POST, value = "/create") + public ResponseEntity create(@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..1195706 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonRepository.java @@ -0,0 +1,6 @@ +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..c065114 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PersonService.java @@ -0,0 +1,48 @@ +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()); + personInDb = repository.save(personInDb); + return personInDb; + } + + public Person delete(Long id) { + Person personInDb = read(id); + repository.delete(personInDb); + return personInDb; + } + + public Person delete(Person person) { + return delete(person.getId()); + } + +} \ No newline at end of file From cb059c06405f33cf87bde7e2c9e6d726a29ab538 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 17 Oct 2021 19:44:14 -0400 Subject: [PATCH 2/2] Testing commits --- src/main/java/io/zipcoder/crudapp/People.java | 39 +++++++++++++++++++ .../io/zipcoder/crudapp/PeopleRepository.java | 6 +++ src/main/java/io/zipcoder/crudapp/Person.java | 12 ++++-- .../io/zipcoder/crudapp/PersonConfig.java | 4 +- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/zipcoder/crudapp/People.java create mode 100644 src/main/java/io/zipcoder/crudapp/PeopleRepository.java diff --git a/src/main/java/io/zipcoder/crudapp/People.java b/src/main/java/io/zipcoder/crudapp/People.java new file mode 100644 index 0000000..9419252 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/People.java @@ -0,0 +1,39 @@ +package io.zipcoder.crudapp; + +import javax.persistence.*; +import java.util.List; + +@Entity +public class People { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @OneToMany + private List personList; + + public People() { + } + + public People(Long id, List personList) { + this.id = id; + this.personList = personList; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getPersonList() { + return personList; + } + + public void setPersonList(List personList) { + this.personList = personList; + } +} diff --git a/src/main/java/io/zipcoder/crudapp/PeopleRepository.java b/src/main/java/io/zipcoder/crudapp/PeopleRepository.java new file mode 100644 index 0000000..5c6babe --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/PeopleRepository.java @@ -0,0 +1,6 @@ +package io.zipcoder.crudapp; + +import org.springframework.data.repository.CrudRepository; + +public interface PeopleRepository extends CrudRepository { +} diff --git a/src/main/java/io/zipcoder/crudapp/Person.java b/src/main/java/io/zipcoder/crudapp/Person.java index ec081f7..da062ca 100644 --- a/src/main/java/io/zipcoder/crudapp/Person.java +++ b/src/main/java/io/zipcoder/crudapp/Person.java @@ -1,9 +1,8 @@ package io.zipcoder.crudapp; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import javax.persistence.*; @Entity public class Person { @@ -13,6 +12,11 @@ public class Person { private String firstName; private String lastName; + + @ManyToOne + @JsonIgnore + private People people; + public Person() { } diff --git a/src/main/java/io/zipcoder/crudapp/PersonConfig.java b/src/main/java/io/zipcoder/crudapp/PersonConfig.java index 9d5a7c2..484780c 100644 --- a/src/main/java/io/zipcoder/crudapp/PersonConfig.java +++ b/src/main/java/io/zipcoder/crudapp/PersonConfig.java @@ -12,8 +12,10 @@ public class PersonConfig { @PostConstruct public void setup() { - service.create(new Person(6L, "Bob", "Billy")); + service.create(new Person()); service.create(new Person()); service.create(new Person()); } } +// Cardinality - OneToOne ManyToMany OneToMany +