diff --git a/build.gradle b/build.gradle index 0e73eff..07aaf1e 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,10 @@ 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' - implementation 'com.googlecode.json-simple:json-simple:1.1.1' + + + + } test { 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/QuizController.java b/src/main/java/com/example/init/controllers/QuizController.java index f8fc358..94d5375 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -2,7 +2,10 @@ 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; @@ -14,23 +17,27 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.List; +import java.util.Map; @Controller public class QuizController { - @GetMapping("/quiz") - public String getQuiz(Model model) throws IOException { - Gson gson = new Gson(); - String urlApi = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=10&css"; - HttpURLConnection connection = (HttpURLConnection) new URL(urlApi).openConnection(); - InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); - String data = bufferedReader.readLine(); - Type jsonCasting = new TypeToken>() { - }.getType(); - List jsonList = gson.fromJson(data, jsonCasting); - model.addAttribute("quiz", jsonList); - bufferedReader.close(); - return "quiz"; - } -} \ No newline at end of file + + + @GetMapping("/quiz") + public String getQuiz(Model model) throws IOException { + Gson gson = new Gson(); + String urlApi = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=10&css"; + HttpURLConnection connection = (HttpURLConnection) new URL(urlApi).openConnection(); + InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String data = bufferedReader.readLine(); + Type jsonCasting = new TypeToken>(){}.getType(); + List jsonList = gson.fromJson(data,jsonCasting); + model.addAttribute("quiz",jsonList); + bufferedReader.close(); + return "quiz"; + } + +} + 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 602e8ab..cf4dab6 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -1,37 +1,21 @@ package com.example.init.models; + import java.util.List; public class Quiz { public int id; public String question; - public Object description; - public Answers answers; - public String multiple_correct_answers; - public CorrectAnswers correct_answers; - public String correct_answer; - public Object explanation; - public Object tip; - public List tags; - public String category; - public String difficulty; - - public Quiz(int id, String question, Object description, Answers answers, String multiple_correct_answers, - CorrectAnswers correct_answers, String correct_answer, Object explanation, Object tip, List tags, - String category, String difficulty) { - this.id = id; + + public String description; + + + public Quiz(String question , String description ) { this.question = question; this.description = description; - this.answers = answers; - this.multiple_correct_answers = multiple_correct_answers; - this.correct_answers = correct_answers; - this.correct_answer = correct_answer; - this.explanation = explanation; - this.tip = tip; - this.tags = tags; - this.category = category; - this.difficulty = difficulty; + + } private static class Answers { 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/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 @@

+ + + diff --git a/src/main/resources/templates/quiz.html b/src/main/resources/templates/quiz.html index b3041e1..a9e2368 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -15,23 +15,25 @@
-

-

-

-

-

-

-

-

-

-

-

-

- < - - + + +
+ +
    +
  • +
  • +
  • +
  • +
  • +
  • + +
+ +
+ + + + +