-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentService.java
More file actions
61 lines (49 loc) · 2.04 KB
/
Copy pathCommentService.java
File metadata and controls
61 lines (49 loc) · 2.04 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
package demo.codeexample.comment.application;
import demo.codeexample.comment.CommentDto;
import demo.codeexample.comment.CommentLookup;
import demo.codeexample.comment.domain.CommentRepository;
import demo.codeexample.comment.CreateCommentDto;
import demo.codeexample.comment.domain.Comment;
import demo.codeexample.security.UserAuthHelper;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@AllArgsConstructor
public class CommentService implements CommentLookup {
private final CommentRepository commentRepository;
private final ModelMapper modelMapper;
private final UserAuthHelper userAuthHelper;
//Add
public CommentDto createComment(CreateCommentDto dto) {
Long writerId = userAuthHelper.getCurrentUserId();
String userName = userAuthHelper.getCurrentUserName();
Comment commentEntity = new Comment();
commentEntity.setContent(dto.getContent());
commentEntity.setTaskId(dto.getTaskId());
commentEntity.setUserId(writerId);
commentEntity.setUserName(userName);
commentRepository.save(commentEntity);
return modelMapper.map(commentEntity, CommentDto.class);
}
@Override // Implementing the method from your Facade
public List<CommentDto> getCommentsForTask(long taskId) {
return commentRepository.findAllByTaskIdOrderByCreatedAtDesc(taskId).stream()
.map(entity -> modelMapper.map(entity, CommentDto.class))
.toList();
}
@Override
public void deleteComment(long commentId) {
commentRepository.deleteById(commentId);
}
public List<CommentDto> getAllComments() {
List<Comment> commentEntities = commentRepository.findAll();
final List<CommentDto> commentDtos = new ArrayList<>();
for (Comment commentEntity : commentEntities) {
commentDtos.add(modelMapper.map(commentEntity, CommentDto.class));
}
return commentDtos;
}
}