Skip to content

Commit

Permalink
Create question functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aplantier committed Feb 11, 2022
1 parent b30e91a commit 889044a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 62 deletions.
32 changes: 25 additions & 7 deletions src/main/java/Model/Controllers/QuestionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import Model.Documents.*;
import Model.Payload.request.CreateQuestionRequest;
import Model.Payload.request.CreateTextQuestionRequest;
import Model.Payload.request.SignupRequest;
import Model.Payload.response.MessageResponse;
import Model.Repositories.QuestionRepository;
import Model.Repositories.ReponsesRepository;
Expand All @@ -16,7 +14,6 @@

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import java.util.Set;

Expand All @@ -40,16 +37,37 @@ public ResponseEntity<?> addRessource(String reponse, @PathVariable String name,
.badRequest()
.body(new MessageResponse("Error: No such ressource!"));
}
@PutMapping("/create")

/**
* Method for a teacher to create a question
* @param createQuestionRequest_a
* @return
*/
@PostMapping("/create")
@PreAuthorize("hasRole('TEACHER')")
public ResponseEntity<?> createQuestion(@Valid @RequestBody CreateQuestionRequest createQuestionRequest_a){
Question questionToAdd ;
Question questionToAdd = null ;

Optional<Question> oquestion = questionRepository.findByEnonce(createQuestionRequest_a.getEnonce());
if (oquestion.isPresent()) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Question Already exist"));
}
switch (createQuestionRequest_a.getQuestionType()){
case "text":
questionToAdd= new Question(createQuestionRequest_a.getEnonce(),((CreateTextQuestionRequest) createQuestionRequest_a).getReponse());
questionToAdd= new Question(createQuestionRequest_a.getEnonce(),createQuestionRequest_a.getReponse());
break;
default:


}
return ResponseEntity
if (questionToAdd != null ) {
questionRepository.save(questionToAdd);
return ResponseEntity.ok(new MessageResponse("Question successfully "));
}
else
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: No such ressource!"));

Expand Down
39 changes: 17 additions & 22 deletions src/main/java/Model/Documents/Question.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package Model.Documents;

