Browse Source

baker lab

Zavon Malone 5 years ago
parent
commit
844a53e179

BIN
.DS_Store View File


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

@@ -2,25 +2,34 @@ 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
+@RestController
8 11
 public class BakerController {
12
+
13
+    @Autowired
9 14
     private BakerRepository bakerRepository;
10 15
 
16
+    @GetMapping("/bakers")
11 17
     public ResponseEntity<Iterable<Baker>> index() {
12 18
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
13 19
     }
14 20
 
15
-    public ResponseEntity<Baker> show(Long id) {
21
+    @GetMapping("/bakers/{id}")
22
+    public ResponseEntity<Baker> show(@PathVariable Long id) {
16 23
         return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
17 24
     }
18 25
 
19
-    public ResponseEntity<Baker> create(Baker baker) {
26
+    @PostMapping("/bakers")
27
+    public ResponseEntity<Baker> create(@PathVariable Baker baker) {
20 28
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
21 29
     }
22 30
 
23
-    public ResponseEntity<Baker> update(Long id, Baker baker) {
31
+    @PutMapping("/bakers/{id}")
32
+    public ResponseEntity<Baker> update(@PathVariable Long id, Baker baker) {
24 33
         Baker foundBaker = bakerRepository.findById(id).get();
25 34
 
26 35
         foundBaker.setName(baker.getName());
@@ -29,7 +38,8 @@ public class BakerController {
29 38
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
30 39
     }
31 40
 
32
-    public ResponseEntity<Boolean> destroy(Long id) {
41
+    @DeleteMapping("/bakers/{id}")
42
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
33 43
         this.bakerRepository.findById(id).get();
34 44
         return new ResponseEntity<>(true, HttpStatus.OK);
35 45
     }

+ 13
- 4
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java View File

@@ -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("/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("/muffins/{id}")
22
+    public ResponseEntity<Muffin> show(@PathVariable Long id) {
17 23
         return new ResponseEntity<>(this.muffinRepository.findById(id).get(), HttpStatus.OK);
18 24
     }
19 25
 
20
-    public ResponseEntity<Muffin> create(Muffin muffin) {
26
+    @PostMapping("/muffins")
27
+    public ResponseEntity<Muffin> create(@PathVariable 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("/muffins/{id}")
32
+    public ResponseEntity<Muffin> update(@PathVariable Long id, Muffin muffin) {
25 33
         Muffin foundMuffin = muffinRepository.findById(id).get();
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("/muffins/{id}")
40
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
32 41
         this.muffinRepository.deleteById(id);
33 42
         return new ResponseEntity<>(true, HttpStatus.OK);
34 43
     }

+ 14
- 3
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java View File

@@ -1,12 +1,23 @@
1 1
 package com.zipcodewilmington.bakery.Models;
2 2
 
3
+import com.zipcodewilmington.bakery.Repositories.BakerRepository;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+
6
+import javax.persistence.Entity;
7
+import javax.persistence.GeneratedValue;
8
+import javax.persistence.GenerationType;
9
+import javax.persistence.Id;
10
+
11
+@Entity
3 12
 public class Baker {
4
-    private Long id;
13
+    @Autowired
14
+    BakerRepository bakerRepository;
5 15
 
16
+    @Id
17
+    @GeneratedValue(strategy = GenerationType.AUTO)
18
+    private Long id;
6 19
     private String name;
7
-
8 20
     private String employeeId;
9
-
10 21
     private String specialty;
11 22
 
12 23
     public Baker(String name, String employeeId, String specialty) {

+ 14
- 1
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java View File

@@ -1,8 +1,21 @@
1 1
 package com.zipcodewilmington.bakery.Models;
2 2
 
3
+import com.zipcodewilmington.bakery.Repositories.MuffinRepository;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+
6
+import javax.persistence.Entity;
7
+import javax.persistence.GeneratedValue;
8
+import javax.persistence.GenerationType;
9
+import javax.persistence.Id;
10
+
11
+@Entity
3 12
 public class Muffin {
4
-    private Long id;
13
+    @Autowired
14
+    MuffinRepository muffinRepository;
5 15
 
16
+    @Id
17
+    @GeneratedValue(strategy = GenerationType.AUTO)
18
+    private Long id;
6 19
     private String flavor;
7 20
 
8 21
     public Muffin(String flavor) {

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

@@ -4,5 +4,6 @@ import com.zipcodewilmington.bakery.Models.Baker;
4 4
 import org.springframework.data.repository.CrudRepository;
5 5
 import org.springframework.stereotype.Repository;
6 6
 
7
+@Repository
7 8
 public interface BakerRepository extends CrudRepository<Baker, Long> {
8 9
 }

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

@@ -4,5 +4,6 @@ import com.zipcodewilmington.bakery.Models.Muffin;
4 4
 import org.springframework.data.repository.CrudRepository;
5 5
 import org.springframework.stereotype.Repository;
6 6
 
7
+@Repository
7 8
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
8 9
 }