-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentService.java
More file actions
77 lines (65 loc) · 2.68 KB
/
Copy pathCommentService.java
File metadata and controls
77 lines (65 loc) · 2.68 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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.logger.LoggerLookup;
import demo.codeexample.project.TaskLookup;
import demo.codeexample.security.UserAuthHelper;
import demo.codeexample.shared.LoggerAction;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.security.access.AccessDeniedException;
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;
private final LoggerLookup logger;
//Add
public CommentDto createComment(CreateCommentDto dto) {
Long writerId = userAuthHelper.getCurrentUserId();
String userName = userAuthHelper.getCurrentUserName();
if (writerId == null) {
throw new AccessDeniedException("Authentication Required");
}
Comment commentEntity = new Comment();
commentEntity.setContent(dto.getContent());
commentEntity.setTaskId(dto.getTaskId());
commentEntity.setUserId(writerId);
commentEntity.setUserName(userName);
Comment savedComment =commentRepository.save(commentEntity);
logger.log(
LoggerAction.COMMENT_ADDED,
writerId,
"TASK",
savedComment.getId(),
dto.getTaskId(),
"User " + userName + " added a comment to task " + dto.getTaskId()
);
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;
}
}