Search in sources :

Example 1 with Course

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

the class CourseResourceIntTest method createCourse.

@Test
@Transactional
public void createCourse() throws Exception {
    int databaseSizeBeforeCreate = courseRepository.findAll().size();
    // Create the Course
    restCourseMockMvc.perform(post("/api/courses").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(course))).andExpect(status().isCreated());
    // Validate the Course in the database
    List<Course> courseList = courseRepository.findAll();
    assertThat(courseList).hasSize(databaseSizeBeforeCreate + 1);
    Course testCourse = courseList.get(courseList.size() - 1);
    assertThat(testCourse.getTitle()).isEqualTo(DEFAULT_TITLE);
    assertThat(testCourse.getStudentGroupName()).isEqualTo(DEFAULT_STUDENT_GROUP_NAME);
    assertThat(testCourse.getTeachingAssistantGroupName()).isEqualTo(DEFAULT_TEACHING_ASSISTANT_GROUP_NAME);
    assertThat(testCourse.getInstructorGroupName()).isEqualTo(DEFAULT_INSTRUCTOR_GROUP_NAME);
    assertThat(testCourse.getStartDate()).isEqualTo(DEFAULT_START_DATE);
    assertThat(testCourse.getEndDate()).isEqualTo(DEFAULT_END_DATE);
    assertThat(testCourse.isOnlineCourse()).isEqualTo(DEFAULT_ONLINE_COURSE);
}
Also used : Course(de.tum.in.www1.artemis.domain.Course) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Course

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

the class CourseResourceIntTest method updateCourse.

@Test
@Transactional
public void updateCourse() throws Exception {
    // Initialize the database
    courseService.save(course);
    int databaseSizeBeforeUpdate = courseRepository.findAll().size();
    // Update the course
    Course updatedCourse = courseRepository.findOne(course.getId());
    updatedCourse.title(UPDATED_TITLE).studentGroupName(UPDATED_STUDENT_GROUP_NAME).teachingAssistantGroupName(UPDATED_TEACHING_ASSISTANT_GROUP_NAME).instructorGroupName(UPDATED_INSTRUCTOR_GROUP_NAME).startDate(UPDATED_START_DATE).endDate(UPDATED_END_DATE).onlineCourse(UPDATED_ONLINE_COURSE);
    restCourseMockMvc.perform(put("/api/courses").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(updatedCourse))).andExpect(status().isOk());
    // Validate the Course in the database
    List<Course> courseList = courseRepository.findAll();
    assertThat(courseList).hasSize(databaseSizeBeforeUpdate);
    Course testCourse = courseList.get(courseList.size() - 1);
    assertThat(testCourse.getTitle()).isEqualTo(UPDATED_TITLE);
    assertThat(testCourse.getStudentGroupName()).isEqualTo(UPDATED_STUDENT_GROUP_NAME);
    assertThat(testCourse.getTeachingAssistantGroupName()).isEqualTo(UPDATED_TEACHING_ASSISTANT_GROUP_NAME);
    assertThat(testCourse.getInstructorGroupName()).isEqualTo(UPDATED_INSTRUCTOR_GROUP_NAME);
    assertThat(testCourse.getStartDate()).isEqualTo(UPDATED_START_DATE);
    assertThat(testCourse.getEndDate()).isEqualTo(UPDATED_END_DATE);
    assertThat(testCourse.isOnlineCourse()).isEqualTo(UPDATED_ONLINE_COURSE);
}
Also used : Course(de.tum.in.www1.artemis.domain.Course) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Course

use of de.tum.in.www1.artemis.domain.Course 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("hasAnyRole('ADMIN')")
@Timed
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", ENTITY_NAME, "idexists");
    }
    Course result = courseService.save(course);
    return ResponseEntity.created(new URI("/api/courses/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with Course

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

the class ParticipationResource method createParticipation.

/**
 * POST  /participations : Create a new participation.
 *
 * @param participation the participation to create
 * @return the ResponseEntity with status 201 (Created) and with body the new participation, or with status 400 (Bad Request) if the participation has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/participations")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<Participation> createParticipation(@RequestBody Participation participation) throws URISyntaxException {
    log.debug("REST request to save Participation : {}", participation);
    Course course = participation.getExercise().getCourse();
    if (!courseService.userHasTAPermissions(course)) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
    if (participation.getId() != null) {
        throw new BadRequestAlertException("A new participation cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Participation result = participationService.save(participation);
    return ResponseEntity.created(new URI("/api/participations/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with Course

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

the class ProgrammingExerciseResource method getAllProgrammingExercises.

/**
 * GET  /programming-exercises : get all the programmingExercises.
 *
 * @return the ResponseEntity with status 200 (OK) and the list of programmingExercises in body
 */
@GetMapping("/programming-exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public List<ProgrammingExercise> getAllProgrammingExercises() {
    log.debug("REST request to get all ProgrammingExercises");
    List<ProgrammingExercise> exercises = programmingExerciseRepository.findAll();
    Stream<ProgrammingExercise> authorizedExercises = exercises.stream().filter(exercise -> {
        Course course = exercise.getCourse();
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isTeachingAssistantInCourse(course, user) || authCheckService.isInstructorInCourse(course, user) || authCheckService.isAdmin();
    });
    return authorizedExercises.collect(Collectors.toList());
}
Also used : User(de.tum.in.www1.artemis.domain.User) ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) Course(de.tum.in.www1.artemis.domain.Course) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 Course (de.tum.in.www1.artemis.domain.Course)9 URI (java.net.URI)7 ProgrammingExercise (de.tum.in.www1.artemis.domain.ProgrammingExercise)6 User (de.tum.in.www1.artemis.domain.User)6 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)6 Transactional (org.springframework.transaction.annotation.Transactional)5 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)3 de.tum.in.www1.artemis.service (de.tum.in.www1.artemis.service)3 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)3 URISyntaxException (java.net.URISyntaxException)3 Test (org.junit.Test)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 HttpStatus (org.springframework.http.HttpStatus)3 ResponseEntity (org.springframework.http.ResponseEntity)3 Authentication (org.springframework.security.core.Authentication)3 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)3