Skip to content

Commit ebc302e

Browse files
authored
Merge pull request #45 from ithsjava25/Task-Comment-relation
Task comment relation
2 parents 9c754bc + ba521bb commit ebc302e

7 files changed

Lines changed: 87 additions & 9 deletions

File tree

src/main/java/demo/codeexample/comment/application/CommentService.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package demo.codeexample.comment.application;
22
import demo.codeexample.comment.CommentDto;
3+
import demo.codeexample.comment.CommentLookup;
34
import demo.codeexample.comment.domain.CommentRepository;
45
import demo.codeexample.comment.CreateCommentDto;
56
import demo.codeexample.comment.domain.Comment;
@@ -12,10 +13,9 @@
1213

1314
@Service
1415
@AllArgsConstructor
15-
public class CommentService {
16+
public class CommentService implements CommentLookup {
1617

1718
private final CommentRepository commentRepository;
18-
//private final TaskRepository taskRepository;
1919
private final ModelMapper modelMapper;
2020
//private final "Some kind of Authorization service" with saved user
2121

@@ -31,6 +31,19 @@ public CommentDto createComment(CreateCommentDto createCommentDto) {
3131
return modelMapper.map(commentEntity, CommentDto.class);
3232
}
3333

34+
@Override // Implementing the method from your Facade
35+
public List<CommentDto> getCommentsForTask(long taskId) {
36+
// Use a repository method to find comments by Task ID
37+
return commentRepository.findAllByTaskIdOrderByCreatedAtDesc(taskId).stream()
38+
.map(entity -> modelMapper.map(entity, CommentDto.class))
39+
.toList();
40+
}
41+
42+
@Override // Implementing the method from your Facade
43+
public void deleteComment(long commentId) {
44+
commentRepository.deleteById(commentId);
45+
}
46+
3447
public List<CommentDto> getAllComments() {
3548
List<Comment> commentEntities = commentRepository.findAll();
3649
final List<CommentDto> commentDtos = new ArrayList<>();

src/main/java/demo/codeexample/comment/domain/Comment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class Comment {
2222
@Column(nullable = false, length = 300)
2323
private String content;
2424

25+
@Column
26+
private Long taskId;
27+
2528
// @Column(nullable = false)
2629
// private Long writerId;
2730

src/main/java/demo/codeexample/comment/domain/CommentRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
import org.springframework.data.jpa.repository.JpaRepository;
44

5+
import java.util.List;
6+
57
public interface CommentRepository extends JpaRepository<Comment, Long> {
8+
List<Comment> findAllByTaskIdOrderByCreatedAtDesc(Long taskId);
9+
610
}

src/main/java/demo/codeexample/config/DataInitializer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package demo.codeexample.config;
22

3-
import demo.codeexample.user.CreateUserDto;
43
import demo.codeexample.user.CreateUserDto;
54
import demo.codeexample.shared.Role;
65
import demo.codeexample.user.UserLookup;

src/main/java/demo/codeexample/logger/application/LoggerService.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55
import demo.codeexample.logger.domain.LoggerEntity;
66
import demo.codeexample.logger.domain.LoggerRepository;
77
import lombok.RequiredArgsConstructor;
8+
import org.springframework.context.annotation.Primary;
89
import org.springframework.stereotype.Service;
910

1011
@Service
1112
@RequiredArgsConstructor
12-
public class LoggerService implements LoggerLookup {
13+
public class LoggerService implements LoggerLookup{
1314

1415
private final LoggerRepository loggerRepository;
1516

1617
@Override
1718
public void log(LoggerAction action, Long userId, String entityType, Long entityId) {
1819
LoggerEntity log = new LoggerEntity();
1920
log.setAction(action);
20-
log.setUserId(userId);
21-
log.setEntityType(entityType);
22-
log.setEntityId(entityId);
23-
24-
loggerRepository.save(log);
2521
}
2622

2723
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package demo.codeexample.task;
2+
3+
import demo.codeexample.comment.CommentDto;
4+
import demo.codeexample.task.domain.Task;
5+
import demo.codeexample.task.domain.TaskStatus;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.time.LocalDateTime;
11+
import java.util.List;
12+
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
@Data
16+
public class TaskResponseDto {
17+
private Long taskId;
18+
private String title;
19+
private String description;
20+
private TaskStatus status;
21+
private LocalDateTime deadline;
22+
private Long projectId;
23+
private Long userId;
24+
25+
private List<CommentDto> comments;
26+
27+
}
28+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
11
package demo.codeexample.task.infrastructure.adapters.in;
2+
import demo.codeexample.comment.CommentLookup;
3+
import demo.codeexample.task.TaskResponseDto;
4+
import demo.codeexample.task.application.ports.in.TaskUseCase;
5+
import demo.codeexample.task.domain.Task;
6+
import lombok.RequiredArgsConstructor;
7+
import org.modelmapper.ModelMapper;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.ui.Model;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.server.ResponseStatusException;
215

16+
17+
18+
@Controller
19+
@RequestMapping("/tasks")
20+
@RequiredArgsConstructor
321
public class TaskController {
22+
private final TaskUseCase taskUseCase;
23+
private final CommentLookup commentLookup;
24+
private final ModelMapper modelMapper;
25+
26+
27+
@GetMapping("/{taskId}/view")
28+
public String viewTask(@PathVariable Long taskId, Model model) {
29+
Task task = taskUseCase.findById(taskId)
30+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
31+
32+
TaskResponseDto dto = modelMapper.map(task, TaskResponseDto.class);
33+
34+
dto.setComments(commentLookup.getCommentsForTask(taskId));
35+
36+
model.addAttribute("task", dto);
37+
return "task-detail";
38+
}
439
}

0 commit comments

Comments
 (0)