WebsiteProjectController.java 5.5 KB
package com.aigeo.website.controller;

import com.aigeo.website.entity.WebsiteProject;
import com.aigeo.website.service.WebsiteProjectService;
import com.aigeo.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 网站项目控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/website-projects")
@Tag(name = "网站项目管理", description = "网站项目相关接口")
public class WebsiteProjectController {
    
    @Autowired
    private WebsiteProjectService websiteProjectService;
    
    @PostMapping
    @Operation(summary = "创建网站项目", description = "创建新的网站项目")
    public Result<WebsiteProject> createProject(@RequestBody WebsiteProject project) {
        try {
            WebsiteProject savedProject = websiteProjectService.save(project);
            return Result.success("项目创建成功", savedProject);
        } catch (Exception e) {
            log.error("创建网站项目失败", e);
            return Result.error("项目创建失败");
        }
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取网站项目详情", description = "根据ID获取网站项目详情")
    public Result<WebsiteProject> getProjectById(@PathVariable Integer id) {
        try {
            return websiteProjectService.findById(id)
                    .map(project -> Result.success("查询成功", project))
                    .orElse(Result.error("项目不存在"));
        } catch (Exception e) {
            log.error("获取网站项目详情失败, id: {}", id, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping
    @Operation(summary = "获取网站项目列表", description = "获取所有网站项目列表")
    public Result<List<WebsiteProject>> getAllProjects() {
        try {
            List<WebsiteProject> projects = websiteProjectService.findAll();
            return Result.success("查询成功", projects);
        } catch (Exception e) {
            log.error("获取网站项目列表失败", e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/company/{companyId}")
    @Operation(summary = "根据公司ID获取网站项目列表", description = "根据公司ID获取网站项目列表")
    public Result<List<WebsiteProject>> getProjectsByCompanyId(@PathVariable Integer companyId) {
        try {
            List<WebsiteProject> projects = websiteProjectService.findByCompanyId(companyId);
            return Result.success("查询成功", projects);
        } catch (Exception e) {
            log.error("根据公司ID获取网站项目列表失败, companyId: {}", companyId, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID获取网站项目列表", description = "根据用户ID获取网站项目列表")
    public Result<List<WebsiteProject>> getProjectsByUserId(@PathVariable Integer userId) {
        try {
            List<WebsiteProject> projects = websiteProjectService.findByUserId(userId);
            return Result.success("查询成功", projects);
        } catch (Exception e) {
            log.error("根据用户ID获取网站项目列表失败, userId: {}", userId, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/status/{status}")
    @Operation(summary = "根据状态获取网站项目列表", description = "根据状态获取网站项目列表")
    public Result<List<WebsiteProject>> getProjectsByStatus(@PathVariable String status) {
        try {
            WebsiteProject.ProjectStatus projectStatus = WebsiteProject.ProjectStatus.fromCode(status);
            List<WebsiteProject> projects = websiteProjectService.findByStatus(projectStatus);
            return Result.success("查询成功", projects);
        } catch (Exception e) {
            log.error("根据状态获取网站项目列表失败, status: {}", status, e);
            return Result.error("查询失败");
        }
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新网站项目", description = "更新网站项目信息")
    public Result<WebsiteProject> updateProject(@PathVariable Integer id, @RequestBody WebsiteProject project) {
        try {
            if (!websiteProjectService.findById(id).isPresent()) {
                return Result.error("项目不存在");
            }
            project.setId(id);
            WebsiteProject updatedProject = websiteProjectService.save(project);
            return Result.success("项目更新成功", updatedProject);
        } catch (Exception e) {
            log.error("更新网站项目失败, id: {}", id, e);
            return Result.error("项目更新失败");
        }
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除网站项目", description = "删除指定ID的网站项目")
    public Result<String> deleteProject(@PathVariable Integer id) {
        try {
            if (!websiteProjectService.findById(id).isPresent()) {
                return Result.error("项目不存在");
            }
            websiteProjectService.deleteById(id);
            return Result.success("项目删除成功");
        } catch (Exception e) {
            log.error("删除网站项目失败, id: {}", id, e);
            return Result.error("项目删除失败");
        }
    }
}