Skip to content

finished lab #5

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 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.zipcoder.crudapp.controllers;

import io.zipcoder.crudapp.models.Person;
import io.zipcoder.crudapp.services.PersonService;
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;

@RequestMapping("/people")
@RestController
public class PersonController {

private PersonService personService;

public PersonController(PersonService personService){
this.personService = personService;
}

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

@GetMapping
public ResponseEntity<Iterable<Person>> getPersonList(){
return new ResponseEntity<>(this.personService.getAll(),HttpStatus.OK);
}

@PostMapping
public ResponseEntity<Person> createPerson(@RequestBody Person p){
return new ResponseEntity<>(this.personService.createPerson(p),HttpStatus.CREATED);
}

@PutMapping("/{id}")
public ResponseEntity<Person> updatePerson (Person p, @PathVariable Integer id){
return new ResponseEntity<>(this.personService.updatePerson(p,id),HttpStatus.OK) ;
}

@DeleteMapping("/{id}")
public void deletePersonById(@PathVariable Integer id){
this.personService.deletePersonById(id);
}
}
40 changes: 40 additions & 0 deletions src/main/java/io/zipcoder/crudapp/models/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.zipcoder.crudapp.models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person {
private String first_NAME;
private String last_NAME;

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

public Person(){

}

public String getFirst_NAME() {
return first_NAME;
}

public String getLast_NAME() {
return last_NAME;
}

public int getId() {
return id;
}

public void setFirst_NAME(String firstName) {
this.first_NAME = firstName;
}

public void setLast_NAME(String lastName) {
this.last_NAME = lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.zipcoder.crudapp.repositories;

import io.zipcoder.crudapp.models.Person;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

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

import io.zipcoder.crudapp.models.Person;
import io.zipcoder.crudapp.repositories.PersonRepository;
import org.springframework.stereotype.Service;

@Service
public class PersonService {
private PersonRepository personRepository;

public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}

public void deletePersonById (Integer id){
this.personRepository.delete(id);
}

public Person updatePerson(Person newPersonData, Integer id){
Person personToUpdate = this.personRepository.findOne(id);
personToUpdate.setFirst_NAME(newPersonData.getFirst_NAME());
personToUpdate.setLast_NAME(newPersonData.getLast_NAME());
this.personRepository.save(personToUpdate);

return personToUpdate;
}

public Person getPerson(Integer id){
return this.personRepository.findOne(id);
}

public Person createPerson(Person p){
return this.personRepository.save(p);
}

public Iterable<Person> getAll (){
return this.personRepository.findAll();
}

}
2 changes: 1 addition & 1 deletion src/main/resources/application-h2.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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=false
4 changes: 2 additions & 2 deletions src/main/resources/schema-h2.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
DROP TABLE PERSON;
DROP TABLE IF EXISTS PERSON;

CREATE TABLE PERSON (
ID NUMBER(10,0) NOT NULL AUTO_INCREMENT,
FIRST_NAME VARCHAR2(255) DEFAULT NULL,
LAST_NAME VARCHAR2(255) DEFAULT NULL,
PRIMARY KEY (ID));

DROP SEQUENCE hibernate_sequence;
DROP SEQUENCE IF EXISTS hibernate_sequence;

CREATE SEQUENCE hibernate_sequence;
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.zipcoder.crudapp.controllers;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@SpringBootTest
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
public class PersonControllerTests {

@Autowired
private MockMvc mvc;


@MockBean
private PersonRepository repository;

@Test
public void testCreatePerson() throws Exception {
Person newPerson = new Person();
newPerson.setFirst_NAME("Valentin");
newPerson.setLast_NAME("G");

BDDMockito
.given(repository.save(newPerson))
.willReturn(newPerson);

String expectedContent = "{\"ID\":null,\"FIRST_NAME\":\"Valentin\",\"LAST_NAME\":\"G\"}";
this.mvc.perform(MockMvcRequestBuilders
.post("/people")
.content(expectedContent)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(MockMvcResultMatchers.status().isCreated())
.andExpect(MockMvcResultMatchers.content().string(expectedContent));
}
}