| [700e2f9] | 1 | package com.finki.icare.mapper;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.BlogDTO;
|
|---|
| 4 | import com.finki.icare.dto.CommentDTO;
|
|---|
| 5 | import com.finki.icare.model.Blog;
|
|---|
| 6 | import org.mapstruct.*;
|
|---|
| 7 |
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 |
|
|---|
| 11 | @Mapper(componentModel = MappingConstants.ComponentModel.SPRING, uses = CommentMapper.class)
|
|---|
| 12 | public interface BlogMapper {
|
|---|
| 13 |
|
|---|
| 14 | @Mapping(target = "idBlog", source = "blog.idBlog")
|
|---|
| 15 | @Mapping(target = "title", source = "blog.title")
|
|---|
| 16 | @Mapping(target = "content", expression = "java(includeFullData ? blog.getContent() : null)")
|
|---|
| 17 | @Mapping(target = "dateOfPost", source = "blog.dateOfPost")
|
|---|
| 18 | @Mapping(target = "patientId", source = "blog.patient.idUser")
|
|---|
| 19 | @Mapping(target = "patientUsername", source = "blog.patient.username")
|
|---|
| 20 | @Mapping(target = "patientName", expression = "java(blog.getPatient().getName() + \" \" + blog.getPatient().getSurname())")
|
|---|
| 21 | @Mapping(target = "likesCount", expression = "java(blog.getLikedBy() != null ? blog.getLikedBy().size() : 0)")
|
|---|
| 22 | @Mapping(target = "commentsCount", expression = "java(blog.getComments() != null ? blog.getComments().size() : 0)")
|
|---|
| 23 | @Mapping(target = "likedByCurrentUser", expression = "java(isLikedByCurrentUser(blog, currentUserId))")
|
|---|
| 24 | @Mapping(target = "comments", expression = "java(mapComments(blog, includeFullData, commentMapper))")
|
|---|
| 25 | BlogDTO toDTO(Blog blog, @Context Integer currentUserId, @Context Boolean includeFullData, @Context CommentMapper commentMapper);
|
|---|
| 26 |
|
|---|
| 27 | default boolean isLikedByCurrentUser(Blog blog, Integer currentUserId) {
|
|---|
| 28 | return currentUserId != null &&
|
|---|
| 29 | blog.getLikedBy() != null &&
|
|---|
| 30 | blog.getLikedBy()
|
|---|
| 31 | .stream()
|
|---|
| 32 | .anyMatch(patient -> patient.getIdUser().equals(currentUserId));
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | default List<CommentDTO> mapComments(Blog blog, boolean includeFullData, CommentMapper commentMapper) {
|
|---|
| 36 | if (!includeFullData || blog.getComments() == null) {
|
|---|
| 37 | return new ArrayList<>();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | return blog.getComments()
|
|---|
| 41 | .stream()
|
|---|
| 42 | .map(commentMapper::toDTO)
|
|---|
| 43 | .toList();
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|