added filter on title and description on post
This commit is contained in:
@@ -8,6 +8,7 @@ import com.krylov.refound.repository.ChatRepository;
|
||||
import com.krylov.refound.repository.MessageRepository;
|
||||
import com.krylov.refound.repository.UserRepository;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
@@ -64,7 +65,6 @@ public class ChatService {
|
||||
Chat chat = chatRepository.findById(chatId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Чат не найден"));
|
||||
|
||||
// Проверка что отправитель - участник чата
|
||||
if (!chat.getUserOneId().equals(senderId) && !chat.getUserTwoId().equals(senderId)) {
|
||||
throw new SecurityException("Вы не участник этого чата");
|
||||
}
|
||||
@@ -77,9 +77,21 @@ public class ChatService {
|
||||
|
||||
MessageDto dto = toMessageDto(message);
|
||||
|
||||
// Отправляем сообщение через WebSocket в топик конкретного чата
|
||||
messagingTemplate.convertAndSend("/topic/chat/" + chatId, dto);
|
||||
|
||||
// Уведомляем получателя об непрочитанных
|
||||
Long recipientId = chat.getUserOneId().equals(senderId)
|
||||
? chat.getUserTwoId()
|
||||
: chat.getUserOneId();
|
||||
|
||||
long unreadCount = messageRepository
|
||||
.countByChatIdAndIsReadFalseAndSenderIdNot(chatId, recipientId);
|
||||
|
||||
messagingTemplate.convertAndSend(
|
||||
"/topic/user/" + recipientId + "/unread",
|
||||
Map.of("chatId", chatId, "unreadCount", unreadCount)
|
||||
);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
@@ -122,4 +134,13 @@ public class ChatService {
|
||||
dto.setCreatedAt(m.getCreatedAt());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public void deleteChat(Long chatId) {
|
||||
chatRepository.deleteById(chatId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void markAsRead(Long chatId, Long userId) {
|
||||
messageRepository.markAsReadByChatIdAndRecipient(chatId, userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.krylov.refound.mapper.PostMapper;
|
||||
import com.krylov.refound.repository.FavoriteRepository;
|
||||
import com.krylov.refound.repository.ImageRepository;
|
||||
import com.krylov.refound.repository.PostRepository;
|
||||
import com.krylov.refound.util.PostSpecification;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -28,6 +29,9 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -83,22 +87,24 @@ public class PostService {
|
||||
return request;
|
||||
}
|
||||
|
||||
public List<PostResponse> getFeed() {
|
||||
List<Post> posts = repository.findAll();
|
||||
public Page<PostResponse> getFeed(String search, Pageable pageable) {
|
||||
Specification<Post> spec = PostSpecification.hasSearch(search);
|
||||
Page<Post> postsPage = repository.findAll(spec, pageable);
|
||||
|
||||
// Get all post IDs
|
||||
List<Long> postIds = posts.stream()
|
||||
List<Long> postIds = postsPage.getContent().stream()
|
||||
.map(Post::getId)
|
||||
.toList();
|
||||
|
||||
// Count likes for each post
|
||||
if (postIds.isEmpty()) {
|
||||
return Page.empty(pageable);
|
||||
}
|
||||
|
||||
Map<Long, Long> likeCounts = favoriteRepository.countLikesByPostIds(postIds).stream()
|
||||
.collect(Collectors.toMap(
|
||||
row -> (Long) row[0], // post ID
|
||||
row -> (Long) row[1] // like count
|
||||
row -> (Long) row[0],
|
||||
row -> (Long) row[1]
|
||||
));
|
||||
|
||||
// Get current user's favorites if authenticated
|
||||
Set<Long> userFavoriteIds = new HashSet<>();
|
||||
try {
|
||||
User currentUser = userService.getCurrentUser();
|
||||
@@ -109,18 +115,14 @@ public class PostService {
|
||||
// User not authenticated - that's OK
|
||||
}
|
||||
|
||||
// Set like counts and favorite status
|
||||
final Set<Long> finalUserFavoriteIds = userFavoriteIds;
|
||||
return posts.stream()
|
||||
.map(post -> {
|
||||
PostResponse response = mapper.toResponse(post);
|
||||
response.setLikeCount(likeCounts.getOrDefault(post.getId(), 0L));
|
||||
response.setFavorite(finalUserFavoriteIds.contains(post.getId()));
|
||||
response.setUserRole(post.getUser().getRole());
|
||||
return response;
|
||||
})
|
||||
.toList();
|
||||
|
||||
return postsPage.map(post -> {
|
||||
PostResponse response = mapper.toResponse(post);
|
||||
response.setLikeCount(likeCounts.getOrDefault(post.getId(), 0L));
|
||||
response.setFavorite(finalUserFavoriteIds.contains(post.getId()));
|
||||
response.setUserRole(post.getUser().getRole());
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
public List <PostResponse> getById(Long id) {
|
||||
|
||||
Reference in New Issue
Block a user