Browse Source

CRUDApplication

Person
PersonController
PersonRepository
PersonService

Working Part 2
Nick Satinover 5 years ago
parent
commit
0b2d13060e

+ 47
- 47
pom.xml View File

@@ -1,65 +1,65 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
-	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
-	<modelVersion>4.0.0</modelVersion>
3
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+    <modelVersion>4.0.0</modelVersion>
5 5
 
6
-	<groupId>io.zipcoder</groupId>
7
-	<artifactId>crud-app-starter</artifactId>
8
-	<version>0.0.1-SNAPSHOT</version>
9
-	<packaging>jar</packaging>
6
+    <groupId>io.zipcoder</groupId>
7
+    <artifactId>crud-app-starter</artifactId>
8
+    <version>0.0.1-SNAPSHOT</version>
9
+    <packaging>jar</packaging>
10 10
 
11
-	<name>crud-app-starter</name>
12
-	<description>Starter project for Spring Boot-based CRUD applications</description>
11
+    <name>crud-app-starter</name>
12
+    <description>Starter project for Spring Boot-based CRUD applications</description>
13 13
 
14
-	<parent>
15
-		<groupId>org.springframework.boot</groupId>
16
-		<artifactId>spring-boot-starter-parent</artifactId>
17
-		<version>1.5.2.RELEASE</version>
18
-		<relativePath/> <!-- lookup parent from repository -->
19
-	</parent>
14
+    <parent>
15
+        <groupId>org.springframework.boot</groupId>
16
+        <artifactId>spring-boot-starter-parent</artifactId>
17
+        <version>1.5.2.RELEASE</version>
18
+        <relativePath/> <!-- lookup parent from repository -->
19
+    </parent>
20 20
 
21
-	<properties>
22
-		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23
-		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24
-		<java.version>1.8</java.version>
25
-	</properties>
21
+    <properties>
22
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24
+        <java.version>1.8</java.version>
25
+    </properties>
26 26
 
27
-	<dependencies>
28
-		<dependency>
29
-			<groupId>org.springframework.boot</groupId>
30
-			<artifactId>spring-boot-starter</artifactId>
31
-		</dependency>
27
+    <dependencies>
28
+        <dependency>
29
+            <groupId>org.springframework.boot</groupId>
30
+            <artifactId>spring-boot-starter</artifactId>
31
+        </dependency>
32 32
 
33
-		<dependency>
34
-			<groupId>com.h2database</groupId>
35
-			<artifactId>h2</artifactId>
33
+        <dependency>
34
+            <groupId>com.h2database</groupId>
35
+            <artifactId>h2</artifactId>
36 36
             <scope>compile</scope>
37
-		</dependency>
38
-		<dependency>
39
-			<groupId>org.springframework.boot</groupId>
40
-			<artifactId>spring-boot-starter-test</artifactId>
41
-			<scope>test</scope>
42
-		</dependency>
37
+        </dependency>
38
+        <dependency>
39
+            <groupId>org.springframework.boot</groupId>
40
+            <artifactId>spring-boot-starter-test</artifactId>
41
+            <scope>test</scope>
42
+        </dependency>
43 43
         <dependency>
44 44
             <groupId>org.springframework.boot</groupId>
45 45
             <artifactId>spring-boot-starter-web</artifactId>
46 46
             <!--<version>RELEASE</version>-->
47 47
         </dependency>
48
-		<dependency>
49
-			<groupId>org.springframework.boot</groupId>
50
-			<artifactId>spring-boot-starter-data-jpa</artifactId>
51
-			<!--<version>1.3.5.RELEASE</version>-->
52
-		</dependency>
48
+        <dependency>
49
+            <groupId>org.springframework.boot</groupId>
50
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
51
+            <!--<version>1.3.5.RELEASE</version>-->
52
+        </dependency>
53 53
     </dependencies>
54 54
 
55
-	<build>
56
-		<plugins>
57
-			<plugin>
58
-				<groupId>org.springframework.boot</groupId>
59
-				<artifactId>spring-boot-maven-plugin</artifactId>
60
-			</plugin>
61
-		</plugins>
62
-	</build>
55
+    <build>
56
+        <plugins>
57
+            <plugin>
58
+                <groupId>org.springframework.boot</groupId>
59
+                <artifactId>spring-boot-maven-plugin</artifactId>
60
+            </plugin>
61
+        </plugins>
62
+    </build>
63 63
 
64 64
 
65
-</project>
65
+</project>

+ 51
- 0
src/main/java/io/zipcoder/crudapp/Person.java View File

@@ -0,0 +1,51 @@
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
+    private String firstName;
11
+    private String lastname;
12
+    @Id
13
+    @GeneratedValue(strategy = GenerationType.AUTO)
14
+    private int id;
15
+
16
+    public Person(){}
17
+
18
+    public Person(String firstName, String lastName) {
19
+        this.firstName = firstName;
20
+        this.lastname = lastName;
21
+    }
22
+
23
+    public String getFirstName() {
24
+        return firstName;
25
+    }
26
+
27
+    public void setFirstName(String firstName) {
28
+        this.firstName = firstName;
29
+    }
30
+
31
+    public String getLastname() {
32
+        return lastname;
33
+    }
34
+
35
+    public void setLastname(String lastname) {
36
+        this.lastname = lastname;
37
+    }
38
+
39
+    public int getId() {
40
+        return id;
41
+    }
42
+
43
+    @Override
44
+    public String toString() {
45
+        return "Person{" +
46
+                "firstName='" + firstName + '\'' +
47
+                ", lastname='" + lastname + '\'' +
48
+                ", id=" + id +
49
+                '}';
50
+    }
51
+}

+ 50
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java View File

@@ -0,0 +1,50 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.web.bind.annotation.*;
4
+
5
+import java.util.List;
6
+
7
+@RestController
8
+public class PersonController {
9
+
10
+    //  var URI = "http://localhost:8080/people/";
11
+    @PostMapping("/people/{firstName}/{lastName}")
12
+    Person createPerson(@PathVariable String firstName, @PathVariable String lastName){ //(Person p)
13
+        Person person = new Person(firstName, lastName);
14
+        return null;
15
+    }
16
+
17
+    @GetMapping("/people/{id}")
18
+    Person getPerson(@PathVariable int requestedId){
19
+        int id = requestedId;
20
+        return null;
21
+    }
22
+
23
+    @GetMapping("/people")
24
+    List<Person> getPersonList(){
25
+        return null;
26
+    }
27
+
28
+    @PostMapping("/people/{id}")
29
+    Person updatePerson(Person p){
30
+        //update person then return
31
+        return p;
32
+    }
33
+
34
+    @DeleteMapping("/people{id}")
35
+    void deletePerson(@PathVariable int id){
36
+
37
+    }
38
+}
39
+/*
40
+POST /people - create a new person
41
+Response: 201 Created
42
+GET /people - get the list of all people
43
+Response: 200 OK
44
+GET /people/{id} - Get the person with id number {id}
45
+Response: 200 OK if found, else 404 Not Found
46
+PUT /people/{id} - Update the person with id number {id}
47
+Response: 200 OK if updated, 201 Created if a new entity was created
48
+DELETE /people/{id} - delete the person with id number {id}
49
+Response: 204 No Content
50
+ */

+ 8
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java View File

@@ -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
+}

+ 13
- 0
src/main/java/io/zipcoder/crudapp/PersonService.java View File

@@ -0,0 +1,13 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+
6
+@Service
7
+public class PersonService {
8
+
9
+    @Autowired
10
+    PersonRepository personRepository;
11
+
12
+    
13
+}