NiraParikh 5 yıl önce
ebeveyn
işleme
898be085a3

+ 19
- 7
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java Dosyayı Görüntüle

@@ -7,25 +7,36 @@ import org.springframework.http.HttpStatus;
7 7
 import org.springframework.http.ResponseEntity;
8 8
 import org.springframework.web.bind.annotation.*;
9 9
 
10
+import javax.persistence.PostUpdate;
11
+
10 12
 @RestController
11 13
 public class BakerController {
14
+
12 15
     private BakerRepository bakerRepository;
13 16
 
14
-    @GetMapping("/Baker")
17
+    @Autowired
18
+    public BakerController(BakerRepository bakerRepository) {
19
+        this.bakerRepository = bakerRepository;
20
+    }
21
+
22
+
23
+    @GetMapping("/Baker/")
15 24
     public ResponseEntity<Iterable<Baker>> index() {
16 25
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
17 26
     }
18 27
 
19
-    public ResponseEntity<Baker> show(Long id) {
28
+    @GetMapping("/Baker/{id}")
29
+    public ResponseEntity<Baker> show(@PathVariable Long id) {
20 30
         return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
21 31
     }
22 32
 
23
-    @PostMapping("/Baker")
24
-    public ResponseEntity<Baker> create(Baker baker) {
33
+    @PostMapping("/Baker/")
34
+    public ResponseEntity<Baker> create(@RequestBody Baker baker) {
25 35
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
26 36
     }
27 37
 
28
-    public ResponseEntity<Baker> update(Long id, Baker baker) {
38
+    @PutMapping("/Baker/{id}")
39
+    public ResponseEntity<Baker> update(@PathVariable Long id, @RequestBody Baker baker) {
29 40
         Baker foundBaker = bakerRepository.findById(id).get();
30 41
 
31 42
         foundBaker.setName(baker.getName());
@@ -34,8 +45,9 @@ public class BakerController {
34 45
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
35 46
     }
36 47
 
37
-    public ResponseEntity<Boolean> destroy(Long id) {
38
-        this.bakerRepository.findById(id).get();
48
+    @DeleteMapping("/Baker/{id}")
49
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
50
+        this.bakerRepository.delete(this.bakerRepository.findById(id).get());
39 51
         return new ResponseEntity<>(true, HttpStatus.OK);
40 52
     }
41 53
 }

+ 16
- 10
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java Dosyayı Görüntüle

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

+ 7
- 4
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java Dosyayı Görüntüle

@@ -1,23 +1,26 @@
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;
3
+import javax.persistence.*;
7 4
 
8 5
 @Entity
9 6
 public class Baker {
10 7
 
11 8
     @Id
12 9
     @GeneratedValue(strategy = GenerationType.AUTO)
10
+    @Column(name = "id")
13 11
     private Long id;
14 12
 
13
+    @Column(name = "name")
15 14
     private String name;
16 15
 
16
+    @Column(name = "employeeId")
17 17
     private String employeeId;
18 18
 
19
+    @Column(name = "specialty")
19 20
     private String specialty;
20 21
 
22
+    public Baker() {}
23
+
21 24
     public Baker(String name, String employeeId, String specialty) {
22 25
         this.name = name;
23 26
         this.employeeId = employeeId;

+ 8
- 4
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java Dosyayı Görüntüle

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

+ 1
- 1
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java Dosyayı Görüntüle

@@ -4,6 +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
-@RestController
7
+@Repository
8 8
 public interface BakerRepository extends CrudRepository<Baker, Long> {
9 9
 }

+ 1
- 1
src/main/java/com/zipcodewilmington/bakery/Repositories/MuffinRepository.java Dosyayı Görüntüle

@@ -4,6 +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
-@RestController
7
+@Repository
8 8
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
9 9
 }