TopicController.java 2.7 KB
// keyword/controller/TopicController.java
package com.aigeo.keyword.controller;

import com.aigeo.keyword.entity.Topic;
import com.aigeo.keyword.service.TopicService;
import com.aigeo.common.enums.TopicStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/topics")
public class TopicController {
    
    @Autowired
    private TopicService topicService;
    
    @GetMapping
    public List<Topic> getAllTopics() {
        return topicService.getAllTopics();
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Topic> getTopicById(@PathVariable Integer id) {
        Optional<Topic> topic = topicService.getTopicById(id);
        return topic.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/company/{companyId}")
    public List<Topic> getTopicsByCompanyId(@PathVariable Integer companyId) {
        return topicService.getTopicsByCompanyId(companyId);
    }
    
    @GetMapping("/company/{companyId}/status/{status}")
    public List<Topic> getTopicsByCompanyIdAndStatus(@PathVariable Integer companyId, 
                                                     @PathVariable TopicStatus status) {
        return topicService.getTopicsByCompanyIdAndStatus(companyId, status);
    }
    
    @GetMapping("/source-task/{sourceTaskId}")
    public List<Topic> getTopicsBySourceTaskId(@PathVariable Integer sourceTaskId) {
        return topicService.getTopicsBySourceTaskId(sourceTaskId);
    }
    
    @PostMapping
    public Topic createTopic(@RequestBody Topic topic) {
        return topicService.saveTopic(topic);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Topic> updateTopic(@PathVariable Integer id, 
                                             @RequestBody Topic topicDetails) {
        Optional<Topic> topic = topicService.getTopicById(id);
        if (topic.isPresent()) {
            Topic updatedTopic = topic.get();
            updatedTopic.setTitle(topicDetails.getTitle());
            updatedTopic.setDescription(topicDetails.getDescription());
            updatedTopic.setSourceUrl(topicDetails.getSourceUrl());
            updatedTopic.setStatus(topicDetails.getStatus());
            
            Topic savedTopic = topicService.saveTopic(updatedTopic);
            return ResponseEntity.ok(savedTopic);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTopic(@PathVariable Integer id) {
        topicService.deleteTopic(id);
        return ResponseEntity.noContent().build();
    }
}