Pārlūkot izejas kodu

working on part2

Christian Sheridan 5 gadus atpakaļ
vecāks
revīzija
80c2e34b3a

+ 7
- 0
pom.xml Parādīt failu

@@ -48,6 +48,13 @@
48 48
 			<artifactId>spring-boot-starter-test</artifactId>
49 49
 			<scope>test</scope>
50 50
 		</dependency>
51
+		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
52
+		<dependency>
53
+			<groupId>org.springframework</groupId>
54
+			<artifactId>spring-jdbc</artifactId>
55
+			<version>5.1.3.RELEASE</version>
56
+		</dependency>
57
+
51 58
 	</dependencies>
52 59
 
53 60
 	<build>

+ 41
- 0
src/main/java/io/zipcoder/persistenceapp/Person.java Parādīt failu

@@ -0,0 +1,41 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.hibernate.boot.jaxb.hbm.internal.GenerationTimingConverter;
4
+
5
+import javax.persistence.*;
6
+import java.util.Date;
7
+
8
+@Entity
9
+public class Person {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    @Column
14
+    private Integer id;
15
+
16
+    @Column(name = "FIRST_NAME")
17
+    private String firstName;
18
+
19
+    @Column(name = "LAST_NAME")
20
+    private String lastName;
21
+
22
+    @Column(name = "MOBILE")
23
+    private String mobileNumber;
24
+
25
+    @Column(name = "BIRTHDAY")
26
+    private Date birthday;
27
+
28
+    @Column(name = "HOME_ID")
29
+    private Integer homeId;
30
+
31
+    public Person() {
32
+    }
33
+
34
+    public Person(String firstName, String lastName, String mobileNumber, Date birthday, Integer homeId) {
35
+        this.firstName = firstName;
36
+        this.lastName = lastName;
37
+        this.mobileNumber = mobileNumber;
38
+        this.birthday = birthday;
39
+        this.homeId = homeId;
40
+    }
41
+}

+ 36
- 0
src/main/java/io/zipcoder/persistenceapp/PersonController.java Parādīt failu

@@ -0,0 +1,36 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.boot.context.config.ResourceNotFoundException;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.stereotype.Controller;
8
+import org.springframework.web.bind.annotation.*;
9
+
10
+@Controller
11
+public class PersonController {
12
+    @Autowired
13
+    private PersonService personService;
14
+
15
+    @PostMapping(path = {"/people"})
16
+    @ResponseBody
17
+    public ResponseEntity<Person> createPerson(@RequestBody Person p){
18
+        return personService.createPerson(p);
19
+    }
20
+
21
+    @PutMapping(path = {"/people/{id}"})
22
+    public ResponseEntity<Person> updatePerson(@PathVariable Integer id, Person p){
23
+        return personService.updatePerson(id, p);
24
+    }
25
+
26
+
27
+    @GetMapping(path = {"/people/{id}"})
28
+    public ResponseEntity<Person> getPerson(@PathVariable Integer id){
29
+        return personService.getPerson(id);
30
+    }
31
+
32
+    @DeleteMapping(path = {"/people/{id}"})
33
+    public ResponseEntity<Void> deletePerson(@PathVariable Integer id){
34
+        return personService.deletePerson(id);
35
+    }
36
+}

+ 8
- 0
src/main/java/io/zipcoder/persistenceapp/PersonRepository.java Parādīt failu

@@ -0,0 +1,8 @@
1
+package io.zipcoder.persistenceapp;
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
+}

+ 41
- 0
src/main/java/io/zipcoder/persistenceapp/PersonService.java Parādīt failu

@@ -0,0 +1,41 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.boot.autoconfigure.session.SessionProperties;
5
+import org.springframework.boot.context.config.ResourceNotFoundException;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.stereotype.Service;
9
+import org.springframework.web.bind.annotation.*;
10
+
11
+@Service
12
+public class PersonService {
13
+
14
+    @Autowired
15
+    private PersonRepository personRepository;
16
+
17
+    public ResponseEntity<Person> createPerson(Person p) {
18
+        return new ResponseEntity<>(personRepository.save(p), HttpStatus.CREATED);
19
+    }
20
+
21
+
22
+    public ResponseEntity<Person> updatePerson( Integer id, Person p) {
23
+        return new ResponseEntity<>(personRepository.save(p), HttpStatus.OK);
24
+    }
25
+
26
+
27
+    public ResponseEntity<Person> getPerson( Integer id) {
28
+        return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
29
+    }
30
+
31
+
32
+    public ResponseEntity<Void> deletePerson( Integer id) {
33
+        try {
34
+            personRepository.delete(id);
35
+            return ResponseEntity.noContent().build();
36
+        } catch (ResourceNotFoundException e) {
37
+            return ResponseEntity.notFound().build();
38
+        }
39
+    }
40
+
41
+}