Skip to content

Commit

Permalink
Merge pull request CSC207-UofT#45 from CSC207-UofT/tony-dashboard
Browse files Browse the repository at this point in the history
Hooking up frontend dashboard and matching screen with backend
  • Loading branch information
tonyhu-x committed Dec 5, 2021
2 parents 79a0156 + 20fabd3 commit 2ead860
Show file tree
Hide file tree
Showing 19 changed files with 1,035 additions and 94 deletions.
53 changes: 51 additions & 2 deletions app/src/main/java/com/amigo/control/DashboardController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
package com.amigo.control;

import java.util.List;

import com.amigo.match.DemoMatching;
import com.amigo.match.Match;
import com.amigo.present.UserPresenter;
import com.amigo.user.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DashboardController {

@Autowired
UserPresenter presenter;

@GetMapping("/dashboard")
public String showDashboard() {
return "dashboard";
public ModelAndView showDashboard(@ModelAttribute User user) {
if (!user.isValid() && !presenter.hasUser()) {
return new ModelAndView("error-page");
}
if (user.isValid()) {
presenter.setUser(user);
}
ModelAndView mav = new ModelAndView("dashboard");
presenter.populate(mav.getModel());
return mav;
}

@GetMapping("/matching-screen")
public ModelAndView showMatchingScreen() {
ModelAndView mav = new ModelAndView("matching-screen");
User currentUser = presenter.getUser();
var allMatches = DemoMatching.doMatching(currentUser);
List<Match> matchList = allMatches.get(currentUser.getId());
currentUser.setCurrentMatches(matchList);

int counter = 0;
for (var each : currentUser.getCurrentMatches()) {
UserPresenter p = new UserPresenter();
if (each.getUser1() == currentUser){
p.setUser(each.getUser2());
}
else {
p.setUser(each.getUser1());
}
p.setSuffix(String.valueOf(++counter));
p.populate(mav.getModel());
}
return mav;
}

@GetMapping("/*")
public String showError() {
return "error-page";
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/amigo/control/DatabaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public class DatabaseController {
return "Saved";
}

@GetMapping("/testaddcourse")
public @ResponseBody String testAddCourse() {
Course course = new Course("CSC101", "LEC0101", "TUT0101");
courseRepository.save(course);
return "Saved";
}

/**
* Returns all the courses saved in the database
*/
Expand Down
35 changes: 25 additions & 10 deletions app/src/main/java/com/amigo/control/RegistrationController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package com.amigo.control;

import javax.naming.Binding;
import javax.servlet.http.HttpServletRequest;

import com.amigo.course.Course;
import com.amigo.course.CourseRepository;
import com.amigo.form.CourseForm;
import com.amigo.form.CourseFormList;
import com.amigo.form.InterestForm;
import com.amigo.form.RegistrationForm;
import com.amigo.user.UserBuilder;
import com.amigo.validate.CourseValidator;
import com.amigo.validate.RegistrationValidator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* Controller for account creation.
* <p>
* This controller is used throughout the user registration process (currently 3
* screens). Once registration is successful, a new {@code User} object is
* created and control is passed on to {@link DashboardController}.
*/
@Controller
public class RegistrationController {
Expand All @@ -36,8 +37,14 @@ public class RegistrationController {

@Autowired
CourseValidator courseValidator;

@Autowired
UserBuilder userBuilder;

@Autowired
CourseRepository courseRepository;

@GetMapping("/")
@GetMapping({"/", "/index"})
public String showWelcomePage(Model model) {
model.addAttribute("regForm", new RegistrationForm());
return "index";
Expand All @@ -48,14 +55,15 @@ public String showWelcomePage(Model model) {
*
* @param form a Data Access Object representing the registration form
*/
@PostMapping("/")
@PostMapping({"/", "/index"})
public String validateRegistration(Model model, @ModelAttribute("regForm") RegistrationForm form, BindingResult result) {
validator.validate(form, result);

if (result.hasErrors()) {
model.addAttribute("errorMessage", result.getAllErrors().get(0).getCode());
return "index";
}
userBuilder.populate(form);
return "redirect:/register-courses";
}

Expand All @@ -76,7 +84,13 @@ public String validateCourses(Model model, @ModelAttribute CourseFormList course
model.addAttribute("courseForms", new CourseFormList());
return "register-courses";
}
else {
for (CourseForm form : courseForms.getCourseList()) {
courseRepository.save(new Course(form.getCourseCode(), form.getLectureCode(), form.getTutorialCode()));
}
}

userBuilder.populate(courseForms);
return "redirect:/register-interests";
}

Expand All @@ -87,8 +101,9 @@ public String showRegisterInterestPage(Model model) {
}

@PostMapping("/register-interests")
public String validateInterests(Model model, @ModelAttribute("interestForm") InterestForm interestForm) {
System.out.println(interestForm.getHobbies());
public String validateInterests(RedirectAttributes attributes, @ModelAttribute("interestForm") InterestForm interestForm) {
userBuilder.populate(interestForm);
attributes.addFlashAttribute("user", userBuilder.createUser());
return "redirect:/dashboard";
}
}
1 change: 0 additions & 1 deletion app/src/main/java/com/amigo/form/CourseForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
System.out.println(courseCode);
this.courseCode = courseCode;
}
public String getLectureCode() {
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/amigo/match/DemoMatching.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.amigo.match;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.amigo.course.CourseSetFactory;
import com.amigo.user.ProfileFactory;
import com.amigo.user.User;
import com.amigo.user.UserFactory;

public class DemoMatching {

public static Map<String, List<Match>> doMatching(User user) {
Matcher matcher = new Matcher();
List<User> users = createDemoUsers();
users.add(user);
return matcher.match(users);
}

private static List<User> createDemoUsers() {
ArrayList<User> users = new ArrayList<User>();
ProfileFactory profileFactory = new ProfileFactory();
CourseSetFactory courseSetFactory = new CourseSetFactory();
UserFactory userFactory = new UserFactory();
var p1 = profileFactory.createProfile("Toni", 2, "Linguistics", courseSetFactory.createCourseSet("CSC207,LIN102,LIN101"),
"", "Chess", "Ice Skating", "Taylor Swift", "Eat Manpuku");
var p2 = profileFactory.createProfile("Ikshat", 2, "Computer Science", courseSetFactory.createCourseSet("CSC207,CSC301,CSC209"),
"", "Sleep", "Rock Climbing", "Zachary Knowles", "Play Video Games");
var p3 = profileFactory.createProfile("Rue", 2, "Computer Science", courseSetFactory.createCourseSet("CSC207,CSC236,CSC209"),
"", "Poetry", "Ew Sports", "BTS", "Read");
var p4 = profileFactory.createProfile("Adrian", 2, "Gender Studies", courseSetFactory.createCourseSet("ECO101,CSC207,MAT237"),
"", "Economics", "Ski", "Illenium", "Breathe");
var p5 = profileFactory.createProfile("Dien", 2, "Computational Biology and Bioinformatics", courseSetFactory.createCourseSet("CSC207,CSC236,BIO101"),
"", "Makeup", "Dance", "Blackpink", "Makeup Even More");
var p6 = profileFactory.createProfile("Lawrence", 2, "Chemistry", courseSetFactory.createCourseSet("CHM101,CHM102,CSC207,MAT237"),
"", "Crypto", "Run", "Pop", "Lift Weights");

users.add(userFactory.createUser(p1));
users.add(userFactory.createUser(p2));
users.add(userFactory.createUser(p3));
users.add(userFactory.createUser(p4));
users.add(userFactory.createUser(p5));
users.add(userFactory.createUser(p6));

return users;
}
}
57 changes: 57 additions & 0 deletions app/src/main/java/com/amigo/present/UserPresenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.amigo.present;

import java.util.Map;

import com.amigo.user.User;

import org.springframework.stereotype.Controller;

/**
* A presenter that populates the model for dashboard page given a specific
* user.
*/
@Controller
public class UserPresenter {

private User user;

/**
* The suffix used to distinguish each attribute set.
*/
private String suffix = "";

/**
* Returns whether there is currently a {@code User} object associated with this
* presenter.
*/
public boolean hasUser() {
return user != null;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}

/**
* Populates the ModelMap with attributes taken from the user.
*/
public void populate(Map<String, Object> map) {
if (user == null) {
throw new IllegalStateException("User not set with setUser()");
}
map.put("name" + (suffix.equals("") ? "" : "_" + suffix), user.getProfile().getName());
map.put("hobbies" + (suffix.equals("") ? "" : "_" + suffix), user.getProfile().getHobbies());
map.put("sports" + (suffix.equals("") ? "" : "_" + suffix), user.getProfile().getSportInterest());
map.put("music" + (suffix.equals("") ? "" : "_" + suffix), user.getProfile().getMusInterest());
map.put("recreational" + (suffix.equals("") ? "" : "_" + suffix), user.getProfile().getRecInterest());
map.put("courses" + (suffix.equals("") ? "" : "_" + suffix), user.getProfile().getCoursesAsString());
}
}
11 changes: 9 additions & 2 deletions app/src/main/java/com/amigo/user/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.Collectors;

import javax.persistence.Entity;
import com.amigo.course.Course;
Expand All @@ -24,7 +25,9 @@ public class Profile {
/**
* Creates a profile using the default constructor
*/
public Profile() {}
public Profile() {
this.courses = new HashSet<>();
}

/**
* Creates a profile containing a name, year of study, program of study, courses, contact info and interests.
Expand Down Expand Up @@ -97,11 +100,15 @@ public HashSet<Course> getCourses() {
public ArrayList<String> getCoursesList() {
ArrayList<String> coursesStringList = new ArrayList<>();
for (Course course : courses) {
coursesStringList.add(course.toString());
coursesStringList.add(course.getCourseCode());
}
return coursesStringList;
}

public String getCoursesAsString() {
return getCoursesList().stream().collect(Collectors.joining(" "));
}

/**
* Returns the profile's contact info
*/
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/amigo/user/ProfileFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.HashSet;

public class ProfileFactory{
public class ProfileFactory {

/**
* Constructor for ProfileFactory
Expand Down
27 changes: 26 additions & 1 deletion app/src/main/java/com/amigo/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class User {
/**
* Creates a user using the default constructor
*/
public User() {}
public User() {
this.currentMatches = new ArrayList<>();
}

/**
* Creates a user containing an id, an email, a password, a profile and an empty list of current matches.
Expand All @@ -48,13 +50,20 @@ public Profile getProfile() {
return profile;
}

public void setProfile(Profile profile) {
this.profile = profile;
}

/**
* Returns a user's id
*/
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

/**
* Returns a user's email
Expand All @@ -63,6 +72,10 @@ public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

/**
* Returns a user's password
*/
Expand Down Expand Up @@ -106,4 +119,16 @@ public String toStringCurrentMatches() {
}
return namesCurrentMatches.toString();
}

public void setPassword(String password) {
this.password = password;
}

/**
* Returns whether the user object has been properly instantiated and has its
* attributes set.
*/
public boolean isValid() {
return profile != null;
}
}
Loading

0 comments on commit 2ead860

Please sign in to comment.