Skip to content

finito #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions src/main/java/io/zipcoder/crudapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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;
}
}
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(1L, "Raymond","Fitzgerald"));
service.create(new Person(2L, "Stephanie","Fitzgerald"));
service.create(new Person(3L, "Kelly", "Fitzgerald"));
service.create(new Person(4L, "Beans","Fitzgerald"));
}

}
56 changes: 56 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 = "/readAll")
public ResponseEntity<List<Person>> readAll(){
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@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);
} else{
return new ResponseEntity<>(service.read(id), HttpStatus.NOT_FOUND);
}
}

@RequestMapping(method = RequestMethod.PUT, value = "/update/{id}")
public ResponseEntity<Person> update(@PathVariable Long id, @RequestBody Person person){
if(service.read(id) != null) {
return new ResponseEntity<>(service.update(id, person), HttpStatus.OK);
}else{
return new ResponseEntity<>(service.create(person), 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);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/delete")
public ResponseEntity<Person> delete(@RequestBody Person person){
return new ResponseEntity<>(service.delete(person),HttpStatus.NO_CONTENT);
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.zipcoder.crudapp;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository

public interface PersonRepo extends CrudRepository<Person, Long> {
}

42 changes: 42 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 PersonRepo repo;

public Person create(Person person){
return repo.save(person);
}
public Person read(Long id){
return repo.findOne(id);
}
public List<Person> readAll(){
Iterable<Person> personIterable = repo.findAll();
List<Person> personList = new ArrayList<>();
personIterable.forEach(personList::add);
return personList;
}
public Person update(Long id, Person person){
Person newPerson = read(id);
newPerson.setFirstName(person.getFirstName());
newPerson.setLastName(person.getLastName());
newPerson = repo.save(newPerson);
return newPerson;
}
public Person delete(Long id){
Person newPerson = read(id);
repo.delete(newPerson);
return newPerson;
}
public Person delete(Person person){
return delete(person.getId());
}

}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.profiles.active=h2

spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect