TopicSourceController.java 2.6 KB
// keyword/controller/TopicSourceController.java
package com.aigeo.keyword.controller;

import com.aigeo.keyword.entity.TopicSource;
import com.aigeo.keyword.service.TopicSourceService;
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/topic-sources")
public class TopicSourceController {
    
    @Autowired
    private TopicSourceService topicSourceService;
    
    @GetMapping
    public List<TopicSource> getAllTopicSources() {
        return topicSourceService.getAllTopicSources();
    }
    
    @GetMapping("/active")
    public List<TopicSource> getActiveTopicSources() {
        return topicSourceService.getActiveTopicSources();
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<TopicSource> getTopicSourceById(@PathVariable Integer id) {
        Optional<TopicSource> topicSource = topicSourceService.getTopicSourceById(id);
        return topicSource.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PostMapping
    public TopicSource createTopicSource(@RequestBody TopicSource topicSource) {
        return topicSourceService.saveTopicSource(topicSource);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<TopicSource> updateTopicSource(@PathVariable Integer id, 
                                                         @RequestBody TopicSource topicSourceDetails) {
        Optional<TopicSource> topicSource = topicSourceService.getTopicSourceById(id);
        if (topicSource.isPresent()) {
            TopicSource updatedTopicSource = topicSource.get();
            updatedTopicSource.setName(topicSourceDetails.getName());
            updatedTopicSource.setEngine(topicSourceDetails.getEngine());
            updatedTopicSource.setQueryPattern(topicSourceDetails.getQueryPattern());
            updatedTopicSource.setResultField(topicSourceDetails.getResultField());
            updatedTopicSource.setMaxResults(topicSourceDetails.getMaxResults());
            updatedTopicSource.setIsActive(topicSourceDetails.getIsActive());
            
            TopicSource savedTopicSource = topicSourceService.saveTopicSource(updatedTopicSource);
            return ResponseEntity.ok(savedTopicSource);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTopicSource(@PathVariable Integer id) {
        topicSourceService.deleteTopicSource(id);
        return ResponseEntity.noContent().build();
    }
}