Skip to content
Open

fin #11

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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</build>

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/io/zipcoder/crudapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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)
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 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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.zipcoder.crudapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration // classes that run before the application is served
public class PersonConfig {

@Autowired
private PersonService service;

@PostConstruct
public void setup() {
service.create(new Person(10L, "Manny", "Mbanefo"));
service.create(new Person());
service.create(new Person());
}
}
53 changes: 53 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.zipcoder.crudapp;

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
@RequestMapping(value = "/person-controller")
public class PersonController { // they wrap it up with a response entity and decorate the methods with annotations to expose them as endpoints
@Autowired
private PersonService service;

@RequestMapping(method = RequestMethod.POST, value = "/create")
public ResponseEntity<Person> create(@RequestBody Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}

@RequestMapping(method = RequestMethod.GET, value = "/read/{id}")
public ResponseEntity<Person> read(@PathVariable Long id) {
if(service.read(id) != null) {
return new ResponseEntity<>(service.read(id), HttpStatus.OK);
}
return new ResponseEntity<>(service.read(id), HttpStatus.NOT_FOUND);
}

@RequestMapping(method = RequestMethod.GET, value = "/readAll")
public ResponseEntity<List<Person>> readAll() {
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.PUT, value = "/update/{id}")
public ResponseEntity<Person> update(
@PathVariable Long id,
@RequestBody Person newPersonData) {
if(service.read(id) != null) {
return new ResponseEntity<>(service.update(id, newPersonData), HttpStatus.OK);
} return new ResponseEntity<>(service.create(newPersonData), HttpStatus.CREATED);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/delete/{id}")
public ResponseEntity<Person> delete(@PathVariable Long id) {
return new ResponseEntity<>(service.delete(id), HttpStatus.NO_CONTENT);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/delete")
public ResponseEntity<Person> delete(Person person) {
return new ResponseEntity<>(service.delete(person), HttpStatus.NO_CONTENT);
}
}

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> {

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

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

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonService {
@Autowired
private PersonRepository repository;

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

public Person read(Long id) {
return repository.findOne(id);
//.get();
}

public List<Person> readAll() {
Iterable<Person> personIterable = repository.findAll();
List<Person> result = new ArrayList<>();
personIterable.forEach(result::add);
return result;
}

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

public Person delete(Long id) {
Person personInDb = read(id);
repository.delete(personInDb);
return personInDb;
}

public Person delete(Person person) {
return delete(person.getId());
}

}