package com.izouma.nineth.web;

import com.anji.captcha.util.StringUtils;
import com.izouma.nineth.domain.Project;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.service.ProjectService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目管理
 */
@RestController
@RequestMapping("/project")
@AllArgsConstructor
public class ProjectController extends BaseController{

    private ProjectService projectService;

    @PostMapping("/all")
    public Page<Project> all(@RequestBody PageQuery pageQuery) {

        return projectService.all(pageQuery);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Project create(@RequestBody Project project) {
        if(StringUtils.isBlank(project.getEnName())
                ||StringUtils.isBlank(project.getChName())
                || StringUtils.isBlank(project.getFrontAddr())){
            throw new BusinessException("参数缺失");
        }
        return projectService.save(project);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/delete")
    public void delete(@RequestBody Project project) {
        if(project.getId() == null){
            throw new BusinessException("参数缺失");
        }

        projectService.delete(project);
    }

}
