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