TopicController.java
2.7 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
68
69
70
71
72
73
74
75
76
77
// 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();
}
}