FormsController.java

/*
 * +====================================================================+
 * |         Copyright (C) 2015 Rochester Institute of Technology,      |
 * |            103 Lomb Memorial Drive, Rochester, NY - 14623          |
 * |                        All Rights Reserved.                        |
 * +====================================================================+
 *   FILENAME
 *    FormsController.java
 *
 *   AUTHOR
 *    @author Khanh Ho (kchisd at rit.edu)
 *
 * =====================================================================
 */

package edu.rit.coopeval.controller.rest;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonView;
import edu.rit.coopeval.dao.DepartmentTermFormRepository;
import edu.rit.coopeval.form.FormType;
import edu.rit.coopeval.model.DepartmentTermForm;
import edu.rit.coopeval.model.User;
import edu.rit.coopeval.security.SecurityUser;
import edu.rit.coopeval.viewmodel.JsonViewer;
import edu.rit.coopeval.viewmodel.form.FormSummaryViewModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller for endpoints /api/forms
 */
@RestController
@RequestMapping(path = "api/forms", produces = "application/json")
public class FormsController {

    @Autowired
    private DepartmentTermFormRepository deptTermFormRepo;

    /**
     * GET forms assigned the department of the currently logged in user.
     *
     * @param sUser the current principal
     *
     * @return the list of forms
     */
    @RequestMapping(path = "department")
    @JsonView(JsonViewer.Summary.class)
    @PreAuthorize("hasAuthority('Evaluator')")
    public List<DepartmentTermForm> getDepartmentForms(@AuthenticationPrincipal SecurityUser sUser) {
        User user = sUser.user();
        return deptTermFormRepo.findByDepartmentEvaluatorsUsername(user.getUsername());
    }

    private List<FormSummaryViewModel> studentForms(List<DepartmentTermForm> deptForms) {
        return deptForms.stream()
                        .map(DepartmentTermForm::getFormByStudentFormId)
                        .filter(Objects::nonNull)
                        .distinct()
                        .map(f -> new FormSummaryViewModel(f, FormType.STUDENT))
                        .collect(Collectors.toList());
    }

    private List<FormSummaryViewModel> employerForms(List<DepartmentTermForm> deptForms) {
        return deptForms.stream()
                        .map(DepartmentTermForm::getFormByEmployerFormId)
                        .filter(Objects::nonNull)
                        .distinct()
                        .map(f -> new FormSummaryViewModel(f, FormType.EMPLOYER))
                        .collect(Collectors.toList());
    }
}