Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/init/InitApplication.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,7 +9,6 @@ public class InitApplication {

public static void main(String[] args) {
SpringApplication.run(InitApplication.class, args);

}

}
39 changes: 23 additions & 16 deletions src/main/java/com/example/init/controllers/QuizController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<List<Quiz>>() {
}.getType();
List<Quiz> jsonList = gson.fromJson(data, jsonCasting);
model.addAttribute("quiz", jsonList);
bufferedReader.close();
return "quiz";
}
}


@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<List<Quiz>>(){}.getType();
List<Quiz> jsonList = gson.fromJson(data,jsonCasting);
model.addAttribute("quiz",jsonList);
bufferedReader.close();
return "quiz";
}

}

29 changes: 24 additions & 5 deletions src/main/java/com/example/init/controllers/User.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -34,6 +34,8 @@ public class User {

@Autowired
ContentRepository contentRepository;
@Autowired
CommentRepository commentRepository;

@GetMapping("/signup")
public String getSignUpPage() {
Expand Down Expand Up @@ -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<Coders> following = feed.getFollowers();
Set<Coders> 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<Post> posts = (List<Post>) codersRepository.findByUsername(user.getUsername()).getPosts();
model.addAttribute("posts", posts);
// model.addAttribute("userProfile", user);
return "post";
}
}
19 changes: 11 additions & 8 deletions src/main/java/com/example/init/models/Coders.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,11 +29,13 @@ public class Coders implements UserDetails {
@OneToMany(mappedBy = "applicationUser", fetch = FetchType.EAGER)
private List<Post> posts;

@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@ManyToMany
@JoinTable(name = "follower_follower", joinColumns = @JoinColumn(name = "from_id"), inverseJoinColumns = @JoinColumn(name = "to_id"))
List<Coders> followers = new ArrayList<>();
@ManyToMany(mappedBy = "followers", fetch = FetchType.EAGER)
List<Coders> following = new ArrayList<>();
Set<Coders> followers ;
@ManyToMany(mappedBy = "followers")
Set<Coders> following ;
// @OneToMany(mappedBy = "applicationUser", fetch = FetchType.EAGER)
// private List<Comment> comments;

public Coders() {
}
Expand Down Expand Up @@ -147,19 +150,19 @@ public void setPosts(List<Post> posts) {
this.posts = posts;
}

public List<Coders> getFollowers() {
public Set<Coders> getFollowers() {
return this.followers;
}

public void setFollowers(List<Coders> followers) {
public void setFollowers(Set<Coders> followers) {
this.followers = followers;
}

public List<Coders> getFollowing() {
public Set<Coders> getFollowing() {
return this.following;
}

public void setFollowing(List<Coders> following) {
public void setFollowing(Set<Coders> following) {
this.following = following;
}

Expand Down
64 changes: 64 additions & 0 deletions src/main/java/com/example/init/models/Comment.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
29 changes: 28 additions & 1 deletion src/main/java/com/example/init/models/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Set;

@Entity
public class Post {
Expand All @@ -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<Comment> comments;

// public Post(Post post, String body) {
// this.post = post;
// this.body = body;
// }

// public Post getPost() {
// return post;
// }

public Post() {
}
Expand Down Expand Up @@ -64,4 +80,15 @@ public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

// public void setPost(Post post) {
// this.post = post;
// }

public List<Comment> getComments() {
return comments;
}

public void setComments(List<Comment> comments) {
this.comments = comments;
}
}
32 changes: 8 additions & 24 deletions src/main/java/com/example/init/models/Quiz.java
Original file line number Diff line number Diff line change
@@ -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<Tag> 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<Tag> 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 {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/example/init/repositories/CommentRepository.java
Original file line number Diff line number Diff line change
@@ -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<Comment , Long> {
// List<Post> findByPost(Post post, Sort sort);
}
Original file line number Diff line number Diff line change
@@ -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<Post, Long> {
public Post findPostById(Long id);

}
Loading