sakthivikram/geo-location
0
1package com.georeport.controller;2 3import com.georeport.dto.ApiResponse;4import com.georeport.dto.CommentResponse;5import com.georeport.dto.CreateCommentRequest;6import com.georeport.entity.User;7import com.georeport.repository.UserRepository;8import com.georeport.service.CommentService;9import jakarta.validation.Valid;10import org.springframework.beans.factory.annotation.Autowired;11import org.springframework.http.ResponseEntity;12import org.springframework.security.core.Authentication;13import org.springframework.web.bind.annotation.*;14 15import java.util.List;16 17/**18 * REST controller for managing comments on issues.19 */20@RestController21@RequestMapping("/api/issues/{issueId}/comments")22public class CommentController {23 24 @Autowired25 private CommentService commentService;26 27 @Autowired28 private UserRepository userRepository;29 30 /**31 * Add a new comment to an issue32 */33 @PostMapping34 public ResponseEntity<ApiResponse<CommentResponse>> addComment(35 @PathVariable Long issueId,36 @Valid @RequestBody CreateCommentRequest request,37 Authentication authentication) {38 39 User user = userRepository.findByEmail(authentication.getName())40 .orElseThrow(() -> new RuntimeException("User not found"));41 42 CommentResponse comment = commentService.addComment(issueId, request, user);43 44 return ResponseEntity.ok(ApiResponse.<CommentResponse>builder()45 .success(true)46 .message("Comment added successfully")47 .data(comment)48 .build());49 }50 51 /**52 * Get all comments for an issue (threaded)53 */54 @GetMapping55 public ResponseEntity<ApiResponse<List<CommentResponse>>> getComments(56 @PathVariable Long issueId) {57 58 List<CommentResponse> comments = commentService.getCommentsForIssue(issueId);59 60 return ResponseEntity.ok(ApiResponse.<List<CommentResponse>>builder()61 .success(true)62 .data(comments)63 .build());64 }65 66 /**67 * Get comment count for an issue68 */69 @GetMapping("/count")70 public ResponseEntity<ApiResponse<Long>> getCommentCount(@PathVariable Long issueId) {71 long count = commentService.getCommentCount(issueId);72 73 return ResponseEntity.ok(ApiResponse.<Long>builder()74 .success(true)75 .data(count)76 .build());77 }78}79 