Skip to content
New issue

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

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

Already on GitHub? # to your account

Survey classes #14

Merged
merged 8 commits into from
Mar 4, 2023
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
</plugins>
</build>

</project>
</project>
63 changes: 63 additions & 0 deletions src/main/java/com/group11/surveymonkey/Survey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.group11.surveymonkey;

import jakarta.persistence.*;

@Entity
public class Survey {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id = null;
private String surveyName;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "survey")
private List<TextQnA> textList;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "survey")
private List<RangeQnA> rangeList;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "survey")
private List<ChoiceQnA> choiceList;

public Survey() {

}
public Survey(String surveyName){
this.surveyName = surveyName;
}
public void addTextQnA(TextQnA newQuestion){
this.textList.add(newQuestion);
}
public void setTextList(List<TextQnA> newQuestionList){
this.textList=newQuestionList;
}

public void addRangeQnA(RangeQnA newQuestion){
this.rangeList.add(newQuestion);
}
public void setRangeList(List<RangeQnA> newQuestionList){
this.rangeList=newQuestionList;
}

public void addChoiceQnA(ChoiceQnA newQuestion){
this.choiceList.add(newQuestion);
}
public void setChoiceList(List<ChoiceQnA> newQuestionList){
this.choiceList=newQuestionList;
}
public Integer getSurveyID(){
return this.id;
}
public void setSurveyID(Integer i){
this.id=i;
}
public String getSurveyName(){
return this.surveyName;
}
public void setSurveyName(String surveyName){
this.surveyName = surveyName;
}

@Override
public String toString(){
return surveyName;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/group11/surveymonkey/SurveyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.group11.surveymonkey;


import java.util.List;
import java.util.Optional;
import org.example.SurveyRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class SurveyController {
@Autowired
private SurveyRepository surveyService;

@PostMapping("/survey")
public Survey saveSurvey(){
return surveyService.save(new Survey());
}

@GetMapping({"/survey/{id}"})
public Optional<Survey> getSurvey(@PathVariable("id") int id) {
return surveyService.findById(id);
}

@PutMapping({"/survey/{id}/newQuestion"})
public String addQuestion(@RequestParam("qid") Integer QuestionId, @PathVariable("id") int id, @RequestParam("type") int questionType) {
if (questionType==0){
TextQnA newQuestion = new TextQnA;
surveyService.findById(id).ifPresent(x-> x.addTextQnA(newQuestion));
}else if (questionType==1){
RangeQnA newQuestion = new RangeQnA;
surveyService.findById(id).ifPresent(x-> x.addRangeQnA(newQuestion));
}else{
ChoiceQnA newQuestion = new ChoiceQnA;
surveyService.findById(id).ifPresent(x-> x.addChoiceQnA(newQuestion));
}
surveyService.findById(id).ifPresent(x-> surveyService.save(x));
return "Added successfully";
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/group11/surveymonkey/SurveyRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.group11.surveymonkey;
import java.util.List;
import java.util.Optional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "survey", path = "survey")

public interface SurveyRepository extends CrudRepository<Survey, Integer>{
}
15 changes: 15 additions & 0 deletions src/main/java/com/group11/surveymonkey/SurveyService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.group11.surveymonkey;

import java.util.List;
import java.util.Optional;

/**
* This interface is implemented by SurveyServiceImpl,
* which will expand on the methods under this interface
* and use it for its own purpose.
*/
public interface SurveyService {
Survey saveSurvey(Survey survey);
Optional<Survey> getSurvey(Integer surveyId);
void addQuestion(Integer questionId, Integer surveyId, Integer questionType);
}
42 changes: 42 additions & 0 deletions src/main/java/com/group11/surveymonkey/SurveyServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.group11.surveymonkey;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SurveyServiceImpl implements SurveyService{
@Autowired
private SurveyRepository surveyRepository;
private TextQnARepository textQnARepository;
private RangeQnARepository rangeQnARepository;
private ChoiceQnARepository choiceQnARepository;
@Override
public Survey saveSurvey(Survey survey) {
return surveyRepository.save(survey);
}

@Override
public Optional<Survey> getSurvey(Integer surveyId) {
return surveyRepository.findById(surveyId);
}

@Override
public void addQuestion(Integer questionId, Integer surveyId, Integer questionType) {
Survey upSurvey = surveyRepository.findById(surveyId).get();
if (questionType == 0) {
TextQnA textQnA = textQnARepository.findById(questionId).get();
upSurvey.addTextQnA(textQnA);
}
else if (questionType == 1) {
RangeQnA rangeQnA = rangeQnARepository.findById(questionId).get();
upSurvey.addRangeQnA(rangeQnA);
}
else {
ChoiceQnA choiceQnA = choiceQnARepository.findById(questionId).get();
upSurvey.addChoiceQnA(choiceQnA);
}
surveyRepository.save(upSurvey);
}
}