ThuyKhong vor 6 Jahren
Ursprung
Commit
2713c91d43

BIN
.DS_Store Datei anzeigen


+ 8
- 1
pom.xml Datei anzeigen

@@ -48,7 +48,14 @@
48 48
 		<dependency>
49 49
 			<groupId>org.springframework.data</groupId>
50 50
 			<artifactId>spring-data-commons</artifactId>
51
-			<version>1.13.10.RELEASE</version>
51
+		</dependency>
52
+        <dependency>
53
+            <groupId>org.springframework.boot</groupId>
54
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
55
+        </dependency>
56
+		<dependency>
57
+			<groupId>com.h2database</groupId>
58
+			<artifactId>h2</artifactId>
52 59
 		</dependency>
53 60
 	</dependencies>
54 61
 

+ 17
- 6
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java Datei anzeigen

@@ -2,27 +2,37 @@ 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.stereotype.Controller;
9
+import org.springframework.stereotype.Service;
10
+import org.springframework.web.bind.annotation.*;
7 11
 
12
+@RestController
8 13
 public class BakerController {
9 14
 
15
+    @Autowired
10 16
     private BakerRepository bakerRepository;
11 17
 
18
+    @RequestMapping(value = "/bakers", method = RequestMethod.GET)
12 19
     public ResponseEntity<Iterable<Baker>> index() {
13 20
         return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
14 21
     }
15 22
 
16
-    public ResponseEntity<Baker> show(Long id) {
17
-        return new ResponseEntity<>(this.bakerRepository.findOne(id), HttpStatus.OK);
23
+    @RequestMapping(value = "/bakers/id", method = RequestMethod.GET)
24
+    public ResponseEntity<Baker> show(@PathVariable Long id) {
25
+        return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
18 26
     }
19 27
 
20
-    public ResponseEntity<Baker> create(Baker baker) {
28
+    @RequestMapping(value="/bakers", method= RequestMethod.POST)
29
+    public ResponseEntity<Baker> create(@PathVariable Baker baker) {
21 30
         return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
22 31
     }
23 32
 
24
-    public ResponseEntity<Baker> update(Long id, Baker baker) {
25
-        Baker foundBaker = bakerRepository.findOne(id);
33
+    @RequestMapping(value="/bakers/id", method= RequestMethod.PUT)
34
+    public ResponseEntity<Baker> update(Long id, @PathVariable Baker baker) {
35
+        Baker foundBaker = bakerRepository.findById(id).get();
26 36
 
27 37
         foundBaker.setName(baker.getName());
28 38
         foundBaker.setSpecialty(baker.getSpecialty());
@@ -30,8 +40,9 @@ public class BakerController {
30 40
         return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
31 41
     }
32 42
 
43
+    @RequestMapping(value="/bakers/id", method= RequestMethod.DELETE)
33 44
     public ResponseEntity<Boolean> destroy(Long id) {
34
-        this.bakerRepository.delete(id);
45
+        this.bakerRepository.delete(bakerRepository.findById(id).get());
35 46
         return new ResponseEntity<>(true, HttpStatus.OK);
36 47
     }
37 48
 }

+ 17
- 7
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java Datei anzeigen

@@ -2,34 +2,44 @@ 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.*;
7 10
 
11
+@RestController
8 12
 public class MuffinController {
9 13
 
14
+    @Autowired
10 15
     private MuffinRepository muffinRepository;
11 16
 
17
+    @RequestMapping(value="/muffins", method = RequestMethod.GET)
12 18
     public ResponseEntity<Iterable<Muffin>> index() {
13 19
         return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
14 20
     }
15 21
 
16
-    public ResponseEntity<Muffin> show(Long id) {
17
-        return new ResponseEntity<>(this.muffinRepository.findOne(id), HttpStatus.OK);
22
+    @RequestMapping(value = "/muffins/id", method=RequestMethod.GET)
23
+    public ResponseEntity<Muffin> show(@PathVariable Long id) {
24
+        return new ResponseEntity<>(this.muffinRepository.findById(id).get(), HttpStatus.OK);
18 25
     }
19 26
 
20
-    public ResponseEntity<Muffin> create(Muffin muffin) {
27
+    @RequestMapping(value = "/muffins", method = RequestMethod.POST)
28
+    public ResponseEntity<Muffin> create(@RequestBody Muffin muffin) {
21 29
         return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
22 30
     }
23 31
 
24
-    public ResponseEntity<Muffin> update(Long id, Muffin muffin) {
25
-        Muffin foundMuffin = muffinRepository.findOne(id);
32
+    @RequestMapping(value = "/muffin/id", method=RequestMethod.PUT)
33
+    public ResponseEntity<Muffin> update(@PathVariable Long id, @RequestBody Muffin muffin) {
34
+        Muffin foundMuffin = muffinRepository.findById(id).get();
26 35
         foundMuffin.setFlavor(muffin.getFlavor());
27 36
 
28 37
         return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
29 38
     }
30 39
 
31
-    public ResponseEntity<Boolean> destroy(Long id) {
32
-        this.muffinRepository.delete(id);
40
+    @RequestMapping(value = "/muffins/id", method = RequestMethod.DELETE)
41
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
42
+        this.muffinRepository.delete(muffinRepository.findById(id).get());
33 43
         return new ResponseEntity<>(true, HttpStatus.OK);
34 44
     }
35 45
 

+ 10
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java Datei anzeigen

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

+ 10
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java Datei anzeigen

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

+ 2
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java Datei anzeigen

@@ -2,6 +2,8 @@ package com.zipcodewilmington.bakery.Repositories;
2 2
 
3 3
 import com.zipcodewilmington.bakery.Models.Baker;
4 4
 import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.stereotype.Repository;
5 6
 
7
+@Repository
6 8
 public interface BakerRepository extends CrudRepository<Baker, Long> {
7 9
 }

+ 2
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/MuffinRepository.java Datei anzeigen

@@ -2,6 +2,8 @@ package com.zipcodewilmington.bakery.Repositories;
2 2
 
3 3
 import com.zipcodewilmington.bakery.Models.Muffin;
4 4
 import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.stereotype.Repository;
5 6
 
7
+@Repository
6 8
 public interface MuffinRepository extends CrudRepository<Muffin, Long> {
7 9
 }

+ 1
- 0
src/main/resources/application.properties Datei anzeigen

@@ -0,0 +1 @@
1
+server.port=8081

+ 22
- 9
src/test/java/com/zipcodewilmington/bakery/BakeryApplicationTests.java Datei anzeigen

@@ -1,16 +1,29 @@
1 1
 package com.zipcodewilmington.bakery;
2 2
 
3
+import com.zipcodewilmington.bakery.Controllers.BakerController;
4
+import com.zipcodewilmington.bakery.Controllers.MuffinController;
5
+import org.junit.Assert;
3 6
 import org.junit.Test;
4 7
 import org.junit.runner.RunWith;
8
+import org.springframework.beans.factory.annotation.Autowired;
5 9
 import org.springframework.boot.test.context.SpringBootTest;
6 10
 import org.springframework.test.context.junit4.SpringRunner;
7 11
 
8
-@RunWith(SpringRunner.class)
9
-@SpringBootTest
10
-public class BakeryApplicationTests {
11
-
12
-	@Test
13
-	public void contextLoads() {
14
-	}
15
-
16
-}
12
+//@RunWith(SpringRunner.class)
13
+//@SpringBootTest
14
+//public class BakeryApplicationTests {
15
+//
16
+//	@Autowired
17
+//	private BakerController bakerController;
18
+//
19
+//	@Autowired
20
+//	private MuffinController muffinController;
21
+//
22
+//	@Test
23
+//	public void contextLoads() {
24
+//		Assert.assertNotNull(bakerController);
25
+//		Assert.assertNotNull(muffinController);
26
+//	}
27
+//
28
+//
29
+//}