Skip to content

Done lab #10

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 2 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
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<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;
}
}
50 changes: 50 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.zipcoder.crudapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping(value = "/person_controller")
public class PersonController {
@Autowired
private PersonService service;

@RequestMapping(method = RequestMethod.POST, value = "/create")
public ResponseEntity<Person> createPerson(@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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.zipcoder.crudapp;

import org.springframework.data.repository.CrudRepository;

import java.util.Map;

public interface PersonRepository extends CrudRepository<Person, Long> {
}
46 changes: 46 additions & 0 deletions src/main/java/io/zipcoder/crudapp/PersonService.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.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);
}

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 personInDb= read(id);
personInDb.setFirstName(newPersonData.getFirstName());
personInDb.setLastName(newPersonData.getLastName());
return repository.save(personInDb);
}

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

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

}
3 changes: 2 additions & 1 deletion src/main/resources/application-h2.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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.view=/h2-console
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.profiles.active=h2

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