Added quizz, answers and security
This commit is contained in:
parent
4f3814bd7e
commit
001ff8b5cb
@ -1,17 +1,26 @@
|
||||
package com.bernard.misael;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
import com.bernard.misael.model.User;
|
||||
import com.bernard.misael.service.UserService;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SpringSecurity {
|
||||
|
||||
37
src/main/java/com/bernard/misael/model/Answer.java
Normal file
37
src/main/java/com/bernard/misael/model/Answer.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.bernard.misael.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.Lob;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name="answers")
|
||||
public class Answer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@Column(name="id")
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "question",nullable = false)
|
||||
private Question question;
|
||||
|
||||
@Lob
|
||||
private String value;
|
||||
|
||||
}
|
||||
32
src/main/java/com/bernard/misael/model/Question.java
Normal file
32
src/main/java/com/bernard/misael/model/Question.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.bernard.misael.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name="questions")
|
||||
public class Question {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@Column(name="id")
|
||||
private long id;
|
||||
|
||||
@Column(nullable=false)
|
||||
private int index;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "quizz",nullable = false)
|
||||
private Quizz quizz;
|
||||
|
||||
@Lob
|
||||
private String value;
|
||||
|
||||
}
|
||||
42
src/main/java/com/bernard/misael/model/Quizz.java
Normal file
42
src/main/java/com/bernard/misael/model/Quizz.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.bernard.misael.model;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name="quizz")
|
||||
public class Quizz {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable=false, unique=true)
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="owner")
|
||||
private User owner;
|
||||
|
||||
@Column(nullable=false)
|
||||
@ColumnDefault("false")
|
||||
private boolean isPublic;
|
||||
|
||||
@Column(nullable=false)
|
||||
@ColumnDefault("0")
|
||||
private int questionCount;
|
||||
|
||||
@OneToMany(mappedBy="quizz")
|
||||
private Set<Question> questions;
|
||||
}
|
||||
40
src/main/java/com/bernard/misael/model/QuizzForm.java
Normal file
40
src/main/java/com/bernard/misael/model/QuizzForm.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.bernard.misael.model;
|
||||
|
||||
import org.hibernate.annotations.GeneratedColumn;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name="quizzf")
|
||||
public class QuizzForm {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "answerer",nullable = false)
|
||||
private User user;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "quizz",nullable=false)
|
||||
private Quizz quizz;
|
||||
|
||||
private boolean done;
|
||||
private int currentQuestion;
|
||||
private int answerStep;
|
||||
}
|
||||
@ -2,17 +2,22 @@ package com.bernard.misael.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.cache.spi.support.AbstractReadWriteAccess.Item;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
@Entity
|
||||
@Table(name="users")
|
||||
public class User
|
||||
@ -34,4 +39,7 @@ public class User
|
||||
inverseJoinColumns={@JoinColumn(name="ROLE_ID", referencedColumnName="ID")})
|
||||
private List<Role> roles = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy="owner")
|
||||
private Set<Quizz> myQuizzs;
|
||||
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package com.bernard.misael.web;
|
||||
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
@ -1,8 +1,16 @@
|
||||
package com.bernard.misael.web;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.bernard.misael.model.User;
|
||||
import com.bernard.misael.repository.UserRepository;
|
||||
import com.bernard.misael.service.UserService;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
|
||||
@ -10,11 +18,22 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
@RequestMapping("/questions")
|
||||
public class QuestionsController {
|
||||
|
||||
@Autowired
|
||||
UserRepository ur;
|
||||
|
||||
@GetMapping("/quizz")
|
||||
/*
|
||||
* List all quizz
|
||||
*/
|
||||
public String getQuizz() {
|
||||
public String getQuizz(Model model, Principal p) {
|
||||
User u = null;
|
||||
if (p!=null)
|
||||
u = ur.findByName(p.getName());
|
||||
|
||||
System.out.println("Loaded as user "+u);
|
||||
if(u!=null) {
|
||||
model.addAttribute("quizz",u.getMyQuizzs());
|
||||
}
|
||||
return "quizz.html";
|
||||
}
|
||||
|
||||
@ -24,7 +43,6 @@ public class QuestionsController {
|
||||
*/
|
||||
public String getForms() {
|
||||
return "forms.html";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -11,17 +11,17 @@ spring:
|
||||
validate-on-migrate: true
|
||||
default-schema: misael
|
||||
|
||||
# jpa:
|
||||
# properties:
|
||||
# javax:
|
||||
# persistence:
|
||||
# schema-generation:
|
||||
# create-source: metadata
|
||||
# scripts:
|
||||
# action: update
|
||||
# create-target: db-migration.sql
|
||||
# database:
|
||||
# action: none
|
||||
# hibernate:
|
||||
# ddl-auto: none
|
||||
# generate-ddl: true
|
||||
# jpa:
|
||||
# properties:
|
||||
# javax:
|
||||
# persistence:
|
||||
# schema-generation:
|
||||
# create-source: metadata
|
||||
# scripts:
|
||||
# action: update
|
||||
# create-target: db-migration.sql
|
||||
# database:
|
||||
# action: none
|
||||
# hibernate:
|
||||
# ddl-auto: none
|
||||
# generate-ddl: true
|
||||
|
||||
11
src/main/resources/db/migration/V2__quizz_and_questions.sql
Normal file
11
src/main/resources/db/migration/V2__quizz_and_questions.sql
Normal file
@ -0,0 +1,11 @@
|
||||
create table answers (id bigint generated by default as identity, value oid, question bigint not null, primary key (id));
|
||||
create table questions (id bigint generated by default as identity, index integer not null, value oid, quizz bigint not null, primary key (id));
|
||||
create table quizz (id bigint generated by default as identity, is_public boolean default false not null, name varchar(255) not null, question_count integer default 0 not null, owner bigint, primary key (id));
|
||||
create table quizzf (id bigint generated by default as identity, answer_step integer not null, current_question integer not null, done boolean not null, quizz bigint not null, answerer bigint not null, primary key (id));
|
||||
alter table if exists quizz drop constraint if exists UKc1plspc0ecmwqqpfaf24avb4c;
|
||||
alter table if exists quizz add constraint UKc1plspc0ecmwqqpfaf24avb4c unique (name);
|
||||
alter table if exists answers add constraint FK54dobrdq2u51m4u8s7kg0as8v foreign key (question) references questions;
|
||||
alter table if exists questions add constraint FKq12h25ynjok1m497gwos511te foreign key (quizz) references quizz;
|
||||
alter table if exists quizz add constraint FKfeoogns8m4m4hvno1ttqb30wm foreign key (owner) references users;
|
||||
alter table if exists quizzf add constraint FK4ukbg2yaa93gs5nx1s5d9rqu4 foreign key (quizz) references quizz;
|
||||
alter table if exists quizzf add constraint FKuj3h8r3i4lnxukdqobapwbuq foreign key (answerer) references users;
|
||||
@ -7,11 +7,9 @@
|
||||
<body>
|
||||
<div th:replace="~{header}"/>
|
||||
<main>
|
||||
|
||||
<ul>
|
||||
<li><a href="/questions/newform?q=132">Quizz numéro 1</a></li>
|
||||
<li><a href="/questions/newform?q=177">Quizz numéro 2</a></li>
|
||||
<li><a href="/questions/newform?q=16818">Quizz numéro 3</a></li>
|
||||
<li>Premier item ?</li>
|
||||
<li th:each="q : ${quizz}"><a th:href="@{/questions/newform/{id}(id=${q.id})}">Quizz <span th:text="${q.name}"/></a></li>
|
||||
</ul>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user