From 4755cacc4f74d04bdcf826930f20869fe1d79ef1 Mon Sep 17 00:00:00 2001 From: mohnalkhateeb Date: Mon, 25 Oct 2021 10:32:10 +0300 Subject: [PATCH 1/2] temp --- build.gradle | 3 ++ .../init/controllers/QuizController.java | 8 ++++-- .../java/com/example/init/models/Quiz.java | 28 +++++++++++++++++-- src/main/resources/templates/answers.html | 23 +++++++++++++++ src/main/resources/templates/quiz.html | 20 ++++++------- 5 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/templates/answers.html diff --git a/build.gradle b/build.gradle index 8d03cce..421af9d 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,9 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' implementation 'com.google.code.gson:gson:2.8.8' + +//Thanks for using https://jar-download.com + } test { diff --git a/src/main/java/com/example/init/controllers/QuizController.java b/src/main/java/com/example/init/controllers/QuizController.java index 794de6b..e2ddfc2 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -2,20 +2,22 @@ import com.example.init.models.Quiz; import com.google.gson.Gson; +//import com.google.gson.reflect.TypeToken; + import com.google.gson.reflect.TypeToken; + import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.io.BufferedReader; -import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; -import java.util.ArrayList; import java.util.List; +import java.util.Map; @Controller public class QuizController { @@ -34,8 +36,8 @@ public String getQuiz(Model model) throws IOException { Type jsonCasting = new TypeToken>(){}.getType(); List jsonList = gson.fromJson(data,jsonCasting); model.addAttribute("quiz",jsonList); -// model.addAttribute("answers",jsonList); bufferedReader.close(); return "quiz"; } + } diff --git a/src/main/java/com/example/init/models/Quiz.java b/src/main/java/com/example/init/models/Quiz.java index bf4692f..a79f104 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -1,7 +1,10 @@ package com.example.init.models; import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; +import netscape.javascript.JSObject; import javax.persistence.*; import java.io.*; @@ -18,11 +21,30 @@ public class Quiz { private Long id; public String question; -// public String answers; + public String description; + public static class Answers { - public Quiz(String question) { + public String answer_a; + public String answer_b; + public String answer_c; + public String answer_d; + public String answer_e; + public String answer_f; + } + public Answers anserws; + + public Answers getAnserws() { + return anserws; + } + + public void setAnserws(Answers anserws) { + this.anserws = anserws; + } + + public Quiz(String question , String description ) { this.question = question; -// this.answers = answers; + this.description = description; + } public Long getId() { diff --git a/src/main/resources/templates/answers.html b/src/main/resources/templates/answers.html new file mode 100644 index 0000000..008dd3f --- /dev/null +++ b/src/main/resources/templates/answers.html @@ -0,0 +1,23 @@ + + + + + Quiz Page + + + +
    +
  • +
  • +
  • +
  • +
  • +
  • + +
+ + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/quiz.html b/src/main/resources/templates/quiz.html index 5e39f28..8a20d9a 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -13,17 +13,17 @@
- - - - - - - - + +
    +
  • +
  • +
  • +
  • +
  • +
  • - - +
