cz.muni.fi.editor.webapp.controllers.RequestController.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.webapp.controllers.RequestController.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.editor.webapp.controllers;

import cz.muni.fi.editor.api.dto.RequestDTO;
import cz.muni.fi.editor.api.exceptions.FieldException;
import cz.muni.fi.editor.api.RequestService;
import cz.muni.fi.editor.webapp.additions.ControllerConstants;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 24.8.2016.
 */
@Controller
@RequestMapping("/auth/requests")
@Log4j2
public class RequestController {
    @Autowired
    private RequestService requestService;

    @RequestMapping("/")
    public String list(Model model) {
        model.addAttribute("requests", requestService.getUnApproved());

        return "request.list";
    }

    @RequestMapping("/{request}/approve/")
    public String approve(@PathVariable Long request, RedirectAttributes redirectAttributes) {
        RequestDTO r = new RequestDTO();
        r.setId(request);

        try {
            requestService.approve(r);
        } catch (FieldException ex) {
            log.warn(ex);
            redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, ex.getMessage());
        }

        return "redirect:/auth/requests/";
    }
}