Skip to content

Commit

Permalink
2 bugfixes: delete course, remove quiz exercise from schedule service…
Browse files Browse the repository at this point in the history
… correctly if it was deleted
  • Loading branch information
Stephan Krusche committed May 5, 2019
1 parent 4fbe2fa commit 5fcc77a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/tum/in/www1/artemis/domain/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public class Course implements Serializable {
@Column(name = "registration_enabled")
private Boolean registrationEnabled;

@OneToMany(mappedBy = "course", orphanRemoval = true, fetch = FetchType.LAZY)
@OneToMany(mappedBy = "course", fetch = FetchType.LAZY)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnoreProperties("course")
private Set<Exercise> exercises = new HashSet<>();

@OneToMany(mappedBy = "course", orphanRemoval = true, fetch = FetchType.EAGER)
@OneToMany(mappedBy = "course", fetch = FetchType.EAGER)
@JsonIgnoreProperties(value = "course", allowSetters = true)
private Set<Lecture> lectures = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void delete(Exercise exercise, boolean deleteStudentReposBuildPlans, bool
}
versionControlService.get().deleteProject(programmingExercise.getProjectKey());
}
exerciseRepository.deleteById(exercise.getId());
exerciseRepository.delete(exercise);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Set<Lecture> filterActiveAttachments(Set<Lecture> lecturesWithAttachments
}

public void delete(Lecture lecture) {
lectureRepository.deleteById(lecture.getId());
lectureRepository.delete(lecture);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public List<QuizExercise> findAll() {
* @return the entity
*/
@Transactional(readOnly = true)
public QuizExercise findOne(Long id) {
public Optional<QuizExercise> findById(Long id) {
log.debug("Request to get Quiz Exercise : {}", id);
return quizExerciseRepository.findById(id).get();
return quizExerciseRepository.findById(id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,15 @@ private void run() {
for (long quizId : participationHashMap.keySet()) {

// get the Quiz without the statistics and questions from the database
QuizExercise quizExercise = quizExerciseService.findOne(quizId);
Optional<QuizExercise> quizExercise = quizExerciseService.findById(quizId);
// check if quiz has been deleted
if (quizExercise == null) {
if (!quizExercise.isPresent()) {
participationHashMap.remove(quizId);
continue;
}

// check if the quiz has ended
if (quizExercise.isEnded()) {
if (quizExercise.get().isEnded()) {
// send the participation with containing result and quiz back to the users via websocket
// and remove the participation from the ParticipationHashMap
int counter = 0;
Expand All @@ -291,7 +291,7 @@ private void run() {
counter++;
}
if (counter > 0) {
log.info("Sent out {} participations after {} ms for quiz {}", counter, System.currentTimeMillis() - start, quizExercise.getTitle());
log.info("Sent out {} participations after {} ms for quiz {}", counter, System.currentTimeMillis() - start, quizExercise.get().getTitle());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public ResponseEntity<QuizExercise> updateQuizExercise(@RequestBody QuizExercise
}

// check if quiz is has already started
QuizExercise originalQuiz = quizExerciseService.findOne(quizExercise.getId());
if (originalQuiz == null) {
Optional<QuizExercise> originalQuiz = quizExerciseService.findById(quizExercise.getId());
if (!originalQuiz.isPresent()) {
return ResponseEntity.notFound()
.headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "quizExerciseNotFound", "The quiz exercise does not exist yet. Use POST to create a new quizExercise."))
.build();
}
if (originalQuiz.isStarted()) {
if (originalQuiz.get().isStarted()) {
return ResponseEntity.badRequest().headers(
HeaderUtil.createFailureAlert(ENTITY_NAME, "quizHasStarted", "The quiz has already started. Use the re-evaluate endpoint to make retroactive corrections."))
.body(null);
Expand Down Expand Up @@ -318,12 +318,12 @@ public ResponseEntity<String> performActionForQuizExercise(@PathVariable Long id
public ResponseEntity<Void> deleteQuizExercise(@PathVariable Long id) {
log.debug("REST request to delete QuizExercise : {}", id);

QuizExercise quizExercise = quizExerciseService.findOne(id);
if (quizExercise == null) {
Optional<QuizExercise> quizExercise = quizExerciseService.findById(id);
if (!quizExercise.isPresent()) {
return ResponseEntity.notFound().build();
}

Course course = quizExercise.getCourse();
Course course = quizExercise.get().getCourse();
if (!courseService.userHasAtLeastInstructorPermissions(course)) {
return forbidden();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.security.Principal;
import java.time.ZonedDateTime;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -46,15 +47,18 @@ public void saveSubmission(@DestinationVariable Long exerciseId, @Payload QuizSu
String username = principal.getName();

// check if submission is still allowed
QuizExercise quizExercise = quizExerciseService.findOne(exerciseId);
if (!quizExercise.isSubmissionAllowed()) {
Optional<QuizExercise> quizExercise = quizExerciseService.findById(exerciseId);
if (!quizExercise.isPresent()) {
return;
}
if (!quizExercise.get().isSubmissionAllowed()) {
// TODO: notify user that submission was not saved because quiz is not active over payload and handle this case in the client
messagingTemplate.convertAndSendToUser(username, "/topic/quizExercise/" + exerciseId + "/submission", null);
return;
}

// check if user already submitted for this quiz
Participation participation = participationService.participationForQuizWithResult(quizExercise, username);
Participation participation = participationService.participationForQuizWithResult(quizExercise.get(), username);
if (!participation.getResults().isEmpty()) {
// NOTE: At this point, there can only be one Result because we already checked
// if the quiz is active, so there is no way the student could have already practiced
Expand Down

0 comments on commit 5fcc77a

Please sign in to comment.