Search in sources :

Example 46 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class ProgrammingExerciseSolutionEntryResource method createBehavioralSolutionEntries.

/**
 * POST programming-exercises/:exerciseId/behavioral-solution-entries : Create the behavioral solution entries for a programming exercise
 *
 * @param exerciseId of the exercise
 * @return the {@link ResponseEntity} with status {@code 200} and with body the created solution entries,
 */
@PostMapping("programming-exercises/{exerciseId}/behavioral-solution-entries")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<List<ProgrammingExerciseSolutionEntry>> createBehavioralSolutionEntries(@PathVariable Long exerciseId) {
    log.debug("REST request to create behavioral solution entries");
    ProgrammingExercise exercise = programmingExerciseRepository.findByIdWithTemplateAndSolutionParticipationElseThrow(exerciseId);
    authCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.EDITOR, exercise, null);
    try {
        var solutionEntries = behavioralTestCaseService.generateBehavioralSolutionEntries(exercise);
        return ResponseEntity.ok(solutionEntries);
    } catch (BehavioralSolutionEntryGenerationException e) {
        log.error("Unable to create behavioral solution entries", e);
        throw new InternalServerErrorException(e.getMessage());
    }
}
Also used : ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) InternalServerErrorException(de.tum.in.www1.artemis.web.rest.errors.InternalServerErrorException) BehavioralSolutionEntryGenerationException(de.tum.in.www1.artemis.service.hestia.behavioral.BehavioralSolutionEntryGenerationException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 47 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class VideoUnitResource method createVideoUnit.

/**
 * POST /lectures/:lectureId/video-units : creates a new video unit.
 *
 * @param lectureId      the id of the lecture to which the video unit should be added
 * @param videoUnit the video unit that should be created
 * @return the ResponseEntity with status 201 (Created) and with body the new video unit
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/lectures/{lectureId}/video-units")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<VideoUnit> createVideoUnit(@PathVariable Long lectureId, @RequestBody VideoUnit videoUnit) throws URISyntaxException {
    log.debug("REST request to create VideoUnit : {}", videoUnit);
    if (videoUnit.getId() != null) {
        throw new BadRequestException();
    }
    normalizeVideoUrl(videoUnit);
    validateVideoUrl(videoUnit);
    Lecture lecture = lectureRepository.findByIdWithPostsAndLectureUnitsAndLearningGoalsElseThrow(lectureId);
    if (lecture.getCourse() == null) {
        throw new ConflictException("Specified lecture is not part of a course", "VideoUnit", "courseMissing");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, lecture.getCourse(), null);
    // persist lecture unit before lecture to prevent "null index column for collection" error
    videoUnit.setLecture(null);
    videoUnit = videoUnitRepository.saveAndFlush(videoUnit);
    videoUnit.setLecture(lecture);
    lecture.addLectureUnit(videoUnit);
    Lecture updatedLecture = lectureRepository.save(lecture);
    VideoUnit persistedVideoUnit = (VideoUnit) updatedLecture.getLectureUnits().get(updatedLecture.getLectureUnits().size() - 1);
    return ResponseEntity.created(new URI("/api/video-units/" + persistedVideoUnit.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, "")).body(persistedVideoUnit);
}
Also used : Lecture(de.tum.in.www1.artemis.domain.Lecture) ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) BadRequestException(javax.ws.rs.BadRequestException) VideoUnit(de.tum.in.www1.artemis.domain.lecture.VideoUnit) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 48 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class PostResource method getPostsInCourse.

/**
 * GET /courses/{courseId}/posts : Get all posts for a course by its id
 *
 * @param pageable                  pagination settings to fetch posts in smaller batches
 * @param pagingEnabled             flag stating whether requesting component has paging enabled or not
 * @param postContextFilter         request param for filtering posts
 * @return ResponseEntity with status 200 (OK) and with body all posts for course, that match the specified context
 * or 400 (Bad Request) if the checks on user, course or post validity fail
 */
@GetMapping("courses/{courseId}/posts")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<List<Post>> getPostsInCourse(@ApiParam Pageable pageable, @RequestParam(defaultValue = "false") boolean pagingEnabled, PostContextFilter postContextFilter) {
    Page<Post> coursePosts = postService.getPostsInCourse(pagingEnabled, pageable, postContextFilter);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), coursePosts);
    return new ResponseEntity<>(coursePosts.getContent(), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Post(de.tum.in.www1.artemis.domain.metis.Post) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 49 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class AnswerPostService method deleteAnswerPostById.

/**
 * Checks course and user validity,
 * determines authority to delete post and deletes the post
 *
 * @param courseId     id of the course the answer post belongs to
 * @param answerPostId id of the answer post to delete
 */
public void deleteAnswerPostById(Long courseId, Long answerPostId) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    // checks
    final Course course = preCheckUserAndCourse(user, courseId);
    AnswerPost answerPost = answerPostRepository.findByIdElseThrow(answerPostId);
    mayUpdateOrDeletePostingElseThrow(answerPost, user, course);
    // delete
    answerPostRepository.deleteById(answerPostId);
    // we need to explicitly remove the answer post from the answers of the broadcast post to share up-to-date information
    Post updatedPost = answerPost.getPost();
    updatedPost.removeAnswerPost(answerPost);
    broadcastForPost(new MetisPostDTO(updatedPost, MetisPostAction.UPDATE_POST), course);
}
Also used : User(de.tum.in.www1.artemis.domain.User) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) MetisPostDTO(de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO) Course(de.tum.in.www1.artemis.domain.Course) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost)

