TopicController.java 5.5 KB
package com.aigeo.keyword.controller;

import com.aigeo.keyword.entity.Topic;
import com.aigeo.keyword.service.TopicService;
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/topics")
@Tag(name = "话题管理", description = "话题相关接口")
public class TopicController {
    
    @Autowired
    private TopicService topicService;
    
    @PostMapping
    @Operation(summary = "创建话题", description = "创建新的话题")
    public Result<Topic> createTopic(@RequestBody Topic topic) {
        try {
            Topic savedTopic = topicService.save(topic);
            return Result.success("话题创建成功", savedTopic);
        } catch (Exception e) {
            log.error("创建话题失败", e);
            return Result.error("话题创建失败");
        }
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取话题详情", description = "根据ID获取话题详情")
    public Result<Topic> getTopicById(@PathVariable Integer id) {
        try {
            return topicService.findById(id)
                    .map(topic -> Result.success("查询成功", topic))
                    .orElse(Result.error("话题不存在"));
        } catch (Exception e) {
            log.error("获取话题详情失败, id: {}", id, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping
    @Operation(summary = "获取话题列表", description = "获取所有话题列表")
    public Result<List<Topic>> getAllTopics() {
        try {
            List<Topic> topics = topicService.findAll();
            return Result.success("查询成功", topics);
        } catch (Exception e) {
            log.error("获取话题列表失败", e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/company/{companyId}")
    @Operation(summary = "根据公司ID获取话题列表", description = "根据公司ID获取话题列表")
    public Result<List<Topic>> getTopicsByCompanyId(@PathVariable Integer companyId) {
        try {
            List<Topic> topics = topicService.findByCompanyId(companyId);
            return Result.success("查询成功", topics);
        } catch (Exception e) {
            log.error("根据公司ID获取话题列表失败, companyId: {}", companyId, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/task/{sourceTaskId}")
    @Operation(summary = "根据来源任务ID获取话题列表", description = "根据来源任务ID获取话题列表")
    public Result<List<Topic>> getTopicsBySourceTaskId(@PathVariable Integer sourceTaskId) {
        try {
            List<Topic> topics = topicService.findBySourceTaskId(sourceTaskId);
            return Result.success("查询成功", topics);
        } catch (Exception e) {
            log.error("根据来源任务ID获取话题列表失败, sourceTaskId: {}", sourceTaskId, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/search")
    @Operation(summary = "根据标题获取话题列表", description = "根据标题获取话题列表")
    public Result<List<Topic>> getTopicsByTitle(@RequestParam String title) {
        try {
            List<Topic> topics = topicService.findByTitle(title);
            return Result.success("查询成功", topics);
        } catch (Exception e) {
            log.error("根据标题获取话题列表失败, title: {}", title, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/status/{status}")
    @Operation(summary = "根据状态获取话题列表", description = "根据状态获取话题列表")
    public Result<List<Topic>> getTopicsByStatus(@PathVariable String status) {
        try {
            List<Topic> topics = topicService.findByStatus(status);
            return Result.success("查询成功", topics);
        } catch (Exception e) {
            log.error("根据状态获取话题列表失败, status: {}", status, e);
            return Result.error("查询失败");
        }
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新话题", description = "更新话题信息")
    public Result<Topic> updateTopic(@PathVariable Integer id, @RequestBody Topic topic) {
        try {
            if (!topicService.findById(id).isPresent()) {
                return Result.error("话题不存在");
            }
            topic.setId(id);
            Topic updatedTopic = topicService.save(topic);
            return Result.success("话题更新成功", updatedTopic);
        } catch (Exception e) {
            log.error("更新话题失败, id: {}", id, e);
            return Result.error("话题更新失败");
        }
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除话题", description = "删除指定ID的话题")
    public Result<String> deleteTopic(@PathVariable Integer id) {
        try {
            if (!topicService.findById(id).isPresent()) {
                return Result.error("话题不存在");
            }
            topicService.deleteById(id);
            return Result.success("话题删除成功");
        } catch (Exception e) {
            log.error("删除话题失败, id: {}", id, e);
            return Result.error("话题删除失败");
        }
    }
}