#13 Complete

Открыто
demetrm хочет смерджить 3 коммит(ов) из demetrm/CR-MicroLabs-Annotations-Bakery:master в master

Двоичные данные
.DS_Store Просмотреть файл


+ 28
- 28
pom.xml Просмотреть файл

2
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
     <modelVersion>4.0.0</modelVersion>
4
     <modelVersion>4.0.0</modelVersion>
5
-
5
+    <parent>
6
+        <groupId>org.springframework.boot</groupId>
7
+        <artifactId>spring-boot-starter-parent</artifactId>
8
+        <version>2.1.1.RELEASE</version>
9
+        <relativePath/> <!-- lookup parent from repository -->
10
+    </parent>
6
     <groupId>com.zipcodewilmington</groupId>
11
     <groupId>com.zipcodewilmington</groupId>
7
     <artifactId>bakery</artifactId>
12
     <artifactId>bakery</artifactId>
8
     <version>0.0.1-SNAPSHOT</version>
13
     <version>0.0.1-SNAPSHOT</version>
9
     <packaging>jar</packaging>
14
     <packaging>jar</packaging>
10
-
11
     <name>bakery</name>
15
     <name>bakery</name>
12
     <description>Demo project for Spring Boot</description>
16
     <description>Demo project for Spring Boot</description>
13
 
17
 
14
-    <parent>
15
-        <groupId>org.springframework.boot</groupId>
16
-        <artifactId>spring-boot-starter-parent</artifactId>
17
-        <version>2.0.4.RELEASE</version>
18
-        <relativePath/> <!-- lookup parent from repository -->
19
-    </parent>
20
-
21
     <properties>
18
     <properties>
22
-        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23
-        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24
         <java.version>1.8</java.version>
19
         <java.version>1.8</java.version>
25
     </properties>
20
     </properties>
26
 
21
 
27
     <dependencies>
22
     <dependencies>
23
+        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
28
         <dependency>
24
         <dependency>
29
-            <groupId>org.springframework.boot</groupId>
30
-            <artifactId>spring-boot-starter</artifactId>
25
+            <groupId>javax.xml.bind</groupId>
26
+            <artifactId>jaxb-api</artifactId>
27
+            <version>2.3.0</version>
31
         </dependency>
28
         </dependency>
32
-
33
         <dependency>
29
         <dependency>
34
             <groupId>org.springframework.boot</groupId>
30
             <groupId>org.springframework.boot</groupId>
35
-            <artifactId>spring-boot-starter-data-rest</artifactId>
31
+            <artifactId>spring-boot-starter-web</artifactId>
36
         </dependency>
32
         </dependency>
37
-
38
         <dependency>
33
         <dependency>
39
             <groupId>org.springframework.boot</groupId>
34
             <groupId>org.springframework.boot</groupId>
40
-            <artifactId>spring-boot-starter-web</artifactId>
35
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
41
         </dependency>
36
         </dependency>
42
-
43
         <dependency>
37
         <dependency>
44
             <groupId>org.springframework.boot</groupId>
38
             <groupId>org.springframework.boot</groupId>
45
             <artifactId>spring-boot-starter-test</artifactId>
39
             <artifactId>spring-boot-starter-test</artifactId>
46
             <scope>test</scope>
40
             <scope>test</scope>
47
         </dependency>
41
         </dependency>
48
         <dependency>
42
         <dependency>
49
-            <groupId>org.springframework.data</groupId>
50
-            <artifactId>spring-data-commons</artifactId>
43
+            <groupId>org.hsqldb</groupId>
44
+            <artifactId>hsqldb</artifactId>
45
+            <scope>runtime</scope>
51
         </dependency>
46
         </dependency>
52
         <dependency>
47
         <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>
48
+            <groupId>javax.inject</groupId>
49
+            <artifactId>javax.inject</artifactId>
50
+            <version>1</version>
59
         </dependency>
51
         </dependency>
60
         <dependency>
52
         <dependency>
61
-            <groupId>org.springframework.boot</groupId>
62
-            <artifactId>spring-boot-starter-data-jpa</artifactId>
53
+            <groupId>org.mockito</groupId>
54
+            <artifactId>mockito-core</artifactId>
55
+            <version>1.10.19</version>
56
+            <exclusions>
57
+                <exclusion>
58
+                    <groupId>org.hamcrest</groupId>
59
+                    <artifactId>hamcrest-core</artifactId>
60
+                </exclusion>
61
+            </exclusions>
62
+            <scope>test</scope>
63
         </dependency>
63
         </dependency>
64
     </dependencies>
64
     </dependencies>
65
 
65
 

+ 3
- 3
src/main/java/com/zipcodewilmington/bakery/BakeryApplication.java Просмотреть файл