Example 50 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class AnswerPostService method createAnswerPost.

/**
 * Checks course, user and answer post and associated post validity,
 * determines the associated post, the answer post's author,
 * sets to approved if author is at least a tutor,
 * persists the answer post, and sends a notification to affected user groups
 *
 * @param courseId   id of the course the answer post belongs to
 * @param answerPost answer post to create
 * @return created answer post that was persisted
 */
public AnswerPost createAnswerPost(Long courseId, AnswerPost answerPost) {
    final User user = this.userRepository.getUserWithGroupsAndAuthorities();
    // check
    if (answerPost.getId() != null) {
        throw new BadRequestAlertException("A new answer post cannot already have an ID", METIS_ANSWER_POST_ENTITY_NAME, "idexists");
    }
    final Course course = preCheckUserAndCourse(user, courseId);
    Post post = postRepository.findByIdElseThrow(answerPost.getPost().getId());
    // use post from database rather than user input
    answerPost.setPost(post);
    // set author to current user
    answerPost.setAuthor(user);
    // on creation of an answer post, we set the resolves_post field to false per default
    answerPost.setResolvesPost(false);
    AnswerPost savedAnswerPost = answerPostRepository.save(answerPost);
    this.preparePostAndBroadcast(savedAnswerPost, course);
    sendNotification(post, answerPost, course);
    return savedAnswerPost;
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) User(de.tum.in.www1.artemis.domain.User) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) Course(de.tum.in.www1.artemis.domain.Course) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost)

Aggregations

Post (de.tum.in.www1.artemis.domain.metis.Post)155 WithMockUser (org.springframework.security.test.context.support.WithMockUser)150 Test (org.junit.jupiter.api.Test)136 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)118 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)97 User (de.tum.in.www1.artemis.domain.User)88 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)87 URI (java.net.URI)63 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)56 Course (de.tum.in.www1.artemis.domain.Course)52 Transactional (org.springframework.transaction.annotation.Transactional)37 Reaction (de.tum.in.www1.artemis.domain.metis.Reaction)36 Test (org.junit.Test)36 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)36 Exercise (de.tum.in.www1.artemis.domain.Exercise)35 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 Exam (de.tum.in.www1.artemis.domain.exam.Exam)20 Lecture (de.tum.in.www1.artemis.domain.Lecture)16 ProgrammingExercise (de.tum.in.www1.artemis.domain.ProgrammingExercise)16 SortingOrder (de.tum.in.www1.artemis.domain.enumeration.SortingOrder)16