-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/comment logic #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
10f5f27
477790c
d4018a5
76af3a7
e23b27d
490a190
936442a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package org.example.visacasemanagementsystem.comment.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import org.example.visacasemanagementsystem.comment.dto.CommentDTO; | ||
| import org.example.visacasemanagementsystem.comment.dto.CreateCommentDTO; | ||
| import org.example.visacasemanagementsystem.comment.service.CommentService; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/comments") | ||
| public class CommentController { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| public CommentController(CommentService commentService) { | ||
| this.commentService = commentService; | ||
| } | ||
|
|
||
| // Create new comment | ||
| @PostMapping | ||
| public ResponseEntity<CommentDTO> createComment(@Valid @RequestBody CreateCommentDTO dto){ | ||
| CommentDTO createComment = commentService.createComment(dto); | ||
| return new ResponseEntity<>(createComment, HttpStatus.CREATED); | ||
| } | ||
|
|
||
| // Get all comments for a selected visa case | ||
| @GetMapping("/visa/{visaId}") | ||
| public ResponseEntity<List<CommentDTO>> getCommentsByVisa(@PathVariable Long visaId){ | ||
| List<CommentDTO> comments = commentService.getCommentsByVisaId(visaId); | ||
| return new ResponseEntity<>(comments, HttpStatus.OK); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package org.example.visacasemanagementsystem.comment.repository; | ||
|
|
||
| import org.example.visacasemanagementsystem.comment.entity.Comment; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
|
||
| List<Comment> findByVisaIdOrderByCreatedAtDesc(Long visaId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package org.example.visacasemanagementsystem.comment.service; | ||
|
|
||
| import org.example.visacasemanagementsystem.comment.dto.CommentDTO; | ||
| import org.example.visacasemanagementsystem.comment.dto.CreateCommentDTO; | ||
| import org.example.visacasemanagementsystem.comment.entity.Comment; | ||
| import org.example.visacasemanagementsystem.comment.mapper.CommentMapper; | ||
| import org.example.visacasemanagementsystem.comment.repository.CommentRepository; | ||
| import org.example.visacasemanagementsystem.exception.ResourceNotFoundException; | ||
| import org.example.visacasemanagementsystem.user.entity.User; | ||
| import org.example.visacasemanagementsystem.user.repository.UserRepository; | ||
| import org.example.visacasemanagementsystem.visa.entity.Visa; | ||
| import org.example.visacasemanagementsystem.visa.repository.VisaRepository; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class CommentService { | ||
|
|
||
| private final CommentRepository commentRepository; | ||
| private final CommentMapper commentMapper; | ||
| private final UserRepository userRepository; | ||
| private final VisaRepository visaRepository; | ||
|
|
||
| public CommentService(CommentRepository commentRepository, CommentMapper commentMapper , UserRepository userRepository, VisaRepository visaRepository) { | ||
| this.commentRepository = commentRepository; | ||
| this.commentMapper = commentMapper; | ||
| this.userRepository = userRepository; | ||
| this.visaRepository = visaRepository; | ||
| } | ||
|
|
||
|
|
||
| // Create Comment | ||
| // TODO: Security Risk - Replace authorId from DTO with authenticated user from | ||
| // SecurityContext once Spring Security is integrated to prevent IDOR vulnerabilities. | ||
| @Transactional | ||
| public CommentDTO createComment(CreateCommentDTO dto) { | ||
| if (dto == null) { | ||
| throw new IllegalArgumentException("Comment payload cannot be null"); | ||
| } | ||
|
|
||
| if (dto.text() == null || dto.text().isBlank()) { | ||
| throw new IllegalArgumentException("Comment text cannot be empty"); | ||
| } | ||
|
|
||
| // Get User and Visa from database | ||
| User author = userRepository.findById(dto.authorId()) | ||
| .orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + dto.authorId())); | ||
|
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not trust client-supplied Using 🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a valid point. This issue will be resolved later on when Spring Security is integrated in the project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'll note this for future reviews. ✏️ Learnings added
🧠 Learnings used |
||
|
|
||
| Visa visa = visaRepository.findById(dto.visaId()) | ||
| .orElseThrow(() -> new ResourceNotFoundException("Visa case not found with id: " + dto.visaId())); | ||
|
|
||
| // Map DTO to Entity | ||
| Comment comment = commentMapper.toEntity(dto); | ||
|
|
||
| // Set author and visaCase | ||
| comment.setAuthor(author); | ||
| comment.setVisa(visa); | ||
|
|
||
| // Save and return | ||
| Comment savedComment = commentRepository.save(comment); | ||
| return commentMapper.toDTO(savedComment); | ||
| } | ||
|
|
||
| // Get all comments for a selected visa case | ||
| @Transactional (readOnly = true) | ||
| public List<CommentDTO> getCommentsByVisaId(Long visaId) { | ||
| if (visaId == null || visaId <= 0) { | ||
| throw new IllegalArgumentException("Visa ID must be a positive number"); | ||
| } | ||
|
|
||
| List<Comment> comments = commentRepository.findByVisaIdOrderByCreatedAtDesc(visaId); | ||
| if(comments.isEmpty() && !visaRepository.existsById(visaId)) { | ||
| throw new ResourceNotFoundException("Visa case not found with id: " + visaId); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return comments.stream() | ||
| .map(commentMapper::toDTO) | ||
| .toList(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.example.visacasemanagementsystem.exception; | ||
|
|
||
| public class ResourceNotFoundException extends RuntimeException { | ||
| public ResourceNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.