Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Issue #2 Add User and Surveyor #9

Merged
merged 6 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
76 changes: 76 additions & 0 deletions src/main/java/com/opinionowl/opinionowl/models/AppUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.opinionowl.opinionowl.models;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

/**
* Class User for defining a User entity that uses the Survey website.
*/
@Entity
@Getter
@Setter
@NoArgsConstructor
public class AppUser {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
// Create an auto generated unique Id
private Long id;

// Define a username for the user
public String username;

// Define a password for the associated username
public String password;

// Define a list of surveys that a User has created
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
public List<Survey> listSurveys;

/**
* Default constructor for User.
* @param username A string username.
* @param password A string password.
*/
public AppUser(String username, String password) {
this.username = username;
this.password = password;
this.listSurveys = new ArrayList<>();
}

/**
* Void method for adding a survey to the list of surveys.
* @param survey A Survey survey.
*/
public void addSurvey (Survey survey){
this.listSurveys.add(survey);

}

/**
* Boolean method for removing a survey from the list of surveys.
* @param surveyId A Long survey ID.
* @return True if the survey was removed, false otherwise.
*/
public boolean removeSurvey (Long surveyId) {
return this.listSurveys.removeIf(s -> s.getId().equals(surveyId));
}

/**
* @return A user in String format.
*/
@Override
public String toString(){
String user = "User ID: " + id + "Username: " + username + "Password: " + password;
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
user += "\n List of Surveys: ";
for (Survey s: listSurveys) {
user += "\n" + s.toString();
}
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class LongAnswerQuestion extends Question{
* The default constructor for the class.
*/
public LongAnswerQuestion(){
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
this("", 0);
this(null, "", 0);
}

/**
* The constructor for the class.
* @param prompt the prompt for the question
* @param charLimit the character limit for the response.
*/
public LongAnswerQuestion(String prompt, int charLimit){
super(prompt, QuestionType.LONG_ANSWER);
public LongAnswerQuestion(Survey survey, String prompt, int charLimit){
super(survey, prompt, QuestionType.LONG_ANSWER);
this.charLimit = charLimit;
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/opinionowl/opinionowl/models/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ public class Question {
// The type of the question.
private QuestionType type;

@ManyToOne
private Survey survey;

/**
* The constructor for the question class.
* @param prompt the prompt for the question.
* @param type the type of the question
*/
public Question(String prompt, QuestionType type){
public Question(Survey survey, String prompt, QuestionType type){
this.survey = survey;
this.prompt = prompt;
this.type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import lombok.Getter;
import lombok.Setter;

import java.sql.Array;
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
import java.util.ArrayList;

/**
* The class used to describe the multiple choice questions.
*/
Expand All @@ -17,17 +20,17 @@ public class RadioChoiceQuestion extends Question {
/**
* The default constructor for the class.
*/
public RadioChoiceQuestion() {
this("", new String[0]);
public RadioChoiceQuestion(){
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
this(null, "", new String[0]);
}

/**
* The constructor for the class.
* @param prompt the prompt for the question.
* @param choices the choices for the question.
*/
public RadioChoiceQuestion(String prompt, String[] choices){
super(prompt, QuestionType.RADIO_CHOICE);
public RadioChoiceQuestion(Survey survey, String prompt, String[] choices){
super(survey, prompt, QuestionType.RADIO_CHOICE);
this.choices = choices;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public RangeQuestion(){
* @param upper the upper value for the range.
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
*/
public RangeQuestion(String prompt, int lower, int upper){
this(prompt, lower, upper, 1);
this(null, prompt, lower, upper, 1);
}

/**
Expand All @@ -44,8 +44,8 @@ public RangeQuestion(String prompt, int lower, int upper){
* @param upper the upper value for the range.
* @param increment the increment value for the range.
*/
public RangeQuestion(String prompt, int lower, int upper, int increment){
super(prompt, QuestionType.RANGE);
public RangeQuestion(Survey survey, String prompt, int lower, int upper, int increment){
super(survey, prompt, QuestionType.RANGE);
this.lower = lower;
this.upper = upper;
this.increment = increment;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/opinionowl/opinionowl/models/Survey.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Survey {
private Long id;

// Keeps track of the questions of the survey.
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OneToMany(mappedBy = "survey", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Question> questions;

// Keeps track of the survey responses.
Expand All @@ -35,10 +35,15 @@ public class Survey {
// The title for the survey.
private String title;

// The user for the survey.
@ManyToOne
private AppUser user;

/**
* The default constructor for the survey.
*/
public Survey(String title){
public Survey(AppUser user, String title){
this.user = user;
this.title = title;
this.questions = new ArrayList<>();
this.responses = new ArrayList<>();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/opinionowl/opinionowl/repos/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.opinionowl.opinionowl.repos;

import com.opinionowl.opinionowl.models.AppUser;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
* The repository in charge of managing the CRUD operations for the User Entity.
*/
public interface UserRepository extends CrudRepository<AppUser, Long>{
List<AppUser> findAll();
}
37 changes: 22 additions & 15 deletions src/test/java/com/opinionowl/opinionowl/SurveyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.opinionowl.opinionowl.models.*;
import com.opinionowl.opinionowl.repos.SurveyRepository;
import com.opinionowl.opinionowl.repos.UserRepository;
import org.h2.engine.User;
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -13,12 +15,22 @@ public class SurveyTest {
@Autowired
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
private SurveyRepository surveyRepository;

@Autowired
private UserRepository userRepository;

/**
* Test method for the Survey class.
*/
@Test
public void testPersist() {
Anthony-Massaad marked this conversation as resolved.
Show resolved Hide resolved
Survey survey = new Survey("TEST_SURVEY");
LongAnswerQuestion q1 = new LongAnswerQuestion("test1", 2);
RadioChoiceQuestion q2 = new RadioChoiceQuestion("test2", new String[]{"a", "c", "d"});
RangeQuestion q3 = new RangeQuestion("test3", 1, 10);
AppUser u1 = new AppUser("Test", "123");
Survey survey = new Survey(u1, "TEST_SURVEY");
u1.addSurvey(survey);
userRepository.save(u1);

LongAnswerQuestion q1 = new LongAnswerQuestion(survey, "test1", 2);
RadioChoiceQuestion q2 = new RadioChoiceQuestion(survey, "test2", new String[]{"a", "c", "d"});
RangeQuestion q3 = new RangeQuestion(survey, "test3", 1, 10, 1);

survey.addQuestion(q1);
survey.addQuestion(q2);
Expand All @@ -42,18 +54,13 @@ public void testPersist() {
r2.addAnswer(q1.getId(), "yo");
r2.addAnswer(q2.getId(), "a");

surveyRepository.save(survey);
survey.addResponse(r2);

List<Survey> results = surveyRepository.findAll();
for (Survey r : results){
System.out.println(r.toString());
surveyRepository.save(survey);

for (Question q : r.getQuestions()){
if (q.getType() == QuestionType.LONG_ANSWER){
LongAnswerQuestion laq = (LongAnswerQuestion) q;
System.out.println(laq.getCharLimit());
}
}
List<AppUser> results = userRepository.findAll();
for (AppUser u : results){
System.out.println(u.toString());
}
}
}
}