Chaitali Patel před 5 roky
rodič
revize
bab96e2e20

binární
.DS_Store Zobrazit soubor


+ 25
- 5
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java Zobrazit soubor

2
 
2
 
3
 import com.zipcodewilmington.bakery.Models.Baker;
3
 import com.zipcodewilmington.bakery.Models.Baker;
4
 import com.zipcodewilmington.bakery.Repositories.BakerRepository;
4
 import com.zipcodewilmington.bakery.Repositories.BakerRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
7
 
9
 
10
+
11
+@RestController
8
 public class BakerController {
12
 public class BakerController {
13
+
14
+
9
     private BakerRepository bakerRepository;
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
     public ResponseEntity<Iterable<Baker>> index() {
26
     public ResponseEntity<Iterable<Baker>> index() {
12
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
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
         Baker foundBaker = bakerRepository.findById(id).get();
43
         Baker foundBaker = bakerRepository.findById(id).get();
25
 
44
 
26
         foundBaker.setName(baker.getName());
45
         foundBaker.setName(baker.getName());
29
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
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
         return new ResponseEntity<>(true, HttpStatus.OK);
54
         return new ResponseEntity<>(true, HttpStatus.OK);
35
     }
55
     }
36
 }
56
 }

+ 21
- 5
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java Zobrazit soubor

2
 
2
 
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.http.HttpStatus;
6
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
7
 
9
 
10
+@RestController
8
 public class MuffinController {
11
 public class MuffinController {
9
 
12
 
10
     private MuffinRepository muffinRepository;
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
     public ResponseEntity<Iterable<Muffin>> index() {
24
     public ResponseEntity<Iterable<Muffin>> index() {
13
         return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.muffinRepository.findById(id).get(), HttpStatus.OK);
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
         return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
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
         Muffin foundMuffin = muffinRepository.findById(id).get();
40
         Muffin foundMuffin = muffinRepository.findById(id).get();
26
         foundMuffin.setFlavor(muffin.getFlavor());
41
         foundMuffin.setFlavor(muffin.getFlavor());
27
 
42
 
28
         return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
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
         return new ResponseEntity<>(true, HttpStatus.OK);
49
         return new ResponseEntity<>(true, HttpStatus.OK);
34
     }
50
     }
35
 
51
 

+ 10
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java Zobrazit soubor

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

+ 8
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java Zobrazit soubor

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

+ 1
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java Zobrazit soubor

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

+ 1
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/MuffinRepository.java Zobrazit soubor

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