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
50 changes: 50 additions & 0 deletions src/main/java/io/zipcoder/crudapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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;
}
}
52 changes: 52 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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;

@RequestMapping(value = "person-controller")
@RestController
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 = "/people")
public ResponseEntity<List<Person>> findAll () {
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, value = "/people/{id}")
public ResponseEntity<Person> findOne (
@PathVariable Long id
) {
return new ResponseEntity<>(service.read(id), HttpStatus.OK);
}

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

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

@Repository
public interface PersonRepo extends CrudRepository<Person, Long> {
}
47 changes: 47 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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> result = new ArrayList<>();
personIterable.forEach(result::add);
return result;
}

public Person update(Long id, Person personToUpdate) {
Person personInTheDb = read(id);
personInTheDb.setFirstName(personToUpdate.getFirstName());
personInTheDb.setLastName(personToUpdate.getLastName());
personInTheDb = repo.save(personInTheDb);
return personInTheDb;
}

public Person delete(Long id) {
Person personInDb = read(id);
repo.delete(personInDb);
return personInDb;
}

public Person delete(Person person) {
return delete(person.getId());
}
}
9 changes: 7 additions & 2 deletions src/main/resources/application-h2.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle

spring.datasource.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true
spring.datasource.continue-on-error=true

server.port=8080
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
spring.h2.console.view=/h2-console