Skip to content

Complete #6

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 1 commit 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
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
public class Person {
@Id
@GeneratedValue
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 Person(String firstName, String lastName) {
this(null, firstName, 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;
}
}
48 changes: 48 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.zipcoder.crudapp;

import org.springframework.stereotype.Controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.beans.factory.annotation.Autowired;


@Controller
public class PersonController {

private PersonService service;

@Autowired
public PersonController(PersonService service) {
this.service = service;}

@GetMapping("/people")
public ResponseEntity<Iterable<Person>> showAll() {
return new ResponseEntity<>(service.showAll(), HttpStatus.OK);
}

@GetMapping("/people/{id}")
public ResponseEntity<Person> show(@PathVariable Long id) {
return new ResponseEntity<>(service.show(id), HttpStatus.OK);
}

@PostMapping("/people")
public ResponseEntity<Person> create(@RequestBody Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}

@PutMapping("/people/{id}")
public ResponseEntity<Person> update(@PathVariable Long id, @RequestBody Person person) {
return new ResponseEntity<>(service.update(id, person), HttpStatus.OK);
}

@DeleteMapping("/people/{id}")
public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
return new ResponseEntity<>(service.delete(id), HttpStatus.OK);
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.zipcoder.crudapp;

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

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {

}
33 changes: 33 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.zipcoder.crudapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonService {

@Autowired
private PersonRepository repository;

public PersonService(PersonRepository repository){this.repository = repository;}

public Iterable<Person> showAll() {
return repository.findAll();}

public Person show(Long id){
return repository.findOne(id);} //todo something to do with get method

public Person create(Person person) {
return repository.save(person);}

public Person update(Long id, Person newPersonData) {
Person originalPerson = repository.findOne(id); //todo something to do with get method
originalPerson.setFirstName(newPersonData.getFirstName());
originalPerson.setLastName(newPersonData.getLastName());
return repository.save(originalPerson);
}

public Boolean delete(Long id) {
repository.delete(id);
return true;
}
}