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

Feature/quiz functionality #14

Merged
merged 6 commits into from
Mar 28, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public class QuizDTO {
private String description;
private List<QuizQuestionDTO> questions;
private PublicUserInformationDTO author;
private boolean open;
private boolean isOpen;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AnswerMapper {
public AnswerDTO mapToAnswerDTO(Answer answer) {
return AnswerDTO.builder()
.answerText(answer.getAnswerText())
.isCorrect(answer.isCorrect())
.isCorrect(answer.getIsCorrect())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public QuizDTO mapToQuizDTO(Quiz quiz) {
.questions(quiz.getQuestions().stream()
.map(questionMapper::mapToQuizQuestionDTO)
.toList())
.isOpen(quiz.getIsOpen())
.build();
}

Expand All @@ -52,7 +53,7 @@ public QuizPreviewDTO mapToQuizPreviewDTO(Quiz quiz) {
.id(quiz.getId())
.title(quiz.getName())
.description(quiz.getDescription())
.open(quiz.isOpen())
.open(quiz.getIsOpen())
.build();
}

Expand All @@ -71,6 +72,7 @@ public Quiz mapToQuiz(QuizCreationRequestDTO quizCreationRequestDTO, User user)
.map(questionMapper::mapToQuizQuestion)
.toList())
.author(user)
.isOpen(quizCreationRequestDTO.isOpen())
.build();

createdQuiz.getQuestions().forEach(question -> {
Expand Down
54 changes: 48 additions & 6 deletions src/main/java/edu/ntnu/idatt2105/quizapp/model/User.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package edu.ntnu.idatt2105.quizapp.model;

import jakarta.persistence.*;

import edu.ntnu.idatt2105.quizapp.model.quiz.Quiz;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.Collection;
import java.util.List;

import lombok.*;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -17,15 +33,16 @@
* @author Jeffrey Tabiri
* @author Ramtin Samavat
* @version 1.0
* @since 2024-03-25
* @see UserDetails
* @since 2024-03-25
*/
@Entity
@Table(name = "users")
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class User implements UserDetails {

@Id
Expand Down Expand Up @@ -55,6 +72,9 @@ public class User implements UserDetails {
@Column(name = "role")
private Role role;

@OneToMany(mappedBy = "author", cascade = CascadeType.PERSIST)
private List<Quiz> quizzes;

/**
* Retrieves the roles/authorities associated with this user.
*
Expand Down Expand Up @@ -94,4 +114,26 @@ public boolean isCredentialsNonExpired() {
public boolean isEnabled() {
return true;
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
User user = (User) object;
return Objects.equals(userId, user.userId) &&
Objects.equals(username, user.username) &&
Objects.equals(password, user.password) &&
Objects.equals(email, user.email) && Objects.equals(name, user.name) &&
Objects.equals(surName, user.surName) && role == user.role &&
Objects.equals(quizzes, user.quizzes);
}

@Override
public int hashCode() {
return Objects.hash(userId, username, password, email, name, surName, role);
}
}
26 changes: 22 additions & 4 deletions src/main/java/edu/ntnu/idatt2105/quizapp/model/quiz/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.proxy.HibernateProxy;

/**
* Model class for Answer.
Expand All @@ -29,18 +30,35 @@
@Getter
@Setter
@Builder
@EqualsAndHashCode
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Long id;
private String answerText;
private boolean isCorrect;
private Boolean isCorrect;

@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private QuizQuestion quizQuestion;

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
Answer answer = (Answer) object;
return Objects.equals(id, answer.id) &&
Objects.equals(answerText, answer.answerText) &&
Objects.equals(isCorrect, answer.isCorrect) &&
Objects.equals(quizQuestion, answer.quizQuestion);
}

@Override
public int hashCode() {
return Objects.hash(id, answerText, isCorrect);
}
}


42 changes: 35 additions & 7 deletions src/main/java/edu/ntnu/idatt2105/quizapp/model/quiz/Category.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package edu.ntnu.idatt2105.quizapp.model.quiz;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

/**
Expand All @@ -21,21 +28,42 @@
* @since 2024-03-27
*/
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "categories")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@EqualsAndHashCode
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "id", nullable = false, unique = true)
@NonNull
private Long id;

@Column(name = "description")
@Column(name = "description", nullable = false)
private String description;

@OneToMany(mappedBy = "category", cascade = CascadeType.PERSIST)
private List<Quiz> quizzes;


@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
Category category = (Category) object;
return Objects.equals(id, category.id) &&
Objects.equals(description, category.description) &&
Objects.equals(quizzes, category.quizzes);
}

@Override
public int hashCode() {
return Objects.hash(id, description);
}
}
34 changes: 16 additions & 18 deletions src/main/java/edu/ntnu/idatt2105/quizapp/model/quiz/Quiz.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
@Getter
@Setter
@Builder

public class Quiz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "quiz_id")
private long id;
private Long id;

@Column(name = "name")
private String name;
Expand All @@ -52,34 +53,31 @@ public class Quiz {
@ManyToOne(fetch = FetchType.LAZY)
private User author;

@ManyToOne(fetch = FetchType.LAZY)
private Category category;

@Column(name = "open")
private boolean open;
private Boolean isOpen;

@Override
public final boolean equals(Object object) {
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
Class<?> oEffectiveClass = object instanceof HibernateProxy ?
((HibernateProxy) object).getHibernateLazyInitializer()
.getPersistentClass() : object.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ?
((HibernateProxy) this).getHibernateLazyInitializer()
.getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) {
if (object == null || getClass() != object.getClass()) {
return false;
}
Quiz quiz = (Quiz) object;
return Objects.equals(getId(), quiz.getId());
return Objects.equals(id, quiz.id) && Objects.equals(name, quiz.name) &&
Objects.equals(description, quiz.description) &&
Objects.equals(questions, quiz.questions) &&
Objects.equals(author, quiz.author) &&
Objects.equals(category, quiz.category) &&
Objects.equals(isOpen, quiz.isOpen);
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ?
((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() :
getClass().hashCode();
public int hashCode() {
return Objects.hash(id, name, description, category, isOpen);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.proxy.HibernateProxy;

/**
* Model class for QuizQuestion.
Expand All @@ -38,42 +37,33 @@ public class QuizQuestion {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
private Long id;

@Column(name = "question_text")
private String questionText;

@OneToMany(mappedBy = "quizQuestion", cascade = CascadeType.PERSIST)
@OneToMany(mappedBy = "quizQuestion", cascade = CascadeType.ALL)
private List<Answer> answers;

@ManyToOne(fetch = FetchType.LAZY)
private Quiz quiz;

@Override
public final boolean equals(Object object) {
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
Class<?> oEffectiveClass = object instanceof HibernateProxy ?
((HibernateProxy) object).getHibernateLazyInitializer()
.getPersistentClass() : object.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ?
((HibernateProxy) this).getHibernateLazyInitializer()
.getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) {
if (object == null || getClass() != object.getClass()) {
return false;
}
QuizQuestion that = (QuizQuestion) object;
return Objects.equals(getId(), that.getId());
return Objects.equals(id, that.id) &&
Objects.equals(questionText, that.questionText) &&
Objects.equals(answers, that.answers) && Objects.equals(quiz, that.quiz);
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ?
((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() :
getClass().hashCode();
public int hashCode() {
return Objects.hash(id, questionText, quiz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
public interface QuizRepository extends JpaRepository<Quiz, Long> {
List<Quiz> findAllByAuthorUsername(String username, Pageable pageable);

List<Quiz> findAllByAuthorUsernameAndOpen(String username, Pageable pageable, boolean open);
List<Quiz> findAllByAuthorUsernameAndIsOpen(String username, Pageable pageable, boolean open);

List<Quiz> findAllByOpen(boolean open, Pageable pageable);
List<Quiz> findAllByIsOpen(boolean open, Pageable pageable);

Optional<Quiz> findQuizById(Long id);

Expand Down
Loading
Loading