TopicSourceController.java
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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();
}
}