forked from mtusi2026/mtusi2026lab7
37 lines
1.0 KiB
Java
37 lines
1.0 KiB
Java
package ru.mtusi.messenger.mapper;
|
|
|
|
import org.mapstruct.Mapper;
|
|
import org.mapstruct.Mapping;
|
|
import org.mapstruct.Named;
|
|
|
|
import ru.mtusi.messenger.dto.ChatDTO;
|
|
import ru.mtusi.messenger.dto.CreateChatDTO;
|
|
import ru.mtusi.messenger.model.Chat;
|
|
import ru.mtusi.messenger.model.User;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
|
|
@Mapper(componentModel = "spring")
|
|
public interface ChatMapper {
|
|
|
|
@Mapping(target = "id", ignore = true)
|
|
@Mapping(target = "createdAt", ignore = true)
|
|
@Mapping(target = "members", ignore = true)
|
|
@Mapping(target = "messages", ignore = true)
|
|
Chat toChat(CreateChatDTO dto);
|
|
|
|
@Mapping(target = "memberIds", source = "members", qualifiedByName = "usersToIds")
|
|
ChatDTO toChatDto(Chat chat);
|
|
|
|
@Named("usersToIds")
|
|
default List<UUID> usersToIds(Set<User> users) {
|
|
if (users == null) return new ArrayList<>();
|
|
List<UUID> ids = new ArrayList<>(users.size());
|
|
for (User u : users) ids.add(u.getId());
|
|
return ids;
|
|
}
|
|
}
|