Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/io/zipcoder/crudapp/People.java
Original file line number Diff line number Diff line change
@@ -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<Person> personList;

public People() {
}

public People(Long id, List<Person> personList) {
this.id = id;
this.personList = personList;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List<Person> getPersonList() {
return personList;
}

public void setPersonList(List<Person> personList) {
this.personList = personList;
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PeopleRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.zipcoder.crudapp;

import org.springframework.data.repository.CrudRepository;

public interface PeopleRepository extends CrudRepository {
}
52 changes: 52 additions & 0 deletions src/main/java/io/zipcoder/crudapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.zipcoder.crudapp;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;

@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;


@ManyToOne
@JsonIgnore
private People people;

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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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());
service.create(new Person());
service.create(new Person());
}
}
// Cardinality - OneToOne ManyToMany OneToMany

51 changes: 51 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -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<Person> create(@RequestBody Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}

@RequestMapping(method = RequestMethod.GET, value = "/read/{id}")
public ResponseEntity<Person> 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<List<Person>> readAll() {
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.PUT, value = "/update/{id}")
public ResponseEntity<Person> 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<Person> delete(@PathVariable Long id) {
return new ResponseEntity<>(service.delete(id), HttpStatus.NO_CONTENT);
}

}
6 changes: 6 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.zipcoder.crudapp;

import org.springframework.data.repository.CrudRepository;

public interface PersonRepository extends CrudRepository<Person, Long> {
}
48 changes: 48 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -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<Person> readAll() {
Iterable<Person> personIterable = repository.findAll();
List<Person> 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());
}

}