Skip to content

Commit

Permalink
new: unreadMessageCount in List and updated to unreadMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladik-gif committed Oct 4, 2024
1 parent 6dcea3e commit ca713a3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@AllArgsConstructor
Expand All @@ -25,7 +26,7 @@ public class PrivateTopicInfoResponseDto {
@Schema(description = "Created time")
private LocalDateTime createdAt;

private long unreadMessageCount;
private List<MessageResponseDto> unreadMessages;

private LastMessageResponseDto lastMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.chat.yourway.dto.response.notification.LastMessageResponseDto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import lombok.AllArgsConstructor;
Expand All @@ -29,7 +30,7 @@ public class PublicTopicInfoResponseDto {
@Schema(description = "Created time")
private LocalDateTime createdAt;

private long unreadMessageCount;
private List<MessageResponseDto> unreadMessages;

private LastMessageResponseDto lastMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -42,7 +43,7 @@ public class TopicResponseDto {
@ArraySchema(schema = @Schema(description = "TopicSubscribers"))
private Set<ContactResponseDto> topicSubscribers;

private long unreadMessageCount;
private List<MessageResponseDto> unreadMessages;

private LastMessageResponseDto lastMessage;
}
15 changes: 7 additions & 8 deletions src/main/java/com/chat/yourway/mapper/BaseMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chat.yourway.mapper;

import com.chat.yourway.dto.response.ContactResponseDto;
import com.chat.yourway.dto.response.MessageResponseDto;
import com.chat.yourway.dto.response.notification.LastMessageResponseDto;
import com.chat.yourway.model.Contact;
import com.chat.yourway.model.Message;
Expand All @@ -14,20 +15,17 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Named("BaseMapper")
@Component
@RequiredArgsConstructor
public class BaseMapper {

private final ContactMapper contactMapper;
private final MessageMapper messageMapper;
private final LastMessagesService lastMessagesService;

@Named("isMyMessage")
public boolean isMyMessage(Message message, @Context Contact me) {
return message.getSender().equals(me);
}

@Named("getNamePrivateTopic")
public String getNamePrivateTopic(Topic topic, @Context Contact me) {
Optional<Contact> optionalContact = topic.getTopicSubscribers().stream()
Expand All @@ -46,12 +44,13 @@ public ContactResponseDto getContactPrivateTopic(Topic topic, @Context Contact m
return optionalContact.map(contactMapper::toResponseDto).orElse(null);
}

@Named("getUnreadMessageCount")
public long getUnreadMessageCount(Topic topic, @Context Contact contact) {
@Named("getUnreadMessages")
public List<MessageResponseDto> getUnreadMessages(Topic topic, @Context Contact contact) {
Set<Message> unreadMessages = contact.getUnreadMessages();
return unreadMessages.stream()
.filter(m -> m.getTopic().getId().equals(topic.getId()))
.count();
.map(messageMapper::toMessageResponseDto)
.collect(Collectors.toList());
}

@Named("getLastMessage")
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/chat/yourway/mapper/BaseMessageMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.chat.yourway.mapper;

import com.chat.yourway.model.Contact;
import com.chat.yourway.model.Message;
import lombok.RequiredArgsConstructor;
import org.mapstruct.Context;
import org.mapstruct.Named;
import org.springframework.stereotype.Component;

@Named("BaseMessageMapper")
@Component
@RequiredArgsConstructor
public class BaseMessageMapper {
@Named("isMyMessage")
public boolean isMyMessage(Message message, @Context Contact me) {
return message.getSender().equals(me);
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/chat/yourway/mapper/MessageMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring", uses = BaseMapper.class)
@Mapper(componentModel = "spring", uses = BaseMessageMapper.class)
public interface MessageMapper {
@Mapping(target = "topicId", source = "topic.id")
@Mapping(target = "my", source = ".", qualifiedByName = {"BaseMapper", "isMyMessage"})
@Mapping(target = "my", source = ".", qualifiedByName = {"BaseMessageMapper", "isMyMessage"})
MessageResponseDto toResponseDto(Message message, @Context Contact me);
@Mapping(target = "topicId", source = "topic.id")
@Mapping(target = "my", source = "content")
MessageResponseDto toMessageResponseDto(Message message);
}
6 changes: 3 additions & 3 deletions src/main/java/com/chat/yourway/mapper/TopicMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

@Mapper(componentModel = "spring", uses = BaseMapper.class)
public interface TopicMapper {
@Mapping(target = "unreadMessageCount", source = ".", qualifiedByName = {"BaseMapper", "getUnreadMessageCount"})
@Mapping(target = "unreadMessages", source = ".", qualifiedByName = {"BaseMapper", "getUnreadMessages"})
@Mapping(target = "lastMessage", source = ".", qualifiedByName = {"BaseMapper", "getLastMessage"})
TopicResponseDto toResponseDto(Topic topic, @Context Contact me);
@Mapping(target = "name", source = ".", qualifiedByName = {"BaseMapper", "getNamePrivateTopic"})
@Mapping(target = "contact", source = ".", qualifiedByName = {"BaseMapper", "getContactPrivateTopic"})
@Mapping(target = "unreadMessageCount", source = ".", qualifiedByName = {"BaseMapper", "getUnreadMessageCount"})
@Mapping(target = "unreadMessages", source = ".", qualifiedByName = {"BaseMapper", "getUnreadMessages"})
@Mapping(target = "lastMessage", source = ".", qualifiedByName = {"BaseMapper", "getLastMessage"})
PrivateTopicInfoResponseDto toInfoPrivateResponseDto(Topic topic, @Context Contact me);
@Mapping(target = "unreadMessageCount", source = ".", qualifiedByName = {"BaseMapper", "getUnreadMessageCount"})
@Mapping(target = "unreadMessages", source = ".", qualifiedByName = {"BaseMapper", "getUnreadMessages"})
@Mapping(target = "lastMessage", source = ".", qualifiedByName = {"BaseMapper", "getLastMessage"})
PublicTopicInfoResponseDto toInfoPublicResponseDto(Topic topic, @Context Contact me);
List<PublicTopicInfoResponseDto> toListInfoResponseDto(List<Topic> topics, @Context Contact me);
Expand Down

0 comments on commit ca713a3

Please sign in to comment.