Search in sources :

Example 71 with Post

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

the class AttachmentResource method createAttachment.

/**
 * POST /attachments : Create a new attachment.
 *
 * @param attachment the attachment to create
 * @return the ResponseEntity with status 201 (Created) and with body the new attachment, or with status 400 (Bad Request) if the attachment has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/attachments")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<Attachment> createAttachment(@RequestBody Attachment attachment) throws URISyntaxException {
    log.debug("REST request to save Attachment : {}", attachment);
    if (attachment.getId() != null) {
        throw new BadRequestAlertException("A new attachment cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Attachment result = attachmentRepository.save(attachment);
    this.cacheManager.getCache("files").evict(fileService.actualPathForPublicPath(result.getLink()));
    return ResponseEntity.created(new URI("/api/attachments/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) Attachment(de.tum.in.www1.artemis.domain.Attachment) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 72 with Post

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

the class ComplaintResource method createComplaintForExamExercise.

/**
 * POST complaints/exam/examId: create a new complaint for an exam exercise
 *
 * @param complaint the complaint to create
 * @param principal that wants to complain
 * @param examId the examId of the exam which contains the exercise
 * @return the ResponseEntity with status 201 (Created) and with body the new complaints
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
// TODO: should be exams/{examId}/(participations/{participationId}/)complaints
@PostMapping("complaints/exam/{examId}")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Complaint> createComplaintForExamExercise(@PathVariable Long examId, @RequestBody Complaint complaint, Principal principal) throws URISyntaxException {
    log.debug("REST request to save Complaint for exam exercise: {}", complaint);
    if (complaint.getId() != null) {
        throw new BadRequestAlertException("A new complaint cannot already have an id", COMPLAINT_ENTITY_NAME, "idexists");
    }
    if (complaint.getResult() == null || complaint.getResult().getId() == null) {
        throw new BadRequestAlertException("A complaint can be only associated to a result", COMPLAINT_ENTITY_NAME, "noresultid");
    }
    if (complaintRepository.findByResultId(complaint.getResult().getId()).isPresent()) {
        throw new BadRequestAlertException("A complaint for this result already exists", COMPLAINT_ENTITY_NAME, "complaintexists");
    }
    Result result = resultRepository.findByIdElseThrow(complaint.getResult().getId());
    // For non-exam exercises, the POST complaints should be used
    if (!result.getParticipation().getExercise().isExamExercise()) {
        throw new BadRequestAlertException("A complaint for an course exercise cannot be filed using this component", COMPLAINT_ENTITY_NAME, "complaintAboutCourseExerciseWrongComponent");
    }
    authCheckService.isOwnerOfParticipationElseThrow((StudentParticipation) result.getParticipation());
    // To build correct creation alert on the front-end we must check which type is the complaint to apply correct i18n key.
    String entityName = complaint.getComplaintType() == ComplaintType.MORE_FEEDBACK ? MORE_FEEDBACK_ENTITY_NAME : COMPLAINT_ENTITY_NAME;
    Complaint savedComplaint = complaintService.createComplaint(complaint, OptionalLong.of(examId), principal);
    // Remove assessor information from client request
    savedComplaint.getResult().setAssessor(null);
    return ResponseEntity.created(new URI("/api/complaints/" + savedComplaint.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, entityName, savedComplaint.getId().toString())).body(savedComplaint);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 73 with Post

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

the class ComplaintResource method createComplaint.

/**
 * POST complaints: create a new complaint
 *
 * @param complaint the complaint to create
 * @param principal that wants to complain
 * @return the ResponseEntity with status 201 (Created) and with body the new complaints
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
// TODO: should be participations/{participationId}/results/{resultId}/complaints
@PostMapping("complaints")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Complaint> createComplaint(@RequestBody Complaint complaint, Principal principal) throws URISyntaxException {
    log.debug("REST request to save Complaint: {}", complaint);
    if (complaint.getId() != null) {
        throw new BadRequestAlertException("A new complaint cannot already have an id", COMPLAINT_ENTITY_NAME, "idexists");
    }
    if (complaint.getResult() == null || complaint.getResult().getId() == null) {
        throw new BadRequestAlertException("A complaint can be only associated to a result", COMPLAINT_ENTITY_NAME, "noresultid");
    }
    if (complaintRepository.findByResultId(complaint.getResult().getId()).isPresent()) {
        throw new BadRequestAlertException("A complaint for this result already exists", COMPLAINT_ENTITY_NAME, "complaintexists");
    }
    Result result = resultRepository.findByIdElseThrow(complaint.getResult().getId());
    // For exam exercises, the POST complaints/exam/examId should be used
    if (result.getParticipation().getExercise().isExamExercise()) {
        throw new BadRequestAlertException("A complaint for an exam exercise cannot be filed using this component", COMPLAINT_ENTITY_NAME, "complaintAboutExamExerciseWrongComponent");
    }
    authCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.STUDENT, result.getParticipation().getExercise(), null);
    // To build correct creation alert on the front-end we must check which type is the complaint to apply correct i18n key.
    String entityName = complaint.getComplaintType() == ComplaintType.MORE_FEEDBACK ? MORE_FEEDBACK_ENTITY_NAME : COMPLAINT_ENTITY_NAME;
    Complaint savedComplaint = complaintService.createComplaint(complaint, OptionalLong.empty(), principal);
    // Remove assessor information from client request
    savedComplaint.getResult().setAssessor(null);
    return ResponseEntity.created(new URI("/api/complaints/" + savedComplaint.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, entityName, savedComplaint.getId().toString())).body(savedComplaint);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 74 with Post

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

the class CourseResource method createCourse.

/**
 * POST /courses : create a new course.
 *
 * @param course the course to create
 * @return the ResponseEntity with status 201 (Created) and with body the new course, or with status 400 (Bad Request) if the course has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/courses")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Course> createCourse(@RequestBody Course course) throws URISyntaxException {
    log.debug("REST request to save Course : {}", course);
    if (course.getId() != null) {
        throw new BadRequestAlertException("A new course cannot already have an ID", Course.ENTITY_NAME, "idexists");
    }
    course.validateShortName();
    List<Course> coursesWithSameShortName = courseRepository.findAllByShortName(course.getShortName());
    if (!coursesWithSameShortName.isEmpty()) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createAlert(applicationName, "A course with the same short name already exists. Please choose a different short name.", "shortnameAlreadyExists")).body(null);
    }
    course.validateRegistrationConfirmationMessage();
    course.validateComplaintsAndRequestMoreFeedbackConfig();
    course.validateOnlineCourseAndRegistrationEnabled();
    course.validateAccuracyOfScores();
    if (!course.isValidStartAndEndDate()) {
        throw new BadRequestAlertException("For Courses, the start date has to be before the end date", Course.ENTITY_NAME, "invalidCourseStartDate", true);
    }
    courseService.createOrValidateGroups(course);
    Course result = courseRepository.save(course);
    return ResponseEntity.created(new URI("/api/courses/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, Course.ENTITY_NAME, result.getTitle())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 75 with Post

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

the class ProgrammingExerciseSimulationResource method createProgrammingExerciseWithoutVersionControlAndContinuousIntegrationAvailable.

/**
 * POST /programming-exercises/no-vcs-and-ci-available: Setup a new programmingExercise
 * This method creates a new exercise
 * This exercise is only a SIMULATION for the testing of programming exercises without a connection to the VCS and CI server
 * This functionality is only for testing purposes (noVersionControlAndContinuousIntegrationAvailable)
 * @param programmingExercise the input to create/setup new exercise
 * @return a Response Entity
 */
