Bläddra i källkod

before move to github

Kr Younger 6 år sedan
förälder
incheckning
2851629238

+ 23
- 0
src/main/java/rocks/zipcode/weblogg/controller/HomeController.java Visa fil

@@ -1,20 +1,33 @@
1 1
 package rocks.zipcode.weblogg.controller;
2 2
 
3 3
 import java.util.List;
4
+import java.util.Optional;
4 5
 
5 6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable;
8
+import org.springframework.data.domain.Page;
6 9
 import org.springframework.stereotype.Controller;
7 10
 import org.springframework.ui.Model;
11
+import org.springframework.web.bind.annotation.PathVariable;
8 12
 import org.springframework.web.bind.annotation.RequestMapping;
9 13
 import org.springframework.web.bind.annotation.RequestMethod;
10 14
 
15
+import rocks.zipcode.weblogg.model.Comment;
11 16
 import rocks.zipcode.weblogg.model.Post;
17
+import rocks.zipcode.weblogg.model.User;
18
+import rocks.zipcode.weblogg.repository.UserRepository;
12 19
 
13 20
 @Controller
14 21
 public class HomeController {
15 22
     @Autowired
16 23
     PostController postController;
17 24
 
25
+    @Autowired
26
+    CommentController commentController;
27
+
28
+    @Autowired
29
+    UserRepository userRepository;
30
+
18 31
     @RequestMapping("/")
19 32
     public String index() {
20 33
         return "index";
@@ -23,7 +36,17 @@ public class HomeController {
23 36
     @RequestMapping(value = "/list", method = RequestMethod.GET)
24 37
     public String postList(Model model) {
25 38
         List<Post> posts = postController.listAll();
39
+        // List<Comment> comments = commentController.getAllCommentsByPostId(postId, pageable);
26 40
         model.addAttribute("posts", posts);
27 41
         return "list";
28 42
     }
43
+    @RequestMapping(value = "/user/{userid}", method = RequestMethod.GET)
44
+    public String showUser(@PathVariable Long userid, Model model) {
45
+        Optional<User> usr = userRepository.findById(userid);
46
+        if (usr.isPresent()){
47
+            User foo = usr.get();
48
+            model.addAttribute("user", foo);
49
+        } 
50
+        return "user";
51
+    }
29 52
 }

+ 0
- 1
src/main/java/rocks/zipcode/weblogg/model/Comment.java Visa fil

@@ -54,5 +54,4 @@ public class Comment extends AuditModel {
54 54
         this.post = post;
55 55
     }
56 56
 
57
-    // Getters and Setters (Omitted for brevity)
58 57
 }

+ 18
- 0
src/main/java/rocks/zipcode/weblogg/model/Post.java Visa fil

@@ -3,6 +3,9 @@ package rocks.zipcode.weblogg.model;
3 3
 import javax.persistence.*;
4 4
 import javax.validation.constraints.NotNull;
5 5
 import javax.validation.constraints.Size;
6
+import com.fasterxml.jackson.annotation.JsonIgnore;
7
+import org.hibernate.annotations.OnDelete;
8
+import org.hibernate.annotations.OnDeleteAction;
6 9
 
7 10
 @Entity
8 11
 @Table(name = "posts")
@@ -28,6 +31,20 @@ public class Post extends AuditModel {
28 31
     @Lob
29 32
     private String content;
30 33
 
34
+    @ManyToOne(fetch = FetchType.LAZY, optional = false)
35
+    @JoinColumn(name = "user_id", nullable = false)
36
+    @OnDelete(action = OnDeleteAction.CASCADE)
37
+    @JsonIgnore
38
+    private User user;
39
+
40
+    public User getUser() {
41
+        return this.user;
42
+    }
43
+
44
+    public void setUser(User user) {
45
+        this.user = user;
46
+    }
47
+   
31 48
     public Post() {
32 49
     }
33 50
 
@@ -68,5 +85,6 @@ public class Post extends AuditModel {
68 85
     public void setContent(String content) {
69 86
         this.content = content;
70 87
     }
88
+ 
71 89
 
72 90
 }

+ 90
- 0
src/main/java/rocks/zipcode/weblogg/model/User.java Visa fil

@@ -0,0 +1,90 @@
1
+package rocks.zipcode.weblogg.model;
2
+
3
+import java.util.HashSet;
4
+import java.util.Set;
5
+
6
+import javax.persistence.*;
7
+import javax.validation.constraints.NotNull;
8
+import javax.validation.constraints.Size;
9
+
10
+
11
+@Entity
12
+@Table(name = "users")
13
+public class User {
14
+
15
+    @Id
16
+    @GeneratedValue(strategy = GenerationType.SEQUENCE) // GenerationType.IDENTITY)
17
+    private Long id;
18
+    @NotNull
19
+    @Size(max = 250)
20
+    private String username;
21
+    @NotNull
22
+    @Size(max = 250)
23
+    private String passwordHash;
24
+    @NotNull
25
+    @Size(max = 250)
26
+    private String fullName;
27
+    
28
+    @OneToMany(mappedBy = "user")
29
+    private Set<Post> posts = new HashSet<Post>();
30
+
31
+    public User(String username, String passwordHash, String fullName) {
32
+        this.username = username;
33
+        this.passwordHash = passwordHash;
34
+        this.fullName = fullName;
35
+    }
36
+
37
+    public User() {
38
+    }
39
+
40
+    @Override
41
+    public String toString() {
42
+        return "{" + " id='" + getId() + "'" + ", username='" + getUsername() + "'" + ", passwordHash='"
43
+                + getPasswordHash() + "'" + ", fullName='" + getFullName() + "'" + "}";
44
+    }
45
+
46
+    public Long getId() {
47
+        return this.id;
48
+    }
49
+
50
+    public void setId(Long id) {
51
+        this.id = id;
52
+    }
53
+
54
+    public String getUsername() {
55
+        return this.username;
56
+    }
57
+
58
+    public void setUsername(String username) {
59
+        this.username = username;
60
+    }
61
+
62
+    public String getPasswordHash() {
63
+        return this.passwordHash;
64
+    }
65
+
66
+    public void setPasswordHash(String passwordHash) {
67
+        this.passwordHash = passwordHash;
68
+    }
69
+
70
+    public String getFullName() {
71
+        return this.fullName;
72
+    }
73
+
74
+    public void setFullName(String fullName) {
75
+        this.fullName = fullName;
76
+    }
77
+
78
+    public Set<Post> getPosts() {
79
+        return this.posts;
80
+    }
81
+
82
+    public void setPosts(Set<Post> posts) {
83
+        this.posts = posts;
84
+    }
85
+
86
+    public void addPost(Post post) {
87
+        this.posts.add(post);
88
+    }
89
+
90
+}

+ 60
- 22
src/main/java/rocks/zipcode/weblogg/repository/PostCLR.java Visa fil

@@ -6,33 +6,71 @@ import org.springframework.beans.factory.annotation.Autowired;
6 6
 import org.springframework.boot.CommandLineRunner;
7 7
 import org.springframework.stereotype.Component;
8 8
 
9
+import rocks.zipcode.weblogg.model.Comment;
9 10
 import rocks.zipcode.weblogg.model.Post;
11
+import rocks.zipcode.weblogg.model.User;
10 12
 
11 13
 @Component
12 14
 public class PostCLR implements CommandLineRunner {
13
- private static final Logger log = Logger.getLogger("PostCLR");
14
- @Autowired
15
- private PostRepository postRepo;
16
-
17
- @Override
18
- public void run(String...args) {
19
-  log.info("-------------------------------");
20
-  log.info("Adding Demo Posts.");
21
-  log.info("-------------------------------");
22
-  if (!postRepo.existsByTitle("Hello World!")) { 
23
-    Post tom = new Post("Hello World!", "It's great to be here. Wonderful, really.");
24
-    postRepo.save(tom);
25
-  }
26
-  if (!postRepo.existsByTitle("Another Incredible!")) { 
27
-    Post tom = new Post("Another Incredible!", "It's great to be here. Wonderful, really.");
28
-    postRepo.save(tom);
29
-  }
15
+  private static final Logger log = Logger.getLogger("PostCLR");
16
+  @Autowired
17
+  private PostRepository postRepo;
30 18
 
31
-  if (!postRepo.existsByTitle("Interesting Lorem!")) { 
32
-    Post tom = new Post("Interesting Lorem!", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
33
-    postRepo.save(tom);
34
-  }
19
+  @Autowired
20
+  private UserRepository userRepo;
21
+
22
+  @Autowired
23
+  private CommentRepository cmmtRepo;
24
+
25
+  @Override
26
+  public void run(String... args) {
27
+    User usr;
35 28
 
36
- }
29
+    log.info("-------------------------------");
30
+    log.info("Deleting all Demo data.");
31
+    log.info("-------------------------------");
32
+    cmmtRepo.deleteAllInBatch();
33
+    postRepo.deleteAllInBatch();
34
+    userRepo.deleteAllInBatch();
35
+
36
+    log.info("-------------------------------");
37
+    log.info("Adding Demo User.");
38
+    log.info("-------------------------------");
39
+    if (!userRepo.existsByUsername("anony@mous.coward")) {
40
+      // User(String username, String passwordHash, String fullName)
41
+      usr = new User("anony@mous.coward", "", "Anonymous Coward");
42
+      userRepo.save(usr);
43
+    } else {
44
+      usr = userRepo.findByUsername("anony@mous.coward");
45
+    }
46
+    log.info("-------------------------------");
47
+    log.info("Adding Demo Posts.");
48
+    log.info("-------------------------------");
49
+    if (!postRepo.existsByTitle("Hello World!")) {
50
+      Post tom = new Post("Hello World!", "It's great to be here. Wonderful, really.");
51
+      tom.setUser(usr);
52
+      usr.addPost(tom);
53
+      postRepo.save(tom);
54
+      Comment cmt = new Comment();
55
+      cmt.setText("amazing!");
56
+      cmt.setPost(tom);
57
+      cmmtRepo.save(cmt);
58
+    }
59
+    if (!postRepo.existsByTitle("Another Incredible!")) {
60
+      Post tom = new Post("Another Incredible!", "It's great to be here. Wonderful, really.");
61
+      usr.addPost(tom);
62
+      tom.setUser(usr);
63
+      postRepo.save(tom);
64
+    }
65
+
66
+    if (!postRepo.existsByTitle("Interesting Lorem!")) {
67
+      Post tom = new Post("Interesting Lorem!",
68
+          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
69
+      usr.addPost(tom);
70
+      tom.setUser(usr);
71
+      postRepo.save(tom);
72
+    }
73
+
74
+  }
37 75
 
38 76
 }

+ 15
- 0
src/main/java/rocks/zipcode/weblogg/repository/UserRepository.java Visa fil

@@ -0,0 +1,15 @@
1
+package rocks.zipcode.weblogg.repository;
2
+
3
+import org.springframework.data.jpa.repository.JpaRepository;
4
+import org.springframework.stereotype.Repository;
5
+import org.springframework.transaction.annotation.Transactional;
6
+
7
+import rocks.zipcode.weblogg.model.User;
8
+
9
+@Repository
10
+@Transactional // do you  need this for mysql??
11
+public interface UserRepository extends JpaRepository<User, Long> {
12
+
13
+    boolean existsByUsername(String username);
14
+    User findByUsername(String username);
15
+}

+ 2
- 0
src/main/resources/application.properties Visa fil

@@ -13,3 +13,5 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialec
13 13
 spring.jpa.hibernate.ddl-auto = update
14 14
 
15 15
 spring.thymeleaf.cache = false
16
+
17
+debug = true

+ 1
- 1
src/main/resources/templates/list.html Visa fil

@@ -20,7 +20,7 @@
20 20
                     <div class="card-body">
21 21
                         <h5 class="card-title" th:text="${post.title}"></h5>
22 22
                         <p class="card-text" th:text="${post.content}"></p>
23
-                        <p><a href="#" class="btn btn-primary">Details...</a>&nbsp;
23
+                        <p><a th:href="@{'/user/' + ${post.user.id}}" class="btn btn-primary" th:text="${post.user.username}"></a>&nbsp;
24 24
                             <small><small><span style="float: right" th:text="${post.createdAt}"></span></small></small></p>
25 25
                     </div>
26 26
                 </div>

+ 28
- 0
src/main/resources/templates/user.html Visa fil

@@ -0,0 +1,28 @@
1
+<!DOCTYPE html>
2
+<html xmlns:th="http://www.thymeleaf.org">
3
+
4
+<head>
5
+    <meta charset="UTF-8" />
6
+    <title>Blog</title>
7
+
8
+    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
9
+        crossorigin="anonymous">
10
+
11
+</head>
12
+
13
+<body>
14
+    <div class="container">
15
+        <div class="jumbotron">
16
+            <h1>User Details</h1>
17
+            <h3 th:text="${user.fullName}"></h3>
18
+
19
+            <p th:text="${user.username}"></p>
20
+
21
+            <p><a href="/list" class="btn btn-primary">Show Posts...</a></p>
22
+            <p><a href="/" class="btn btn-primary">Home...</a></p>
23
+
24
+        </div>
25
+    </div>
26
+</body>
27
+
28
+</html>