diff --git a/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java b/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java new file mode 100644 index 0000000..8fc8f23 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java @@ -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 getPersonById(@PathVariable Integer id){ + return new ResponseEntity<>(this.personService.getPerson(id), HttpStatus.OK); + } + + @GetMapping + public ResponseEntity> getPersonList(){ + return new ResponseEntity<>(this.personService.getAll(),HttpStatus.OK); + } + + @PostMapping + public ResponseEntity createPerson(@RequestBody Person p){ + return new ResponseEntity<>(this.personService.createPerson(p),HttpStatus.CREATED); + } + + @PutMapping("/{id}") + public ResponseEntity 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); + } +} diff --git a/src/main/java/io/zipcoder/crudapp/models/Person.java b/src/main/java/io/zipcoder/crudapp/models/Person.java new file mode 100644 index 0000000..42f1f66 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/models/Person.java @@ -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; + } +} diff --git a/src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java b/src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java new file mode 100644 index 0000000..3d486aa --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java @@ -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 { +} diff --git a/src/main/java/io/zipcoder/crudapp/services/PersonService.java b/src/main/java/io/zipcoder/crudapp/services/PersonService.java new file mode 100644 index 0000000..5584243 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/services/PersonService.java @@ -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 getAll (){ + return this.personRepository.findAll(); + } + +} diff --git a/src/main/resources/application-h2.properties b/src/main/resources/application-h2.properties index 74765cc..0ed91d5 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -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 \ No newline at end of file +spring.datasource.continue-on-error=false \ No newline at end of file diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 94f7b0f..c36308a 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -1,4 +1,4 @@ -DROP TABLE PERSON; +DROP TABLE IF EXISTS PERSON; CREATE TABLE PERSON ( ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, @@ -6,6 +6,6 @@ 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; diff --git a/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java new file mode 100644 index 0000000..4499799 --- /dev/null +++ b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java @@ -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)); + } +}