feat: implement Q&A database persistence and Telegram bot integration
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.rit.portal.controller;
|
||||
|
||||
import com.rit.portal.entity.CommunityAnswer;
|
||||
import com.rit.portal.entity.CommunityQuestion;
|
||||
import com.rit.portal.repository.CommunityAnswerRepository;
|
||||
import com.rit.portal.repository.CommunityQuestionRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/questions")
|
||||
public class CommunityQuestionController {
|
||||
|
||||
@Autowired
|
||||
private CommunityQuestionRepository questionRepository;
|
||||
|
||||
@Autowired
|
||||
private CommunityAnswerRepository answerRepository;
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private static final String TELEGRAM_BOT_URL = "http://localhost:8082/send_question";
|
||||
|
||||
@GetMapping
|
||||
public List<CommunityQuestion> getAllQuestions() {
|
||||
return questionRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public CommunityQuestion createQuestion(@RequestBody CommunityQuestion question) {
|
||||
if (question.getUpvotes() == null) question.setUpvotes(0);
|
||||
if (question.getIsAnswered() == null) question.setIsAnswered(false);
|
||||
question.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
CommunityQuestion saved = questionRepository.save(question);
|
||||
|
||||
// Notify Telegram bot in a background thread to keep it robust and non-blocking
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Map<String, Object> payload = new HashMap<>();
|
||||
payload.put("question_id", saved.getId());
|
||||
payload.put("title", saved.getTitle());
|
||||
payload.put("body", saved.getBody());
|
||||
payload.put("author", saved.getAuthor());
|
||||
|
||||
restTemplate.postForEntity(TELEGRAM_BOT_URL, payload, String.class);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to notify Telegram Bot intermediary: " + e.getMessage());
|
||||
}
|
||||
}).start();
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/answers")
|
||||
public ResponseEntity<CommunityAnswer> addAnswer(@PathVariable Integer id, @RequestBody CommunityAnswer answer) {
|
||||
return questionRepository.findById(id).map(question -> {
|
||||
answer.setQuestion(question);
|
||||
if (answer.getUpvotes() == null) answer.setUpvotes(0);
|
||||
if (answer.getIsAccepted() == null) answer.setIsAccepted(false);
|
||||
answer.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
CommunityAnswer savedAnswer = answerRepository.save(answer);
|
||||
|
||||
question.setIsAnswered(true);
|
||||
questionRepository.save(question);
|
||||
|
||||
return ResponseEntity.ok(savedAnswer);
|
||||
}).orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/upvote")
|
||||
public ResponseEntity<CommunityQuestion> upvoteQuestion(@PathVariable Integer id) {
|
||||
return questionRepository.findById(id).map(question -> {
|
||||
question.setUpvotes(question.getUpvotes() + 1);
|
||||
return ResponseEntity.ok(questionRepository.save(question));
|
||||
}).orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/notes")
|
||||
@CrossOrigin(origins = "*") // CrossOrigin configured globally, but added here for safety
|
||||
public class NotePyqController {
|
||||
|
||||
@Autowired
|
||||
@@ -41,7 +40,7 @@ public class NotePyqController {
|
||||
|
||||
// Increment downloads count
|
||||
@PostMapping("/{id}/download")
|
||||
public ResponseEntity<Void> incrementDownloads(@PathVariable Long id) {
|
||||
public ResponseEntity<Void> incrementDownloads(@PathVariable Integer id) {
|
||||
return noteRepository.findById(id).map(note -> {
|
||||
note.setDownloadsCount(note.getDownloadsCount() + 1);
|
||||
note.setFileType(note.getFileType()); // Keep dirty check
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.rit.portal.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "community_answers")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class CommunityAnswer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "question_id", nullable = false)
|
||||
@JsonIgnore
|
||||
private CommunityQuestion question;
|
||||
|
||||
@Column(nullable = false, columnDefinition = "TEXT")
|
||||
private String body;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
@Column(name = "upvotes")
|
||||
private Integer upvotes = 0;
|
||||
|
||||
@Column(name = "is_accepted")
|
||||
private Boolean isAccepted = false;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.rit.portal.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "community_questions")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class CommunityQuestion {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false, columnDefinition = "TEXT")
|
||||
private String body;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
@Column(name = "upvotes")
|
||||
private Integer upvotes = 0;
|
||||
|
||||
@Column(name = "tags", columnDefinition = "text[]")
|
||||
private List<String> tags = new ArrayList<>();
|
||||
|
||||
@Column(name = "is_answered")
|
||||
private Boolean isAnswered = false;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
|
||||
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@Builder.Default
|
||||
private List<CommunityAnswer> answers = new ArrayList<>();
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class NotePyq {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private Integer id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.rit.portal.repository;
|
||||
|
||||
import com.rit.portal.entity.CommunityAnswer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CommunityAnswerRepository extends JpaRepository<CommunityAnswer, Integer> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.rit.portal.repository;
|
||||
|
||||
import com.rit.portal.entity.CommunityQuestion;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CommunityQuestionRepository extends JpaRepository<CommunityQuestion, Integer> {
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface NotePyqRepository extends JpaRepository<NotePyq, Long> {
|
||||
public interface NotePyqRepository extends JpaRepository<NotePyq, Integer> {
|
||||
List<NotePyq> findBySemester(Integer semester);
|
||||
List<NotePyq> findByDepartment(String department);
|
||||
List<NotePyq> findByFileType(String fileType);
|
||||
|
||||
Reference in New Issue
Block a user