Chaitali Patel 5 vuotta sitten
vanhempi
commit
bab96e2e20

BIN
.DS_Store Näytä tiedosto


+ 25
- 5
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java Näytä tiedosto

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

+ 21
- 5
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java Näytä tiedosto

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

+ 10
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java Näytä tiedosto

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

+ 8
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java Näytä tiedosto

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

+ 1
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java Näytä tiedosto

@@ -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 Näytä tiedosto

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