File size: 1,523 Bytes
bb5169d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package com.krishna.service;

import com.krishna.dto.CommentDTO;
import com.krishna.model.CommentModel;
import com.krishna.model.PostModel;
import com.krishna.model.UserModel;
import com.krishna.repository.CommentRepository;
import com.krishna.repository.PostRepository;
import com.krishna.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PostRepository postRepository;

    public CommentModel addComment(CommentDTO dto) throws Exception {
        System.out.println("Received DTO: " + dto.getPostId() + ", " + dto.getUserId());

        UserModel user = userRepository.findById(dto.getUserId())
                .orElseThrow(() -> new Exception("User not found"));

        PostModel post = postRepository.findById(dto.getPostId())
                .orElseThrow(() -> new Exception("Post not found"));

        CommentModel comment = new CommentModel(dto.getContent(), user, post);
        return commentRepository.save(comment);
    }

    public List<CommentModel> getCommentsByPost(Long postId) {
        return commentRepository.findByPostId(postId);
    }

    public void deleteComment(Long commentId) {
        commentRepository.deleteById(commentId); // Or custom logic if needed
    }
}