Skip to content
Snippets Groups Projects

Resolve "[Queue-2.0] Create Course and Create Edition buttons"

Files

/*
* Queue - A Queueing system that can be used to handle labs in higher education
* Copyright (C) 2016-2021 Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.queue.controller;
import javax.validation.Valid;
import nl.tudelft.labracore.api.*;
import nl.tudelft.labracore.api.dto.*;
import nl.tudelft.labracore.lib.security.user.AuthenticatedPerson;
import nl.tudelft.labracore.lib.security.user.Person;
import nl.tudelft.queue.dto.create.QueueCourseCreateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@PreAuthorize("@permissionService.isAdmin()")
@RequestMapping("/admin")
public class AdminController {
@Autowired
private CourseControllerApi cApi;
@Autowired
private ProgramControllerApi programApi;
/**
* Adds a model attribute to every template resolution for recognizing the type of page we are in. For
* this controller all pages will be in the "admin"-area.
*
* @return The generic page name: "admin".
*/
@ModelAttribute("page")
public static String page() {
return "admin";
}
/**
* Return the main admin page.
*
* @return The Thymeleaf view to resolve.
*/
@GetMapping
public String getAdminPage() {
return "admin/view";
}
/**
* Get the list of courses in Queue.
*
* @param model The model to fill out for Thymeleaf template resolution.
* @return The thymeleaf template to resolve.
*/
@GetMapping("/courses")
public String getAllCourses(Model model) {
var courses = cApi.getAllCourses().collectList().block();
model.addAttribute("courses", courses);
return "admin/view/courseList";
}
/**
* Get the admin overview for a specific course.
*
* @param courseId The id of the course to retrieve.
* @param model The model to fill out for Thymeleaf template resolution.
* @return The thymeleaf template to resolve.
*/
@GetMapping("/course/{courseId}")
public String viewCourse(@PathVariable Long courseId, Model model) {
var course = cApi.getCourseById(courseId).block();
model.addAttribute("course", course);
return "admin/view/course";
}
/**
*
* @param model The model to fill out for Thymeleaf template resolution.
* @return The thymeleaf template to resolve.
*/
@GetMapping("/course/add")
public String getCreateCoursePage(@AuthenticatedPerson Person person, Model model) {
QueueCourseCreateDTO create = QueueCourseCreateDTO.builder().build();
model.addAttribute("course", create);
model.addAttribute("programs",
programApi.getAllRelevantPrograms(person.getId()).collectList().block());
return "admin/create/course";
}
/**
* Creates a new course based on the infromation filled in in the {@link QueueCourseCreateDTO} object.
*
* @param create The DTO used to create the course
* @return A redirect to the admin page
*/
@PostMapping("/course/add")
public String createCourse(@Valid @ModelAttribute("course") QueueCourseCreateDTO create) {
CourseCreateDTO course = create.apply();
cApi.addCourse(course).block();
return "redirect:/admin";
}
}
Loading