Jennifer Chao 5 years ago
parent
commit
e9f8ff7d5a

+ 18
- 4
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java View File

2
 
2
 
3
 import com.zipcodewilmington.bakery.Models.Baker;
3
 import com.zipcodewilmington.bakery.Models.Baker;
4
 import com.zipcodewilmington.bakery.Repositories.BakerRepository;
4
 import com.zipcodewilmington.bakery.Repositories.BakerRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.ResponseEntity;
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
 public class BakerController {
16
 public class BakerController {
9
 
17
 
18
+    @Autowired
10
     private BakerRepository bakerRepository;
19
     private BakerRepository bakerRepository;
11
 
20
 
21
+    @GetMapping(path = "/bakers")
12
     public ResponseEntity<Iterable<Baker>> index() {
22
     public ResponseEntity<Iterable<Baker>> index() {
13
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.bakerRepository.findOne(id), HttpStatus.OK);
28
         return new ResponseEntity<>(this.bakerRepository.findOne(id), HttpStatus.OK);
18
     }
29
     }
19
 
30
 
31
+    @PostMapping(path = "/bakers")
20
     public ResponseEntity<Baker> create(Baker baker) {
32
     public ResponseEntity<Baker> create(Baker baker) {
21
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
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
         Baker foundBaker = bakerRepository.findOne(id);
38
         Baker foundBaker = bakerRepository.findOne(id);
26
 
39
 
27
         foundBaker.setName(baker.getName());
40
         foundBaker.setName(baker.getName());
30
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
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
         this.bakerRepository.delete(id);
48
         this.bakerRepository.delete(id);
35
         return new ResponseEntity<>(true, HttpStatus.OK);
49
         return new ResponseEntity<>(true, HttpStatus.OK);
36
     }
50
     }
37
-}
51
+}

+ 12
- 3
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java View File

2
 
2
 
3
 import com.zipcodewilmington.bakery.Models.Muffin;
3
 import com.zipcodewilmington.bakery.Models.Muffin;
4
 import com.zipcodewilmington.bakery.Repositories.MuffinRepository;
4
 import com.zipcodewilmington.bakery.Repositories.MuffinRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
7
 
9
 
10
+@RestController
8
 public class MuffinController {
11
 public class MuffinController {
9
 
12
 
13
+    @Autowired
10
     private MuffinRepository muffinRepository;
14
     private MuffinRepository muffinRepository;
11
 
15
 
16
+    @GetMapping(path = "/muffins")
12
     public ResponseEntity<Iterable<Muffin>> index() {
17
     public ResponseEntity<Iterable<Muffin>> index() {
13
         return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.muffinRepository.findOne(id), HttpStatus.OK);
23
         return new ResponseEntity<>(this.muffinRepository.findOne(id), HttpStatus.OK);
18
     }
24
     }
19
 
25
 
26
+    @PostMapping(path = "/muffins")
20
     public ResponseEntity<Muffin> create(Muffin muffin) {
27
     public ResponseEntity<Muffin> create(Muffin muffin) {
21
         return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
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
         Muffin foundMuffin = muffinRepository.findOne(id);
33
         Muffin foundMuffin = muffinRepository.findOne(id);
26
         foundMuffin.setFlavor(muffin.getFlavor());
34
         foundMuffin.setFlavor(muffin.getFlavor());
27
 
35
 
28
         return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
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
         this.muffinRepository.delete(id);
41
         this.muffinRepository.delete(id);
33
         return new ResponseEntity<>(true, HttpStatus.OK);
42
         return new ResponseEntity<>(true, HttpStatus.OK);
34
     }
43
     }

+ 10
- 1
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java View File

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
 public class Baker {
9
 public class Baker {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
4
     private Long id;
13
     private Long id;
5
 
14
 
6
     private String name;
15
     private String name;

+ 8
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java View File

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
 public class Muffin {
9
 public class Muffin {
4
 
10
 
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
5
     private Long id;
13
     private Long id;
6
 
14
 
7
     private String flavor;
15
     private String flavor;

+ 4
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java View File

2
 
2
 
3
 import com.zipcodewilmington.bakery.Models.Baker;
3
 import com.zipcodewilmington.bakery.Models.Baker;
4
 import org.springframework.data.repository.CrudRepository;
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
 public interface BakerRepository extends CrudRepository<Baker, Long> {
10
 public interface BakerRepository extends CrudRepository<Baker, Long> {
7
 }
11
 }

+ 4
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/MuffinRepository.java View File

2
 
2
 
3
 import com.zipcodewilmington.bakery.Models.Muffin;
3
 import com.zipcodewilmington.bakery.Models.Muffin;
4
 import org.springframework.data.repository.CrudRepository;
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
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
10
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
7
 }
11
 }