import org.hibernate.validator.constraints.Range;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -16,26 +16,21 @@
@Table( name = "Question")
public class Question {
@NotBlank
public String enonce_;
public String enonce="";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@NotBlank
@Size(max = 120)
public String listeEnonces_;
public String listeEnonces_="";

@NotBlank
@Size(max = 120)
public String typeQuestion;
@NotBlank
@Size(max = 120)
public int [] reponsesMultiples;
@NotBlank
@Size(max = 120)
public int reponseQcm;
@NotBlank
public String typeQuestion="";
/* @Range(min=1, max=20)
public int [] reponsesMultiples={};*/
@Range(min=0, max=20)
public int reponseQcm=0;
@Size(max = 120)
public String reponseText;
public String reponseText="";

public Question() {}

Expand All @@ -53,8 +48,8 @@ public Question() {}
public Question(String enonce_, String listrReponses_a, int reponse_a) {
this.listeEnonces_ = listrReponses_a;
this.typeQuestion="qcm";
this.enonce_ = enonce_;
this.reponseQcm = reponseQcm;
this.enonce = enonce_;
this.reponseQcm =reponse_a;
}

public boolean reponse(Reponse reponse){
Expand All @@ -63,13 +58,13 @@ public boolean reponse(Reponse reponse){
return reponse.reponseQcm == this.reponseQcm;

case "choixmult":
if ( reponse.reponsesMultiples.length != reponse.reponsesMultiples.length)
/* if ( reponse.reponsesMultiples.length != reponse.reponsesMultiples.length)
return false ;
for (int i = 0 ; i < reponse.reponsesMultiples.length; i++){
if(reponse.reponsesMultiples[i] == this.reponsesMultiples[i]) {
return false;
}
}
}*/
return true;
case "text":
if(reponse.reponseText.equals(this.reponseText))
Expand All @@ -87,8 +82,8 @@ public boolean reponse(Reponse reponse){
public Question(String enonce_a, String listrReponses_a, int [] reponse_a) {
this.typeQuestion="choixmult";
this.listeEnonces_ = listrReponses_a;
this.enonce_ = enonce_a;
this.reponsesMultiples = reponse_a;
this.enonce = enonce_a;
//this.reponsesMultiples = reponse_a;
}


Expand All @@ -99,7 +94,7 @@ public Question(String enonce_a, String listrReponses_a, int [] reponse_a) {
*/
public Question(String enonce_a, String reponse_a){
this.typeQuestion="text";
this.enonce_=enonce_a;
this.enonce =enonce_a;
this.reponseText=reponse_a;
}
}
43 changes: 37 additions & 6 deletions src/main/java/Model/Payload/request/CreateQuestionRequest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
package Model.Payload.request;

import javax.validation.constraints.Email;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Set;

public class CreateQuestionRequest {
@NotBlank
@Size(min = 3, max = 20)
private String enonce_;
private String enonce;

@NotBlank
@Size(min = 0, max = 20)
String questionType;

@Size(min = 3, max = 20)
private String questionType_;
private String reponse;

@Size(max = 120)
public String listeEnonces_="";

@Size(max = 120)
public String typeQuestion="";
/* @Range(min=1, max=20)
public int [] reponsesMultiples={};*/
@Range(min=0, max=20)
public int reponseQcm=0;
@Size(max = 120)
public String reponseText="";


public String getReponse(){
return this.reponse;
}
public void setReponse(String reponseText){this.reponse=reponseText;}
public CreateQuestionRequest(){
this.enonce="[DEFAULT ENONCE]";
this.questionType="[DEFAULT QUESTION TYPE]";
}

public String getQuestionType(){
return this.questionType_;
return this.questionType;
}

public void setQuestionType(String questionType_a){
this.questionType = questionType_a;
}
public String getEnonce(){
return this.enonce_;
return this.enonce;
}
public void setEnonce(String enonce_a){
this.enonce=enonce_a;
}


Expand Down
17 changes: 0 additions & 17 deletions src/main/java/Model/Payload/request/CreateTextQuestionRequest.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/Model/Repositories/QuestionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
public interface QuestionRepository extends JpaRepository<Question, Long> {

Optional<Question> findById(Long name);

Optional<Question> findByEnonce(String enonce_);
}
28 changes: 21 additions & 7 deletions src/test/java/QuestionnaireTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import Model.Controllers.AuthController;
import Model.Documents.Cours;
import Model.Documents.Module;
import Model.Documents.Questionnaire;
import Model.Repositories.ModuleRepository;
import Model.Repositories.QuestionnaireRepository;
import Model.Repositories.RessourcesRepository;
import Model.Repositories.UserRepository;
import Model.Payload.request.CreateQuestionRequest;
import Model.Repositories.*;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.cucumber.messages.internal.com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -33,6 +31,9 @@ public class QuestionnaireTest extends SpringIntegration {
@Autowired
QuestionnaireRepository questionnaireRepository;

@Autowired
QuestionRepository questionRepository;

@Autowired
AuthController authController;

Expand Down Expand Up @@ -86,7 +87,20 @@ public void questionnairExist(String courseName){
assertTrue(questionnaireRepository.findByName(courseName).isPresent());
}
@When("user {string} create {string} question with enonce {string} and with answer {string}")
public void userCreateTextQuestion(String user_a, String questionType, String enonce_a , String answer_a){
public void userCreateTextQuestion(String user_a, String questionType, String enonce_a , String answer_a) throws UnsupportedEncodingException {
CreateQuestionRequest textQuestionRequest = new CreateQuestionRequest();
textQuestionRequest.setEnonce(enonce_a);
textQuestionRequest.setReponse(answer_a);
textQuestionRequest.setQuestionType("text");
Gson g = new Gson();
String s = g.toJson(textQuestionRequest);
String jwt = authController.generateJwt(user_a, PASSWORD);
executePostObj("http://localhost:8080/api/module/question/create",s,jwt);
System.out.println(latestHttpResponse.getStatusLine().getStatusCode());

}
@Then("Question with enonce {string} exist")
public void questionExistByEnone(String enonce){
assertTrue(questionRepository.findByEnonce(enonce).isPresent());
}
}
16 changes: 16 additions & 0 deletions src/test/java/SpringIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ boolean executePostObj(String url, String obj) throws UnsupportedEncodingExcepti
System.out.println(latestHttpResponse);
return true;
}
boolean executePostObj(String url, String obj, String jwt) throws UnsupportedEncodingException {
HttpPost request = new HttpPost(url);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(obj));
if (jwt != null) {
request.addHeader("Authorization", "Bearer " + jwt);
}
try{
latestHttpResponse = httpClient.execute(request);
}catch(Throwable t){
System.out.println(t.getLocalizedMessage());
return false;
}
System.out.println(latestHttpResponse);
return true;
}

boolean executePost(String url, String jwt) throws IOException {
HttpPost request = new HttpPost(url);
Expand Down
5 changes: 2 additions & 3 deletions src/test/resources/features/Questionnaire.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ Feature: Questionnaire
And Questionnaire "Quest1" has been added

Scenario: Teacher add text question to Questionnaire
When "steve" create "text" question with enonce "Enonce1" and with possible answers "answ1"
And "steve" add "text" question to questionnaire "Quest1"
Then Questionnaire "Quest1" contain the question with Enonce : "Enonce1"
When user "steve" create "text" question with enonce "Enonce1" and with answer "answ1"
Then Question with enonce "Enonce1" exist

0 comments on commit 889044a

Please sign in to comment.