Skip to content

Completed Lab #19

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
27 changes: 27 additions & 0 deletions src/main/java/PersonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import io.zipcoder.crudapp.Person;
import io.zipcoder.crudapp.PersonRepository;

@Configuration
public class PersonConfig {

private PersonRepository repository;


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

@PostConstruct
public void setup() {
repository.save(new Person());
}

}
2 changes: 1 addition & 1 deletion src/main/java/io/zipcoder/crudapp/CRUDApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void main(String[] args) {
@Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
registrationBean.addUrlMappings("/h2-console/*");
return registrationBean;
}
}
45 changes: 45 additions & 0 deletions src/main/java/io/zipcoder/crudapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 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;
}


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



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

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.PathVariable;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/person-controller")
public class PersonController {

private PersonService service;

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

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

@RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
public ResponseEntity<Person> read(@PathVariable Long id) {
return service.read(id);
}

@RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
public ResponseEntity<Person> update(@PathVariable Long id, @RequestBody Person newData) {
return service.update(id, newData);
}

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

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

import java.util.Optional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

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


Optional<Person> findById(Long id);

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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class PersonService {
private PersonRepository repository;

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

public ResponseEntity<Person> create(Person personRequestedToPersist) {
Person personInDatabase = repository.save(personRequestedToPersist);
ResponseEntity<Person> responseEntity = new ResponseEntity<>(personInDatabase, HttpStatus.CREATED);
return responseEntity;
}

public ResponseEntity<Person> read(Long id) {
Person personInDatabase = repository.findById(id).get();
return new ResponseEntity<>(personInDatabase, HttpStatus.OK);
}

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

public ResponseEntity<Person> update(Long id, Person newData) {
Person personInDatabase = repository.findById(id).get();
String newFirstName = newData.getFirstName();
String newLastName = newData.getLastName();
personInDatabase.setFirstName(newFirstName);
personInDatabase.setLastName(newLastName);
personInDatabase = repository.save(personInDatabase);
return new ResponseEntity<>(personInDatabase, HttpStatus.OK);
}
public ResponseEntity<Person> delete(Long id) {
Person personToBeDeleted = repository.findById(id).get();
repository.delete(id);
return new ResponseEntity<>(personToBeDeleted, HttpStatus.OK);
}

}
7 changes: 6 additions & 1 deletion src/main/resources/application-h2.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
spring.datasource.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true
spring.datasource.continue-on-error=true

spring.h2.console.enabled=true
spring.h2.console.view=/h2-console
server.port=8080

1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
spring.profiles.active=h2

spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect