Xcuello hace 5 años
padre
commit
324e60723f

+ 52
- 0
src/main/java/io/zipcoder/crudapp/Person.java Ver fichero

@@ -0,0 +1,52 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Person {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    private String firstName;
14
+    private String lastName;
15
+    private Integer idNumber;
16
+
17
+
18
+    public Person() {
19
+
20
+    }
21
+
22
+    public String getFirstName() {
23
+
24
+        return firstName;
25
+    }
26
+
27
+    public void setFirstName(String firstName) {
28
+
29
+        this.firstName = firstName;
30
+    }
31
+
32
+    public String getLastName() {
33
+
34
+        return lastName;
35
+    }
36
+
37
+    public void setLastName(String lastName) {
38
+
39
+        this.lastName = lastName;
40
+    }
41
+
42
+    public Integer getIdNumber() {
43
+
44
+        return idNumber;
45
+    }
46
+
47
+    public void setIdNumber(Integer idNumber) {
48
+
49
+        this.idNumber = idNumber;
50
+    }
51
+}
52
+

+ 71
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Ver fichero

@@ -0,0 +1,71 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.*;
7
+
8
+@RestController
9
+public class PersonController {
10
+
11
+
12
+    @Autowired
13
+    PersonRepository personRepository;
14
+
15
+    public PersonController() {
16
+
17
+    }
18
+
19
+    @RequestMapping(value = "/people", method = RequestMethod.POST)
20
+    public ResponseEntity<Person> createPerson(@RequestBody Person p) {
21
+
22
+        return new ResponseEntity<>(personRepository.save(p), HttpStatus.CREATED);
23
+    }
24
+
25
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
26
+    public ResponseEntity<Person> getPerson(@PathVariable Integer id) {
27
+
28
+        Person p = personRepository.findOne(id);
29
+
30
+        if (p == null) {
31
+
32
+            return new ResponseEntity<>(p, HttpStatus.NOT_FOUND);
33
+        }
34
+
35
+        return new ResponseEntity<>(p, HttpStatus.OK);
36
+    }
37
+
38
+    @RequestMapping(value = "/people", method = RequestMethod.GET)
39
+    public ResponseEntity<Iterable<Person>> getPersonList() {
40
+
41
+        return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK);
42
+    }
43
+
44
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
45
+    public ResponseEntity<Person> updatePerson(@PathVariable Integer id,@RequestBody Person updatedPerson) {
46
+
47
+        Person p = personRepository.findOne(id);
48
+
49
+        if (p != null) {
50
+
51
+            return new ResponseEntity<>(updatedPerson, HttpStatus.OK);
52
+        }
53
+
54
+        updatedPerson.setIdNumber(id);
55
+
56
+        personRepository.save(updatedPerson);
57
+
58
+        return new ResponseEntity<>(updatedPerson, HttpStatus.CREATED);
59
+
60
+    }
61
+
62
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
63
+    public ResponseEntity<?> DeletePerson(@PathVariable Integer id) {
64
+
65
+        personRepository.delete(id);
66
+
67
+        return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
68
+    }
69
+
70
+}
71
+

+ 8
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Ver fichero

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

+ 3
- 1
src/main/resources/application.properties Ver fichero

@@ -1,3 +1,5 @@
1 1
 spring.profiles.active=h2
2 2
 
3
-spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
4
+
5
+server.port = 8081