6
 @SpringBootApplication
6
 @SpringBootApplication
7
 public class BakeryApplication {
7
 public class BakeryApplication {
8
 
8
 
9
-	public static void main(String[] args) {
10
-		SpringApplication.run(BakeryApplication.class, args);
11
-	}
9
+    public static void main(String[] args) {
10
+        SpringApplication.run(BakeryApplication.class, args);
11
+    }
12
 }
12
 }

+ 28
- 15
src/main/java/com/zipcodewilmington/bakery/Controllers/BakerController.java Просмотреть файл

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

+ 28
- 12
src/main/java/com/zipcodewilmington/bakery/Controllers/MuffinController.java Просмотреть файл

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

+ 10
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Baker.java Просмотреть файл

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

+ 10
- 0
src/main/java/com/zipcodewilmington/bakery/Models/Muffin.java Просмотреть файл

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

+ 3
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/BakerRepository.java Просмотреть файл

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> {
9
+//    Baker findOne(Long id);
10
+    void deleteById(Long id);
8
 }
11
 }

+ 3
- 0
src/main/java/com/zipcodewilmington/bakery/Repositories/MuffinRepository.java Просмотреть файл

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> {
9
+//    Muffin findOne(Long id);
10
+    void deleteById(Long id);
8
 }
11
 }

+ 40
- 0
src/main/java/com/zipcodewilmington/bakery/Services/BakerService.java Просмотреть файл

1
+package com.zipcodewilmington.bakery.Services;
2
+
3
+import com.zipcodewilmington.bakery.Models.Baker;
4
+import com.zipcodewilmington.bakery.Repositories.BakerRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Service;
7
+
8
+@Service
9
+public class BakerService {
10
+    @Autowired
11
+    private BakerRepository bakerRepository;
12
+
13
+    public BakerService(){}
14
+
15
+    public Iterable<Baker> find() {
16
+        return this.bakerRepository.findAll();
17
+    }
18
+
19
+    public Baker findById(Long id) {
20
+        return this.bakerRepository.findById(id).orElse(null);
21
+    }
22
+
23
+    public Baker create(Baker baker) {
24
+        return this.bakerRepository.save(baker);
25
+    }
26
+
27
+    public Baker update(Long id, Baker baker) {
28
+        Baker foundBaker = bakerRepository.findById(id).orElse(null);
29
+
30
+        foundBaker.setName(baker.getName());
31
+        foundBaker.setSpecialty(baker.getSpecialty());
32
+
33
+        return this.bakerRepository.save(foundBaker);
34
+    }
35
+
36
+    public Boolean destroy(Long id) {
37
+        this.bakerRepository.deleteById(id);
38
+        return true;
39
+    }
40
+}

+ 42
- 0
src/main/java/com/zipcodewilmington/bakery/Services/MuffinService.java Просмотреть файл

1
+package com.zipcodewilmington.bakery.Services;
2
+
3
+import com.zipcodewilmington.bakery.Models.Muffin;
4
+import com.zipcodewilmington.bakery.Repositories.MuffinRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Service;
7
+
8
+@Service
9
+public class MuffinService {
10
+
11
+    private MuffinRepository muffinRepository;
12
+
13
+    @Autowired
14
+    public MuffinService(MuffinRepository muffinRepository) {
15
+        this.muffinRepository = muffinRepository;
16
+    }
17
+
18
+    public Iterable<Muffin> find() {
19
+        return this.muffinRepository.findAll();
20
+    }
21
+
22
+    public Muffin findById(Long id) {
23
+        return this.muffinRepository.findById(id).orElse(null);
24
+    }
25
+
26
+    public Muffin create(Muffin muffin) {
27
+        return this.muffinRepository.save(muffin);
28
+    }
29
+
30
+    public Muffin update(Long id, Muffin muffin) {
31
+        Muffin foundMuffin = muffinRepository.findById(id).orElse(null);
32
+        assert foundMuffin != null;
33
+        foundMuffin.setFlavor(muffin.getFlavor());
34
+
35
+        return this.muffinRepository.save(muffin);
36
+    }
37
+
38
+    public Boolean destroy(Long id) {
39
+        this.muffinRepository.deleteById(id);
40
+        return true;
41
+    }
42
+}

+ 3
- 3
src/test/java/com/zipcodewilmington/bakery/BakeryApplicationTests.java Просмотреть файл

9
 @SpringBootTest
9
 @SpringBootTest
10
 public class BakeryApplicationTests {
10
 public class BakeryApplicationTests {
11
 
11
 
12
-	@Test
13
-	public void contextLoads() {
14
-	}
12
+    @Test
13
+    public void contextLoads() {
14
+    }
15
 
15
 
16
 }
16
 }