Skip to content

all classes written and postman tests working #2

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 3 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
53 changes: 53 additions & 0 deletions src/main/java/io/zipcoder/crudapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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)
Integer id;
String firstName;
String lastName;

public Person () {}

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

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

@Controller
public class PersonController {


private PersonService service;

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

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

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

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

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

@DeleteMapping("/people/{id}")
public ResponseEntity<Boolean> deletePerson(@PathVariable Integer id){
return new ResponseEntity<>(service.deletePerson(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.Component;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends CrudRepository <Person, Integer> {
}
43 changes: 43 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.zipcoder.crudapp;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonService {


private PersonRepository repository;

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


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

public Person findOnePerson (Integer id){
return repository.findById(id).get();
}

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

public Person updatePerson (Person person) {
// Person personInDB = repository.findOne(person.getId());
// personInDB.setFirstName(person.getFirstName());
// personInDB.setLastName(person.getLastName());
// return repository.save(personInDB);
return repository.save(person);
}

public Boolean deletePerson (Integer id) {
repository.deleteById(id);
return true;
}
}
16 changes: 0 additions & 16 deletions src/test/java/io/zipcoder/crudapp/CRUDApplicationTests.java

This file was deleted.