use of de.tum.in.www1.artemis.domain.Course in project ArTEMiS by ls1intum.
the class ProgrammingExerciseResource method getProgrammingExercisesForCourse.
/**
* GET /courses/:courseId/exercises : get all the exercises.
*
* @return the ResponseEntity with status 200 (OK) and the list of programmingExercises in body
*/
@GetMapping(value = "/courses/{courseId}/programming-exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
@Transactional(readOnly = true)
public ResponseEntity<List<ProgrammingExercise>> getProgrammingExercisesForCourse(@PathVariable Long courseId) {
log.debug("REST request to get all ProgrammingExercises for the course with id : {}", courseId);
Course course = courseService.findOne(courseId);
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
List<ProgrammingExercise> exercises = programmingExerciseRepository.findByCourseId(courseId);
return ResponseEntity.ok().body(exercises);
}
use of de.tum.in.www1.artemis.domain.Course in project ArTEMiS by ls1intum.
the class ProgrammingExerciseResource method createProgrammingExercise.
/**
* POST /programming-exercises : Create a new programmingExercise.
*
* @param programmingExercise the programmingExercise to create
* @return the ResponseEntity with status 201 (Created) and with body the new programmingExercise, or with status 400 (Bad Request) if the programmingExercise has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/programming-exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<ProgrammingExercise> createProgrammingExercise(@RequestBody ProgrammingExercise programmingExercise) throws URISyntaxException {
log.debug("REST request to save ProgrammingExercise : {}", programmingExercise);
if (programmingExercise.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new programmingExercise cannot already have an ID")).body(null);
}
// fetch course from database to make sure client didn't change groups
Course course = courseService.findOne(programmingExercise.getCourse().getId());
if (course == null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "courseNotFound", "The course belonging to this programming exercise does not exist")).body(null);
}
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
ResponseEntity<ProgrammingExercise> errorResponse = checkProgrammingExerciseForError(programmingExercise);
if (errorResponse != null) {
return errorResponse;
}
ProgrammingExercise result = programmingExerciseRepository.save(programmingExercise);
return ResponseEntity.created(new URI("/api/programming-exercises/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
use of de.tum.in.www1.artemis.domain.Course in project ArTEMiS by ls1intum.
the class ExerciseResource method createExercise.
/**
* POST /exercises : Create a new exercise.
*
* @param exercise the exercise to create
* @return the ResponseEntity with status 201 (Created) and with body the new exercise, or with status 400 (Bad Request) if the exercise has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public // TODO: test if it still works with abstract entity in body
ResponseEntity<Exercise> createExercise(@RequestBody Exercise exercise) throws URISyntaxException {
log.debug("REST request to save Exercise : {}", exercise);
Course course = exercise.getCourse();
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (exercise.getId() != null) {
throw new BadRequestAlertException("A new exercise cannot already have an ID", ENTITY_NAME, "idexists");
}
if (exercise instanceof ProgrammingExercise) {
ResponseEntity<Exercise> errorResponse = checkProgrammingExerciseForError((ProgrammingExercise) exercise);
if (errorResponse != null) {
return errorResponse;
}
}
Exercise result = exerciseRepository.save(exercise);
return ResponseEntity.created(new URI("/api/exercises/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
use of de.tum.in.www1.artemis.domain.Course in project ArTEMiS by ls1intum.
the class JiraAuthenticationProvider method buildAuthoritiesFromGroups.
/**
* Builds the authorities list from the groups:
* group contains configured instructor group name -> instructor role
* otherwise -> student role
*/
private Set<Authority> buildAuthoritiesFromGroups(List<String> groups) {
Set<Authority> authorities = new HashSet<>();
// Check if user is admin
if (groups.contains(ADMIN_GROUP_NAME)) {
Authority adminAuthority = new Authority();
adminAuthority.setName(AuthoritiesConstants.ADMIN);
authorities.add(adminAuthority);
}
List<String> instructorGroups = courseService.getAllInstructorGroupNames();
// Check if user is an instructor in any course
if (groups.stream().anyMatch(group -> instructorGroups.contains(group))) {
Authority instructorAuthority = new Authority();
instructorAuthority.setName(AuthoritiesConstants.INSTRUCTOR);
authorities.add(instructorAuthority);
}
List<String> teachingAssistantGroups = courseService.getAllTeachingAssistantGroupNames();
// Check if user is a tutor in any course
if (groups.stream().anyMatch(group -> teachingAssistantGroups.contains(group))) {
Authority taAuthority = new Authority();
taAuthority.setName(AuthoritiesConstants.TEACHING_ASSISTANT);
authorities.add(taAuthority);
}
Authority userAuthority = new Authority();
userAuthority.setName(AuthoritiesConstants.USER);
authorities.add(userAuthority);
return authorities;
}
use of de.tum.in.www1.artemis.domain.Course in project ArTEMiS by ls1intum.
the class ResultResource method createResult.
/**
* POST /results : Create a new manual result.
*
* @param result the result to create
* @return the ResponseEntity with status 201 (Created) and with body the new result, or with status 400 (Bad Request) if the result has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/results")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<Result> createResult(@RequestBody Result result) throws URISyntaxException {
log.debug("REST request to save Result : {}", result);
Participation participation = result.getParticipation();
Course course = participation.getExercise().getCourse();
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (result.getId() != null) {
throw new BadRequestAlertException("A new result cannot already have an ID.", ENTITY_NAME, "idexists");
} else if (result.getResultString() == null) {
throw new BadRequestAlertException("Result string is required.", ENTITY_NAME, "resultStringNull");
} else if (result.getScore() == null) {
throw new BadRequestAlertException("Score is required.", ENTITY_NAME, "scoreNull");
} else if (result.getScore() != 100 && result.isSuccessful()) {
throw new BadRequestAlertException("Only result with score 100% can be successful.", ENTITY_NAME, "scoreAndSuccessfulNotMatching");
} else if (!result.getFeedbacks().isEmpty() && result.getFeedbacks().stream().filter(feedback -> feedback.getText() == null).count() != 0) {
throw new BadRequestAlertException("In case feedback is present, feedback text and detail text are mandatory.", ENTITY_NAME, "feedbackTextOrDetailTextNull");
}
if (!result.getFeedbacks().isEmpty()) {
result.setHasFeedback(true);
}
Result savedResult = resultRepository.save(result);
result.getFeedbacks().forEach(feedback -> {
feedback.setResult(savedResult);
feedbackService.save(feedback);
});
ltiService.ifPresent(ltiService -> ltiService.onNewBuildResult(savedResult.getParticipation()));
return ResponseEntity.created(new URI("/api/results/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Aggregations