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
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";

}

}
83 changes: 82 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 @@ -93,6 +102,7 @@ public String getUsers(Model model, Principal principal) {
model.addAttribute("allusers", users);
Coders user = codersRepository.findByUsername(principal.getName());
model.addAttribute("username", user.getUsername());
model.addAttribute("userProfile", user);
return "users";
}

Expand All @@ -108,8 +118,79 @@ 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);
model.addAttribute("userProfile", feed);
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("/post");
}

@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 userProfile = codersRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("userProfile", userProfile);
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");
}

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

}
15 changes: 8 additions & 7 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(fetch = FetchType.EAGER)
@JoinTable(name = "follower_follower", joinColumns = @JoinColumn(name = "from_id"), inverseJoinColumns = @JoinColumn(name = "to_id"))
List<Coders> followers = new ArrayList<>();
Set<Coders> followers;
@ManyToMany(mappedBy = "followers", fetch = FetchType.EAGER)
List<Coders> following = new ArrayList<>();
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
63 changes: 63 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,63 @@
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;
}

}
Loading