+
From bbf9e6a3fc24c6e0dc89be9d7dc7dd5060ff9f06 Mon Sep 17 00:00:00 2001 From: mohnalkhateeb Date: Tue, 26 Oct 2021 09:25:20 +0300 Subject: [PATCH 2/2] comment --- .../com/example/init/InitApplication.java | 2 +- .../com/example/init/controllers/User.java | 29 +++++++-- .../java/com/example/init/models/Coders.java | 19 +++--- .../java/com/example/init/models/Comment.java | 64 +++++++++++++++++++ .../java/com/example/init/models/Post.java | 29 ++++++++- .../java/com/example/init/models/Quiz.java | 2 +- .../init/repositories/CommentRepository.java | 12 ++++ .../init/repositories/ContentRepository.java | 6 +- src/main/resources/application.properties | 6 +- src/main/resources/templates/answers.html | 23 ------- src/main/resources/templates/post.html | 38 +++++++++++ src/main/resources/templates/profile.html | 7 +- 12 files changed, 193 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/example/init/models/Comment.java create mode 100644 src/main/java/com/example/init/repositories/CommentRepository.java delete mode 100644 src/main/resources/templates/answers.html create mode 100644 src/main/resources/templates/post.html diff --git a/src/main/java/com/example/init/InitApplication.java b/src/main/java/com/example/init/InitApplication.java index 400e74e..f71a9af 100644 --- a/src/main/java/com/example/init/InitApplication.java +++ b/src/main/java/com/example/init/InitApplication.java @@ -1,5 +1,6 @@ package com.example.init; +import com.example.init.models.Comment; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -8,7 +9,6 @@ public class InitApplication { public static void main(String[] args) { SpringApplication.run(InitApplication.class, args); - } } diff --git a/src/main/java/com/example/init/controllers/User.java b/src/main/java/com/example/init/controllers/User.java index 80f4938..74c37a3 100644 --- a/src/main/java/com/example/init/controllers/User.java +++ b/src/main/java/com/example/init/controllers/User.java @@ -1,8 +1,10 @@ package com.example.init.controllers; import com.example.init.models.Coders; +import com.example.init.models.Comment; import com.example.init.models.Post; import com.example.init.repositories.CodersRepository; +import com.example.init.repositories.CommentRepository; import com.example.init.repositories.ContentRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -13,15 +15,13 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.view.RedirectView; import java.security.Principal; import java.util.ArrayList; import java.util.List; +import java.util.Set; @Controller public class User { @@ -34,6 +34,8 @@ public class User { @Autowired ContentRepository contentRepository; + @Autowired + CommentRepository commentRepository; @GetMapping("/signup") public String getSignUpPage() { @@ -108,8 +110,25 @@ public RedirectView followUser(@AuthenticationPrincipal Coders user, @RequestPar @GetMapping("/feed") public String getUsersInfo(@AuthenticationPrincipal Coders user, Model model) { Coders feed = codersRepository.findByUsername(user.getUsername()); - List following = feed.getFollowers(); + Set following = feed.getFollowers(); model.addAttribute("followers", following); return "feed"; } + + @PostMapping("/addComment") + public RedirectView addComment( Long id , String body) { + Post post = contentRepository.findById(id).get(); + System.out.println(post.getBody()); + Comment comment = new Comment(post , body); + commentRepository.save(comment); + System.out.println(comment.getBody()); + return new RedirectView("/post"); + } + @GetMapping("/post") + public String getUserProfile( Model model,@AuthenticationPrincipal Coders user) { + List posts = (List) codersRepository.findByUsername(user.getUsername()).getPosts(); + model.addAttribute("posts", posts); +// model.addAttribute("userProfile", user); + return "post"; + } } diff --git a/src/main/java/com/example/init/models/Coders.java b/src/main/java/com/example/init/models/Coders.java index 1bfd666..fa197ab 100644 --- a/src/main/java/com/example/init/models/Coders.java +++ b/src/main/java/com/example/init/models/Coders.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Set; @Entity public class Coders implements UserDetails { @@ -28,11 +29,13 @@ public class Coders implements UserDetails { @OneToMany(mappedBy = "applicationUser", fetch = FetchType.EAGER) private List posts; - @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER) + @ManyToMany @JoinTable(name = "follower_follower", joinColumns = @JoinColumn(name = "from_id"), inverseJoinColumns = @JoinColumn(name = "to_id")) - List followers = new ArrayList<>(); - @ManyToMany(mappedBy = "followers", fetch = FetchType.EAGER) - List following = new ArrayList<>(); + Set followers ; + @ManyToMany(mappedBy = "followers") + Set following ; +// @OneToMany(mappedBy = "applicationUser", fetch = FetchType.EAGER) +// private List comments; public Coders() { } @@ -147,19 +150,19 @@ public void setPosts(List posts) { this.posts = posts; } - public List getFollowers() { + public Set getFollowers() { return this.followers; } - public void setFollowers(List followers) { + public void setFollowers(Set followers) { this.followers = followers; } - public List getFollowing() { + public Set getFollowing() { return this.following; } - public void setFollowing(List following) { + public void setFollowing(Set following) { this.following = following; } diff --git a/src/main/java/com/example/init/models/Comment.java b/src/main/java/com/example/init/models/Comment.java new file mode 100644 index 0000000..f18765b --- /dev/null +++ b/src/main/java/com/example/init/models/Comment.java @@ -0,0 +1,64 @@ +package com.example.init.models; + +import javax.persistence.*; +import java.text.SimpleDateFormat; + +@Entity +public class Comment { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + private Long id; + + private String body; + + private String createdAt; + + + @ManyToOne + @JoinColumn(name = "post_id") + private Post post; + + public Post getPost() { + return post; + } + + public Comment( Post post ,String body) { + this.post = post; + this.body = body; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm"); + } + + public Comment() { + + } + + public void setPost(Post post) { + this.post = post; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public String getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + +} diff --git a/src/main/java/com/example/init/models/Post.java b/src/main/java/com/example/init/models/Post.java index f8722c7..16f998d 100644 --- a/src/main/java/com/example/init/models/Post.java +++ b/src/main/java/com/example/init/models/Post.java @@ -4,6 +4,8 @@ import java.sql.Timestamp; import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Set; @Entity public class Post { @@ -13,12 +15,26 @@ public class Post { private Long id; @ManyToOne - @JoinColumn(name = "application_user_id") + @JoinColumn(name = "coder_id") private Coders applicationUser; private String body; private String createdAt; +// @ManyToOne +// @JoinTable(name = "Comment", joinColumns = @JoinColumn(name = "post_id"), inverseJoinColumns = @JoinColumn(name = "comment_id")) +// Post post; + @OneToMany(mappedBy = "post" , fetch = FetchType.EAGER) + private List comments; + +// public Post(Post post, String body) { +// this.post = post; +// this.body = body; +// } + +// public Post getPost() { +// return post; +// } public Post() { } @@ -64,4 +80,15 @@ public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } +// public void setPost(Post post) { +// this.post = post; +// } + + public List getComments() { + return comments; + } + + public void setComments(List comments) { + this.comments = comments; + } } diff --git a/src/main/java/com/example/init/models/Quiz.java b/src/main/java/com/example/init/models/Quiz.java index c780cf8..cf4dab6 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -9,7 +9,7 @@ public class Quiz { public String question; public String description; - + public Quiz(String question , String description ) { this.question = question; diff --git a/src/main/java/com/example/init/repositories/CommentRepository.java b/src/main/java/com/example/init/repositories/CommentRepository.java new file mode 100644 index 0000000..b39e911 --- /dev/null +++ b/src/main/java/com/example/init/repositories/CommentRepository.java @@ -0,0 +1,12 @@ +package com.example.init.repositories; + +import com.example.init.models.Comment; +import com.example.init.models.Post; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import java.util.List; +@Repository +public interface CommentRepository extends JpaRepository { +// List findByPost(Post post, Sort sort); +} diff --git a/src/main/java/com/example/init/repositories/ContentRepository.java b/src/main/java/com/example/init/repositories/ContentRepository.java index e015895..a306df2 100644 --- a/src/main/java/com/example/init/repositories/ContentRepository.java +++ b/src/main/java/com/example/init/repositories/ContentRepository.java @@ -1,10 +1,14 @@ package com.example.init.repositories; +import com.example.init.models.Coders; import com.example.init.models.Post; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface ContentRepository extends JpaRepository { - public Post findPostById(Long id); + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f03a366..8b47469 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ spring.sql.init.platform=postgres spring.datasource.url=jdbc:postgresql://localhost:5432/coders -spring.datasource.username=abdelqader -spring.datasource.password=0000 +spring.datasource.username=mohammad +spring.datasource.password=1234 spring.jpa.database=postgresql spring.jpa.generate-ddl=true -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=create-drop diff --git a/src/main/resources/templates/answers.html b/src/main/resources/templates/answers.html deleted file mode 100644 index 008dd3f..0000000 --- a/src/main/resources/templates/answers.html +++ /dev/null @@ -1,23 +0,0 @@ - - - - - Quiz Page - - - -
    -
  • -
  • -
  • -
  • -
  • -
  • - -
- - - - - - \ No newline at end of file diff --git a/src/main/resources/templates/post.html b/src/main/resources/templates/post.html new file mode 100644 index 0000000..b280ab6 --- /dev/null +++ b/src/main/resources/templates/post.html @@ -0,0 +1,38 @@ + + + + + Title + + +
+
+ + + +
+
+

+

+
+
+ + +
+
+
+ + +
+
+
+

+

+
+
+
+
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 48d79a4..eb527e0 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -25,9 +25,11 @@
- + + +
@@ -41,7 +43,10 @@

+ + +