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
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.web.bind.annotation.*;
8
 import org.springframework.web.bind.annotation.*;
9
 
9
 
10
+import javax.persistence.PostUpdate;
11
+
10
 @RestController
12
 @RestController
11
 public class BakerController {
13
 public class BakerController {
14
+
12
     private BakerRepository bakerRepository;
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
     public ResponseEntity<Iterable<Baker>> index() {
24
     public ResponseEntity<Iterable<Baker>> index() {
16
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
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
         Baker foundBaker = bakerRepository.findById(id).get();
40
         Baker foundBaker = bakerRepository.findById(id).get();
30
 
41
 
31
         foundBaker.setName(baker.getName());
42
         foundBaker.setName(baker.getName());
34
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
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
         return new ResponseEntity<>(true, HttpStatus.OK);
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
 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.beans.factory.annotation.Autowired;
6
-import org.springframework.context.annotation.Bean;
7
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.HttpStatus;
8
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.http.ResponseEntity;
9
-import org.springframework.stereotype.Repository;
10
 import org.springframework.web.bind.annotation.*;
8
 import org.springframework.web.bind.annotation.*;
11
 
9
 
12
 @RestController
10
 @RestController
13
 public class MuffinController {
11
 public class MuffinController {
14
 
12
 
15
-    @Autowired
16
     private MuffinRepository muffinRepository;
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
     public ResponseEntity<Iterable<Muffin>> index() {
22
     public ResponseEntity<Iterable<Muffin>> index() {
20
         return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.muffinRepository.findById(id).get(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
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
         Muffin foundMuffin = muffinRepository.findById(id).get();
38
         Muffin foundMuffin = muffinRepository.findById(id).get();
34
         foundMuffin.setFlavor(muffin.getFlavor());
39
         foundMuffin.setFlavor(muffin.getFlavor());
35
 
40
 
36
         return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
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
         return new ResponseEntity<>(true, HttpStatus.OK);
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
 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;
3
+import javax.persistence.*;
7
 
4
 
8
 @Entity
5
 @Entity
9
 public class Baker {
6
 public class Baker {
10
 
7
 
11
     @Id
8
     @Id
12
     @GeneratedValue(strategy = GenerationType.AUTO)
9
     @GeneratedValue(strategy = GenerationType.AUTO)
10
+    @Column(name = "id")
13
     private Long id;
11
     private Long id;
14
 
12
 
13
+    @Column(name = "name")
15
     private String name;
14
     private String name;
16
 
15
 
16
+    @Column(name = "employeeId")
17
     private String employeeId;
17
     private String employeeId;
18
 
18
 
19
+    @Column(name = "specialty")
19
     private String specialty;
20
     private String specialty;
20
 
21
 
22
+    public Baker() {}
23
+
21
     public Baker(String name, String employeeId, String specialty) {
24
     public Baker(String name, String employeeId, String specialty) {
22
         this.name = name;
25
         this.name = name;
23
         this.employeeId = employeeId;
26
         this.employeeId = employeeId;

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

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

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

4
 import org.springframework.data.repository.CrudRepository;
4
 import org.springframework.data.repository.CrudRepository;
5
 import org.springframework.stereotype.Repository;
5
 import org.springframework.stereotype.Repository;
6
 
6
 
7
-@RestController
7
+@Repository
8
 public interface BakerRepository extends CrudRepository<Baker, Long> {
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
 import org.springframework.data.repository.CrudRepository;
4
 import org.springframework.data.repository.CrudRepository;
5
 import org.springframework.stereotype.Repository;
5
 import org.springframework.stereotype.Repository;
6
 
6
 
7
-@RestController
7
+@Repository
8
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
8
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
9
 }
9
 }