package com.quinn.controller;

import com.quinn.service.SourceService;
import com.quinn.vo.SourceUpdateForm;
import com.quinn.vo.SourceWriteForm;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author limqsh
 * @since 2022-05-03
 */
@Controller
@PreAuthorize("hasAuthority('ADMIN')")
public class SourceUploadController {
    @Resource
    SourceService sourceService;

    @PostMapping("/source/oper")
    public synchronized String write(MultipartFile file, SourceWriteForm sourceWriteForm) throws IOException {
        sourceService.uploadNewSource(file,sourceWriteForm);
        // 重定向到列表页面
        return "redirect:/source";
    }

    // 编辑信息
    @PostMapping("/source/oper/update")
    public String toEdit(MultipartFile file, SourceUpdateForm sourceUpdateForm) throws IOException {
        sourceService.updateSource(file,sourceUpdateForm);
        // 重定向详情页面
        return "redirect:/source/view/" + sourceUpdateForm.getSid();
    }

    // 删除
    @GetMapping("/source/oper/del/{sid}")
    public String toDelete(@PathVariable("sid") String sid) {
        sourceService.deleteSource(sid);
        // 重定向到列表页面
        return "redirect:/source";
    }

}

