Spaces:
Sleeping
Sleeping
| package com.krishna.controller; | |
| import com.krishna.dto.CommentDTO; | |
| import com.krishna.model.CommentModel; | |
| import com.krishna.service.CommentService; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.http.HttpStatus; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.web.bind.annotation.*; | |
| import java.util.List; | |
| public class CommentController { | |
| private CommentService commentService; | |
| public ResponseEntity<?> addComment( CommentDTO dto) { | |
| try { | |
| CommentModel comment = commentService.addComment(dto); | |
| return ResponseEntity.ok(comment); | |
| } catch (Exception e) { | |
| return ResponseEntity.badRequest().body("Error: " + e.getMessage()); | |
| } | |
| } | |
| public ResponseEntity<List<CommentModel>> getComments( Long postId) { | |
| return ResponseEntity.ok(commentService.getCommentsByPost(postId)); | |
| } | |
| // ✅ Corrected delete mapping for deleting a COMMENT | |
| public ResponseEntity<String> deleteComment( Long commentId) { | |
| try { | |
| commentService.deleteComment(commentId); | |
| return ResponseEntity.ok("Comment deleted successfully."); | |
| } catch (Exception e) { | |
| return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error: " + e.getMessage()); | |
| } | |
| } | |
| } | |