Nuridalia.Hernandez 5 years ago
parent
commit
b793aa0993

+ 19
- 6
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java View File

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

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

@@ -2,33 +2,52 @@ 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
+
10 14
     private MuffinRepository muffinRepository;
11 15
 
16
+
17
+    @Autowired
18
+    public MuffinController(MuffinRepository muffinRepository) {
19
+        this.muffinRepository = muffinRepository;
20
+    }
21
+
22
+    @RequestMapping(value = "/muffin", method = RequestMethod.GET)
12 23
     public ResponseEntity<Iterable<Muffin>> index() {
13 24
         return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
14 25
     }
15 26
 
16
-    public ResponseEntity<Muffin> show(Long id) {
27
+
28
+    @RequestMapping(value= "/muffin/{id}", method = RequestMethod.GET)
29
+    public ResponseEntity<Muffin> show(@PathVariable Long id) {
17 30
         return new ResponseEntity<>(this.muffinRepository.findById(id).get(), HttpStatus.OK);
18 31
     }
19 32
 
20
-    public ResponseEntity<Muffin> create(Muffin muffin) {
33
+
34
+@RequestMapping(value = "/muffin", method = RequestMethod.POST)
35
+    public ResponseEntity<Muffin> create(@RequestBody Muffin muffin) {
21 36
         return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
22 37
     }
23 38
 
24
-    public ResponseEntity<Muffin> update(Long id, Muffin muffin) {
39
+
40
+@RequestMapping(value = "muffin/{id}", method = RequestMethod.PUT)
41
+    public ResponseEntity<Muffin> update(@PathVariable Long id, @RequestBody Muffin muffin) {
25 42
         Muffin foundMuffin = muffinRepository.findById(id).get();
26 43
         foundMuffin.setFlavor(muffin.getFlavor());
27 44
 
28 45
         return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
29 46
     }
30 47
 
31
-    public ResponseEntity<Boolean> destroy(Long id) {
48
+
49
+@RequestMapping(value = "/muffin/{id}", method = RequestMethod.DELETE)
50
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
32 51
         this.muffinRepository.deleteById(id);
33 52
         return new ResponseEntity<>(true, HttpStatus.OK);
34 53
     }

+ 12
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java View File

@@ -1,6 +1,15 @@
1 1
 package com.zipcodewilmington.bakery.Models;
2 2
 
3
+
4
+import javax.persistence.Entity;
5
+import javax.persistence.GeneratedValue;
6
+import javax.persistence.GenerationType;
7
+import javax.persistence.Id;
8
+
9
+@Entity
3 10
 public class Baker {
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
4 13
     private Long id;
5 14
 
6 15
     private String name;
@@ -9,6 +18,9 @@ public class Baker {
9 18
 
10 19
     private String specialty;
11 20
 
21
+    public Baker() {
22
+    }
23
+
12 24
     public Baker(String name, String employeeId, String specialty) {
13 25
         this.name = name;
14 26
         this.employeeId = employeeId;

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

@@ -1,10 +1,21 @@
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 {
10
+    @Id
11
+    @GeneratedValue(strategy = GenerationType.AUTO)
4 12
     private Long id;
5 13
 
6 14
     private String flavor;
7 15
 
16
+    public Muffin() {
17
+    }
18
+
8 19
     public Muffin(String flavor) {
9 20
         this.flavor = flavor;
10 21
     }

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

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

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