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,49 @@
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 pr;

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

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

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

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

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

@DeleteMapping("/people/{id}")
public ResponseEntity deletePerson(@PathVariable int id){
pr.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
39 changes: 39 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,39 @@
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)
Integer id;
String firstName;
String 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,9 @@
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> {
}