Yesoda Sanka 5 年 前
コミット
11654d7a5a

+ 61
- 0
src/main/java/io/zipcoder/crudapp/Person.java ファイルの表示

@@ -0,0 +1,61 @@
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
+Create a `Person` class with fields for first name, last name, and an id number.
9
+
10
+ */
11
+
12
+@Entity
13
+public class Person {
14
+     @Id
15
+        @GeneratedValue(strategy = GenerationType.AUTO)
16
+
17
+     private Integer id;
18
+     private String firstname;
19
+     private String lastname;
20
+    
21
+     public String getFirstname(){
22
+
23
+         return firstname;
24
+    }
25
+
26
+    public void setFirstname(String firstname) {
27
+
28
+         this.firstname = firstname;
29
+    }
30
+
31
+    public String getLastname() {
32
+
33
+         return lastname;
34
+    }
35
+
36
+    public void setLastname(String lastname) {
37
+
38
+         this.lastname = lastname;
39
+    }
40
+
41
+    public int getId() {
42
+
43
+         return id;
44
+    }
45
+
46
+     public void setId(int id) {
47
+
48
+         this.id = id;
49
+     }
50
+     @Override
51
+    public String toString() {
52
+         return "Person{" +
53
+                 "id=" + id +
54
+                 ", firstname='" + firstname + '\'' +
55
+                 ", lastname='" + lastname + '\'' +
56
+                 '}';
57
+     }
58
+
59
+
60
+
61
+}

+ 78
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java ファイルの表示

@@ -0,0 +1,78 @@
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
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
8
+
9
+import java.net.URI;
10
+import java.util.ArrayList;
11
+import java.util.List;
12
+import java.util.stream.Collectors;
13
+import java.util.stream.StreamSupport;
14
+
15
+/*
16
+Create a `PersonController` class with `Person createPerson(Person p)`, `
17
+Person getPerson(int id)`, `List<Person> getPersonList()`, `Person updatePerson(Person p)`,
18
+ and `void DeletePerson(int id)` methods, and let it track a list of Person objects.
19
+
20
+Add the `@RestController` annotation to your `PersonController`
21
+class, and using the "Endpoints" list in the Reference section below,
22
+add the appropriate `@RequestMapping` annotations to each of your methods.
23
+Endpoints should be at `/people` and `/people/{id}` as appropriate.
24
+You will have to use `@PathVariable` for id numbers in the URI and `@RequestBody`
25
+for Person objects sent in the requests.
26
+
27
+ */
28
+@RestController
29
+public class PersonController {
30
+
31
+
32
+    @Autowired
33
+   PersonRepository personRepository;
34
+
35
+    @RequestMapping(value = "/person", method = RequestMethod.POST )
36
+
37
+   public  ResponseEntity <?> createPerson(@RequestBody Person p){
38
+
39
+       URI newPollUri = ServletUriComponentsBuilder
40
+               .fromCurrentRequest()
41
+               .path("/{id}")
42
+               .buildAndExpand(p .getId())
43
+               .toUri();
44
+       return new ResponseEntity<> (null, HttpStatus.CREATED) ;
45
+
46
+    }
47
+
48
+    @RequestMapping(value = "/person{id}", method = RequestMethod.GET)
49
+
50
+   public  ResponseEntity<?> getPerson(@PathVariable Integer id){
51
+
52
+        Person person=personRepository.findOne(id);
53
+
54
+        return new ResponseEntity<>(person ,HttpStatus.OK ) ;
55
+    }
56
+
57
+    @RequestMapping(value = "/person", method = RequestMethod.GET)
58
+    public ResponseEntity<Iterable  <Person > >getPersonList( ){
59
+        Iterable<Person> person = personRepository .findAll();
60
+
61
+        Iterable<Person > personIterable = StreamSupport.stream(person .spliterator(),false)
62
+
63
+            .collect(Collectors.toList() );
64
+        return new ResponseEntity<>(personIterable  , HttpStatus.OK ) ;
65
+    }
66
+
67
+    @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT)
68
+    public  ResponseEntity<?> updatePerson(@RequestBody Person person,@PathVariable Integer id){
69
+        Person p=personRepository.save(person ) ;
70
+       return new ResponseEntity<>(HttpStatus.OK);
71
+    }
72
+    @RequestMapping(value = "/person{id}", method = RequestMethod.DELETE )
73
+    public void deletePerson( @PathVariable Integer id){
74
+        personRepository .delete(id);
75
+
76
+    }
77
+
78
+}

+ 10
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java ファイルの表示

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