Jennifer Chao 5 年 前
コミット
e9f8ff7d5a

+ 18
- 4
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java ファイルの表示

@@ -2,26 +2,39 @@ package com.zipcodewilmington.bakery.Controllers;
2 2
 
3 3
 import com.zipcodewilmington.bakery.Models.Baker;
4 4
 import com.zipcodewilmington.bakery.Repositories.BakerRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.http.HttpStatus;
6 7
 import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
7 9
 
10
+import java.lang.reflect.Array;
11
+import java.util.*;
12
+import java.util.stream.Collectors;
13
+import java.util.stream.Stream;
14
+
15
+@RestController
8 16
 public class BakerController {
9 17
 
18
+    @Autowired
10 19
     private BakerRepository bakerRepository;
11 20
 
21
+    @GetMapping(path = "/bakers")
12 22
     public ResponseEntity<Iterable<Baker>> index() {
13 23
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
14 24
     }
15 25
 
16
-    public ResponseEntity<Baker> show(Long id) {
26
+    @GetMapping(path = "/bakers/{id}")
27
+    public ResponseEntity<Baker> show(@PathVariable Long id) {
17 28
         return new ResponseEntity<>(this.bakerRepository.findOne(id), HttpStatus.OK);
18 29
     }
19 30
 
31
+    @PostMapping(path = "/bakers")
20 32
     public ResponseEntity<Baker> create(Baker baker) {
21 33
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
22 34
     }
23 35
 
24
-    public ResponseEntity<Baker> update(Long id, Baker baker) {
36
+    @PutMapping(path = "/bakers/{id}")
37
+    public ResponseEntity<Baker> update(@PathVariable Long id, Baker baker) {
25 38
         Baker foundBaker = bakerRepository.findOne(id);
26 39
 
27 40
         foundBaker.setName(baker.getName());
@@ -30,8 +43,9 @@ public class BakerController {
30 43
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
31 44
     }
32 45
 
33
-    public ResponseEntity<Boolean> destroy(Long id) {
46
+    @DeleteMapping(path = "/bakers/{id}")
47
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
34 48
         this.bakerRepository.delete(id);
35 49
         return new ResponseEntity<>(true, HttpStatus.OK);
36 50
     }
37
-}
51
+}

+ 12
- 3
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java ファイルの表示

@@ -2,33 +2,42 @@ package com.zipcodewilmington.bakery.Controllers;
2 2
 
3 3
 import com.zipcodewilmington.bakery.Models.Muffin;
4 4
 import com.zipcodewilmington.bakery.Repositories.MuffinRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.http.HttpStatus;
6 7
 import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
7 9
 
10
+@RestController
8 11
 public class MuffinController {
9 12
 
13
+    @Autowired
10 14
     private MuffinRepository muffinRepository;
11 15
 
16
+    @GetMapping(path = "/muffins")
12 17
     public ResponseEntity<Iterable<Muffin>> index() {
13 18
         return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
14 19
     }
15 20
 
16
-    public ResponseEntity<Muffin> show(Long id) {
21
+    @GetMapping(path = "/muffins/{id}")
22
+    public ResponseEntity<Muffin> show(@PathVariable Long id) {
17 23
         return new ResponseEntity<>(this.muffinRepository.findOne(id), HttpStatus.OK);
18 24
     }
19 25
 
26
+    @PostMapping(path = "/muffins")
20 27
     public ResponseEntity<Muffin> create(Muffin muffin) {
21 28
         return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
22 29
     }
23 30
 
24
-    public ResponseEntity<Muffin> update(Long id, Muffin muffin) {
31
+    @PutMapping(path = "/muffins/{id}")
32
+    public ResponseEntity<Muffin> update(@PathVariable Long id, Muffin muffin) {
25 33
         Muffin foundMuffin = muffinRepository.findOne(id);
26 34
         foundMuffin.setFlavor(muffin.getFlavor());
27 35
 
28 36
         return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
29 37
     }
30 38
 
31
-    public ResponseEntity<Boolean> destroy(Long id) {
39
+    @DeleteMapping(path = "/muffins/{id}")
40
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
32 41
         this.muffinRepository.delete(id);
33 42
         return new ResponseEntity<>(true, HttpStatus.OK);
34 43
     }

+ 10
- 1
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java ファイルの表示

@@ -1,6 +1,15 @@
1
-package com.zipcodewilmington.bakery.Models;
1
+package com.zipcodewilmington.bakery.Models;;
2 2
 
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
3 9
 public class Baker {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
4 13
     private Long id;
5 14
 
6 15
     private String name;

+ 8
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java ファイルの表示

@@ -1,7 +1,15 @@
1 1
 package com.zipcodewilmington.bakery.Models;
2 2
 
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
3 9
 public class Muffin {
4 10
 
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
5 13
     private Long id;
6 14
 
7 15
     private String flavor;

+ 4
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java ファイルの表示

@@ -2,6 +2,10 @@ package com.zipcodewilmington.bakery.Repositories;
2 2
 
3 3
 import com.zipcodewilmington.bakery.Models.Baker;
4 4
 import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.data.rest.core.annotation.RepositoryRestResource;
6
+import org.springframework.stereotype.Repository;
5 7
 
8
+@Repository
9
+@RepositoryRestResource(collectionResourceRel = "bakers", path = "bakers")
6 10
 public interface BakerRepository extends CrudRepository<Baker, Long> {
7 11
 }

+ 4
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/MuffinRepository.java ファイルの表示

@@ -2,6 +2,10 @@ package com.zipcodewilmington.bakery.Repositories;
2 2
 
3 3
 import com.zipcodewilmington.bakery.Models.Muffin;
4 4
 import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.data.rest.core.annotation.RepositoryRestResource;
6
+import org.springframework.stereotype.Repository;
5 7
 
8
+@Repository
9
+@RepositoryRestResource(collectionResourceRel = "muffins", path = "muffins")
6 10
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
7 11
 }