nafis nibir 6 vuotta sitten
vanhempi
commit
4e41a9f9ad

+ 4
- 0
pom.xml Näytä tiedosto

@@ -40,6 +40,10 @@
40 40
             <scope>runtime</scope>
41 41
         </dependency>
42 42
 
43
+        <dependency>
44
+            <groupId>org.springframework.boot</groupId>
45
+            <artifactId>spring-boot-starter-data-rest</artifactId>
46
+        </dependency>
43 47
         <!--<dependency>-->
44 48
             <!--<groupId>com.h2database</groupId>-->
45 49
             <!--<artifactId>h2</artifactId>-->

+ 2
- 1
src/main/java/com/ziplinegreen/vault/Controller/UserController.java Näytä tiedosto

@@ -3,6 +3,7 @@ package com.ziplinegreen.vault.Controller;
3 3
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4 4
 import com.ziplinegreen.vault.Model.User;
5 5
 import com.ziplinegreen.vault.Repository.UserRepository;
6
+import com.ziplinegreen.vault.Service.UserService;
6 7
 import org.springframework.beans.factory.annotation.Autowired;
7 8
 import org.springframework.data.domain.Page;
8 9
 import org.springframework.data.domain.Pageable;
@@ -15,7 +16,7 @@ import javax.validation.Valid;
15 16
 public class UserController {
16 17
 
17 18
     @Autowired
18
-    private UserRepository userRepository;
19
+    private UserService userService;
19 20
 
20 21
     @GetMapping("/users")
21 22
     public Page<User> getAllPosts(Pageable pageable) {

+ 16
- 2
src/main/java/com/ziplinegreen/vault/Model/User.java Näytä tiedosto

@@ -13,13 +13,27 @@ public class User extends AuditModel{
13 13
 //    @NotNull
14 14
     @Column(name ="USER_USERNAME")
15 15
     private String username;
16
+    private String email;
17
+    private String password;
16 18
 
17 19
     public Long getId() {
18 20
         return id;
19 21
     }
20 22
 
21
-    public void setId(Long id) {
22
-        this.id = id;
23
+    public String getEmail() {
24
+        return email;
25
+    }
26
+
27
+    public void setEmail(String email) {
28
+        this.email = email;
29
+    }
30
+
31
+    public String getPassword() {
32
+        return password;
33
+    }
34
+
35
+    public void setPassword(String password) {
36
+        this.password = password;
23 37
     }
24 38
 
25 39
     public String getUsername() {

+ 7
- 2
src/main/java/com/ziplinegreen/vault/Repository/PostRepository.java Näytä tiedosto

@@ -4,9 +4,14 @@ import com.ziplinegreen.vault.Model.Post;
4 4
 import org.springframework.data.domain.Page;
5 5
 import org.springframework.data.domain.Pageable;
6 6
 import org.springframework.data.jpa.repository.JpaRepository;
7
+import org.springframework.data.jpa.repository.Query;
8
+import org.springframework.data.repository.query.Param;
9
+import org.springframework.data.rest.core.annotation.RepositoryRestResource;
7 10
 import org.springframework.stereotype.Repository;
8 11
 
9
-@Repository
12
+@RepositoryRestResource
10 13
 public interface PostRepository extends JpaRepository<Post,Long> {
11
-    Page<Post> findByUserId(Long userId, Pageable pageable);
14
+
15
+    @Query("select postcontent,timestamp from Post where user_id = :userId")
16
+    Page<Post> findByUserId(@Param("userId")Long userId, Pageable pageable);
12 17
 }

+ 2
- 1
src/main/java/com/ziplinegreen/vault/Repository/UserRepository.java Näytä tiedosto

@@ -2,8 +2,9 @@ package com.ziplinegreen.vault.Repository;
2 2
 
3 3
 import com.ziplinegreen.vault.Model.User;
4 4
 import org.springframework.data.jpa.repository.JpaRepository;
5
+import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5 6
 import org.springframework.stereotype.Repository;
6 7
 
7
-@Repository
8
+@RepositoryRestResource
8 9
 public interface UserRepository extends JpaRepository<User,Long> {
9 10
 }

+ 31
- 0
src/main/java/com/ziplinegreen/vault/Service/PostService.java Näytä tiedosto

@@ -0,0 +1,31 @@
1
+package com.ziplinegreen.vault.Service;
2
+
3
+
4
+import com.ziplinegreen.vault.Model.Post;
5
+import com.ziplinegreen.vault.Repository.PostRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.stereotype.Service;
10
+
11
+@Service
12
+public class PostService {
13
+
14
+    private PostRepository postRepository;
15
+
16
+    @Autowired
17
+    public PostService(PostRepository postRepository) {
18
+        this.postRepository = postRepository;
19
+    }
20
+
21
+    public ResponseEntity<Post> createPost(Post post){
22
+        return new ResponseEntity<>(postRepository.save(post), HttpStatus.CREATED);
23
+    }
24
+
25
+    public ResponseEntity<Post> deletePost(Long id){
26
+        Post post = postRepository.getOne(id);
27
+        postRepository.delete(post);
28
+        return new ResponseEntity<>(HttpStatus.OK);
29
+    }
30
+}
31
+

+ 41
- 0
src/main/java/com/ziplinegreen/vault/Service/UserService.java Näytä tiedosto

@@ -0,0 +1,41 @@
1
+package com.ziplinegreen.vault.Service;
2
+
3
+import com.ziplinegreen.vault.Controller.PostController;
4
+import com.ziplinegreen.vault.Model.Post;
5
+import com.ziplinegreen.vault.Model.User;
6
+import com.ziplinegreen.vault.Repository.UserRepository;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.stereotype.Service;
11
+
12
+import javax.xml.ws.Response;
13
+
14
+@Service
15
+public class UserService {
16
+
17
+    private UserRepository userRepository;
18
+    private PostController postController;
19
+
20
+    @Autowired
21
+    public UserService(UserRepository userRepository, PostController postController) {
22
+        this.userRepository = userRepository;
23
+        this.postController = postController;
24
+    }
25
+
26
+    public ResponseEntity<User> createUser(User user){
27
+        return new ResponseEntity<>(userRepository.save(user), HttpStatus.CREATED);
28
+    }
29
+
30
+    public ResponseEntity<User> deleteUser(Long id){
31
+        User user = userRepository.getOne(id);
32
+        userRepository.delete(user);
33
+        return new ResponseEntity<>(HttpStatus.OK);
34
+    }
35
+
36
+//    public ResponseEntity<User> updatePost(Long userId){
37
+//        User user = userRepository.getOne(userId);
38
+//
39
+//    }
40
+
41
+}