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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.zipcoder.crudapp.controllers;

import io.zipcoder.crudapp.models.Person;
import io.zipcoder.crudapp.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PersonController {

private PersonRepository repository;

@Autowired
public PersonController (PersonRepository repository) {
this.repository = repository;
}

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

@GetMapping("/people/{id}")
public ResponseEntity<Person> getPerson(@PathVariable Integer id) {
return new ResponseEntity<>(repository.findOne(id), HttpStatus.OK);
}

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

@PutMapping("/people/{id}")
public ResponseEntity<Person> updatePerson(@PathVariable Integer id, @RequestBody Person person) {
Person updatedPerson = repository.findOne(id);
updatedPerson.setFirstName(person.getFirstName());
updatedPerson.setLastName(person.getLastName());
return new ResponseEntity<>(repository.save(updatedPerson),HttpStatus.OK);
}

@DeleteMapping("/people/{id}")
public ResponseEntity deletePerson(@PathVariable Integer id) {
repository.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/zipcoder/crudapp/models/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.zipcoder.crudapp.models;

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 Integer id;
private String firstName;
private String lastName;

public Person() {}

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public Integer getId() {
return id;
}

public void setId(Integer 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.zipcoder.crudapp.repositories;

import io.zipcoder.crudapp.models.Person;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

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

}