Skip to content

made changes to the CRUD lab #8

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;


@Entity
public class Person {


@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;


private String firstName;
private String lastName;

public Person() {

}

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

}

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;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(id, person.id) &&
Objects.equals(firstName, person.firstName) &&
Objects.equals(lastName, person.lastName);
}

@Override
public int hashCode() {return Objects.hash(id, firstName, lastName); }

@Override
public String toString() {
return "Person{" + " id = " + this.id + ", first name ='" + this.firstName + '\'' +
", last name'" + this.lastName + '\'' + '}';
}

}
43 changes: 43 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.zipcoder.crudapp;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class PersonController {

private PersonService service;


public PersonController(PersonService service) {
this.service = service;
}
@GetMapping("/people")
public ResponseEntity<Iterable<Person>> getPersonList() {
return new ResponseEntity<>(service.index(), HttpStatus.OK);
}
@GetMapping("/people/{id}")
public ResponseEntity<Person> getPerson(@PathVariable Long id) {
return new ResponseEntity<>(service.show(id), HttpStatus.OK);
}
@PostMapping("/people")
public ResponseEntity<Person> createPerson (Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}
@PutMapping("/people/{id}")
public ResponseEntity<Person> updatePerson(@PathVariable Long id, Person person) {
return new ResponseEntity<>(service.update(id, person), HttpStatus.OK);
}
@DeleteMapping("/people/{id}")
public ResponseEntity<Boolean> deletePerson(@PathVariable Long id) {
return new ResponseEntity<>(service.delete(id), HttpStatus.OK);
}







}
7 changes: 7 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.zipcoder.crudapp;

import org.springframework.data.repository.CrudRepository;

public interface PersonRepository extends CrudRepository<Person, Long> {

}
30 changes: 30 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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> index() {return repository.findAll();}

public Person show(Long id) {return repository.findOne(id);}

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

public Person update(Long id, Person newPersonData) {
Person originalPerson = repository.findOne(id);
originalPerson.setFirstName(newPersonData.getFirstName());
originalPerson.setLastName(newPersonData.getLastName());
return repository.save(originalPerson);
}

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

}