Brandon Defrancis 5 lat temu
rodzic
commit
75cca9f55e

+ 51
- 0
src/main/java/controller/PersonController.java Wyświetl plik

@@ -0,0 +1,51 @@
1
+package controller;
2
+
3
+import domain.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.*;
8
+import repositories.PersonRepository;
9
+
10
+import javax.validation.Valid;
11
+
12
+
13
+@RestController
14
+public class PersonController {
15
+
16
+    private PersonRepository personRepository;
17
+
18
+    @Autowired
19
+    public PersonController(PersonRepository personRepository) {
20
+        this.personRepository = personRepository;
21
+    }
22
+
23
+    @RequestMapping(value = "/people", method= RequestMethod.PUT)
24
+    public ResponseEntity<Person> createPerson(@Valid @RequestBody Person p){
25
+        personRepository.save(p);
26
+        return new ResponseEntity<>(p, HttpStatus.CREATED);
27
+    }
28
+
29
+    @RequestMapping(value="/people/{id}", method=RequestMethod.GET)
30
+    public ResponseEntity<Person> getPerson(@PathVariable int personId) {
31
+        Person p = personRepository.findOne(personId);
32
+        return new ResponseEntity<> (p, HttpStatus.OK);
33
+    }
34
+
35
+    @RequestMapping(value = "/people", method= RequestMethod.GET)
36
+    public ResponseEntity<Iterable<Person>> getPersonList(){
37
+        Iterable<Person> people = personRepository.findAll();
38
+        return new ResponseEntity<>(people, HttpStatus.OK);
39
+    }
40
+
41
+    @RequestMapping(value = "people/{id}", method = RequestMethod.PUT)
42
+    public ResponseEntity<Person> updatePerson(@Valid @RequestBody Person p){
43
+        Person person = personRepository.save(p);
44
+        return new ResponseEntity<>(person, HttpStatus.OK);
45
+    }
46
+
47
+    @RequestMapping(value = "/people", method = RequestMethod.DELETE)
48
+    public void deletePerson(@PathVariable int id){
49
+        personRepository.delete(id);
50
+    }
51
+}

+ 47
- 0
src/main/java/domain/Person.java Wyświetl plik

@@ -0,0 +1,47 @@
1
+package domain;
2
+
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+
6
+import javax.persistence.*;
7
+
8
+@Entity
9
+public class Person {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    @Column(name = "PERSON_ID")
14
+    private int id;
15
+
16
+    @Column(name = "FIRST_NAME")
17
+    @NotEmpty
18
+    private String firstName;
19
+
20
+    @Column(name = "LAST_NAME")
21
+    @NotEmpty
22
+    private String lastName;
23
+
24
+    public int getId() {
25
+        return id;
26
+    }
27
+
28
+    public void setId(int id) {
29
+        this.id = id;
30
+    }
31
+
32
+    public String getFirstName() {
33
+        return firstName;
34
+    }
35
+
36
+    public void setFirstName(String firstName) {
37
+        this.firstName = firstName;
38
+    }
39
+
40
+    public String getLastName() {
41
+        return lastName;
42
+    }
43
+
44
+    public void setLastName(String lastName) {
45
+        this.lastName = lastName;
46
+    }
47
+}

+ 9
- 0
src/main/java/repositories/PersonRepository.java Wyświetl plik

@@ -0,0 +1,9 @@
1
+package repositories;
2
+
3
+import domain.Person;
4
+import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.stereotype.Repository;
6
+
7
+@Repository
8
+public interface PersonRepository extends CrudRepository<Person, Integer> {
9
+}

+ 39
- 0
src/test/java/controller/PersonControllerTest.java Wyświetl plik

@@ -0,0 +1,39 @@
1
+package controller;
2
+
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+import org.mockito.Mock;
6
+import org.mockito.MockitoAnnotations;
7
+
8
+import static org.junit.Assert.*;
9
+
10
+public class PersonControllerTest {
11
+
12
+    @Mock
13
+    private PersonController personController;
14
+
15
+    @Before
16
+    public void setUp() throws Exception{
17
+        MockitoAnnotations.initMocks(this);
18
+    }
19
+
20
+    @Test
21
+    public void createPersonTest() {
22
+    }
23
+
24
+    @Test
25
+    public void getPersonTest() {
26
+    }
27
+
28
+    @Test
29
+    public void getPersonListTest() {
30
+    }
31
+
32
+    @Test
33
+    public void updatePersonTest() {
34
+    }
35
+
36
+    @Test
37
+    public void deletePersonTest() {
38
+    }
39
+}