@PostMapping(ProgrammingExerciseSimulationResource.Endpoints.EXERCISES_SIMULATION)
@PreAuthorize("hasRole('EDITOR')")
@FeatureToggle(Feature.ProgrammingExercises)
public ResponseEntity<ProgrammingExercise> createProgrammingExerciseWithoutVersionControlAndContinuousIntegrationAvailable(@RequestBody ProgrammingExercise programmingExercise) {
    log.debug("REST request to setup ProgrammingExercise : {}", programmingExercise);
    // fetch course from database to make sure client didn't change groups
    Course course = courseRepository.findByIdElseThrow(programmingExercise.getCourseViaExerciseGroupOrCourseMember().getId());
    authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, course, null);
    programmingExercise.setCourse(course);
    programmingExercise.generateAndSetProjectKey();
    try {
        ProgrammingExercise newProgrammingExercise = programmingExerciseSimulationService.createProgrammingExerciseWithoutVersionControlAndContinuousIntegrationAvailable(programmingExercise);
        // Setup all repositories etc
        programmingExerciseSimulationService.setupInitialSubmissionsAndResults(programmingExercise);
        return ResponseEntity.created(new URI("/api/programming-exercises" + newProgrammingExercise.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, newProgrammingExercise.getTitle())).body(newProgrammingExercise);
    } catch (URISyntaxException e) {
        log.error("Error while setting up programming exercise", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).headers(HeaderUtil.createAlert(applicationName, "An error occurred while setting up the exercise: " + e.getMessage(), "errorProgrammingExercise")).body(null);
    }
}
Also used : ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) URISyntaxException(java.net.URISyntaxException) Course(de.tum.in.www1.artemis.domain.Course) URI(java.net.URI) PostMapping(org.springframework.web.bind.annotation.PostMapping) FeatureToggle(de.tum.in.www1.artemis.service.feature.FeatureToggle) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

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