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
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ dependencies {
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'
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'


}

test {
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/com/example/init/controllers/AdminController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.example.init.controllers;

import java.security.Principal;
import java.util.List;

import javax.transaction.Transactional;

import com.example.init.models.Coders;
import com.example.init.repositories.CodersRepository;

import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class AdminController {
@Autowired
CodersRepository codersRepository;

@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;

@GetMapping("/admin")

String admin(Model model) {
List<Coders> coders = codersRepository.findAll();
model.addAttribute("coders", coders);
return "admin";
}

@PostMapping("/admin")
RedirectView addCoder(String email, String username, String password, String firstName, String lastName,
String dateOfBirth, String bio) {

Coders newCoder = new Coders(email, username, bCryptPasswordEncoder.encode(password), firstName, lastName,
dateOfBirth, bio);
codersRepository.save(newCoder);
return new RedirectView("/admin");
}

@GetMapping("/coder")
public String getCoder(@RequestParam long id, Model model, Principal principal) {
Coders coder = codersRepository.findById(id).get();
model.addAttribute("coder", coder);
return "coder";
}

@Transactional
@GetMapping("/del/{id}")
public RedirectView delete(@PathVariable String id, Principal principal, Model model) {
Coders coder = codersRepository.findById(Long.parseLong(id)).orElseThrow();
model.addAttribute("coder", principal.getName());
codersRepository.delete(coder);
return new RedirectView("/admin");
}

@GetMapping("/form/{id}")
public String showUpdateForm(@PathVariable("id") long id, Model model) {
Coders coder = codersRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("coder", coder);
return "putForm";
}

@Transactional
@PostMapping("/upd/{id}")
public RedirectView updateUser(@PathVariable("id") long id, Coders coder) {
Coders updatedCoder = codersRepository.findById(id).orElseThrow();
updatedCoder.setEmail(coder.getEmail());
updatedCoder.setUsername(coder.getUsername());
updatedCoder.setPassword(coder.getPassword());
updatedCoder.setFirstName(coder.getFirstName());
updatedCoder.setLastName(coder.getLastName());
updatedCoder.setDateOfBirth(coder.getDateOfBirth());
updatedCoder.setBio(coder.getBio());
codersRepository.save(updatedCoder);
return new RedirectView("/admin");
}

}
26 changes: 26 additions & 0 deletions src/main/java/com/example/init/controllers/EditorController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.init.controllers;

import com.example.init.models.Editor;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class EditorController {

@GetMapping("/editor")
public String editor(Model model) {

model.addAttribute("editor", new Editor());
return "editor";
}

@PostMapping("/editor")
public String save(Editor editor, Model model) {

model.addAttribute("editor", editor);
return "saved";
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/example/init/controllers/Modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Modal</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous"
/>
</head>
<body>
<div class="card" style="width: 18rem">
<img src="..." class="card-img-top" alt="..." />
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
Some quick example text to build on the card title and make up the
bulk of the card's content.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"
></script>
</body>
</html>
28 changes: 27 additions & 1 deletion src/main/java/com/example/init/controllers/QuizController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package com.example.init.controllers;

import com.example.init.models.Quiz;
import com.example.init.models.ResultsQuiz;
import com.example.init.repositories.CodersRepository;
import com.example.init.repositories.ResultsRepo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.Principal;
import java.util.List;

@Controller
public class QuizController {

@Autowired
ResultsRepo resultsRepo;
@Autowired
CodersRepository codersRepository;

@GetMapping("/quiz")
public String getQuiz(Model model) throws IOException {
Gson gson = new Gson();
Expand All @@ -33,4 +45,18 @@ public String getQuiz(Model model) throws IOException {
bufferedReader.close();
return "quiz";
}
}

@PostMapping("/submit")
public String submit(Model model, Principal principal) {
ResultsQuiz resultsQuiz = new ResultsQuiz();
// ApplicationUser applicationUser = new ApplicationUser();
Quiz quiz = new Quiz();
// resultsQuiz.setTotalCorrect(quiz.getAnswers());
resultsQuiz.setUsername(codersRepository.findByUsername(principal.getName()));
resultsQuiz.setTotalCorrect(resultsQuiz.getTotalCorrect());
model.addAttribute("quiz", resultsRepo.save(resultsQuiz));
return "result";

}

}
89 changes: 88 additions & 1 deletion 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 @@ -15,13 +17,17 @@
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

@Controller
public class User {
Expand All @@ -35,6 +41,9 @@ public class User {
@Autowired
ContentRepository contentRepository;

@Autowired
CommentRepository commentRepository;

@GetMapping("/signup")
public String getSignUpPage() {
return "signup";
Expand Down Expand Up @@ -108,8 +117,86 @@ 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";
}

@Transactional
@GetMapping("/delete/{id}")
public RedirectView deleteUserPost(@PathVariable String id, Principal principal, Model model) {
Post post = contentRepository.findById(Long.parseLong(id)).orElseThrow();
Coders user = codersRepository.findByUsername(principal.getName());
model.addAttribute("username", principal.getName());
model.addAttribute("userProfile", user);
contentRepository.delete(post);
return new RedirectView("/profile");
}

@GetMapping("/edit/{id}")
public String showUpdateForm(@PathVariable("id") long id, Model model) {
Post post = contentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("post", post);
return "updateForm";
}

@PostMapping("/update/{id}")
public RedirectView updateUser(@PathVariable("id") long id, Post post) {
Post updatedPost = contentRepository.findById(id).orElseThrow();
updatedPost.setBody(post.getBody());
contentRepository.save(updatedPost);
return new RedirectView("/profile");
}

@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("profile");
}

@GetMapping("/post")
public String getPost(Model model, @AuthenticationPrincipal Coders user) {
List<Post> posts = (List<Post>) codersRepository.findByUsername(user.getUsername()).getPosts();
model.addAttribute("posts", posts);
return "post";
}

@GetMapping("/UserForm/{id}")
public String updateInfo(@PathVariable("id") long id, Model model) {
Coders coder = codersRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("coder", coder);
return "userInfoForm";
}

@Transactional
@PostMapping("/updateInfo/{id}")
public RedirectView updateInfo(@PathVariable("id") long id, Coders coder) {
Coders updatedCoder = codersRepository.findById(id).orElseThrow();
updatedCoder.setEmail(coder.getEmail());
updatedCoder.setUsername(coder.getUsername());
updatedCoder.setPassword(coder.getPassword());
updatedCoder.setFirstName(coder.getFirstName());
updatedCoder.setLastName(coder.getLastName());
updatedCoder.setDateOfBirth(coder.getDateOfBirth());
updatedCoder.setBio(coder.getBio());
codersRepository.save(updatedCoder);
return new RedirectView("/profile");
}

@Transactional
@PostMapping("/unfollow")
public RedirectView unfollow(@AuthenticationPrincipal Coders user, @RequestParam Long id) {
Coders feed = codersRepository.findByUsername(user.getUsername());
Coders follow = codersRepository.findById(id).orElseThrow();
feed.getFollowers().remove(follow);
return new RedirectView("/feed");
}

}
17 changes: 9 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,11 @@ 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;

public Coders() {
}
Expand Down Expand Up @@ -147,19 +148,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
Loading