start backend
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.krylov.refound.repository;
|
||||
|
||||
import com.krylov.refound.entity.Favorite;
|
||||
import com.krylov.refound.entity.Post;
|
||||
import com.krylov.refound.entity.User;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
|
||||
|
||||
boolean existsByUserAndPost(User user, Post post);
|
||||
|
||||
List<Favorite> findByUser(User user);
|
||||
|
||||
void deleteByUserAndPost(User user, Post post);
|
||||
|
||||
@Query("""
|
||||
SELECT f.post.id FROM Favorite f
|
||||
WHERE f.user.id = :userId
|
||||
AND f.post.id IN :postIds
|
||||
""")
|
||||
List<Long> findFavoritePostIds(Long userId, List<Long> postIds);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.krylov.refound.repository;
|
||||
|
||||
import com.krylov.refound.entity.Image;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ImageRepository extends JpaRepository<Image, Long> {
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.krylov.refound.repository;
|
||||
|
||||
import com.krylov.refound.dto.DialogResponse;
|
||||
import com.krylov.refound.entity.Message;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface MessageRepository extends JpaRepository<Message, Long> {
|
||||
|
||||
@Query("""
|
||||
SELECT m FROM Message m
|
||||
WHERE (m.sender.id = :user1 AND m.receiver.id = :user2)
|
||||
OR (m.sender.id = :user2 AND m.receiver.id = :user1)
|
||||
ORDER BY m.createdAt
|
||||
""")
|
||||
List<Message> findChat(Long user1, Long user2);
|
||||
|
||||
@Query("""
|
||||
SELECT m FROM Message m
|
||||
WHERE m.id IN (
|
||||
SELECT MAX(m2.id)
|
||||
FROM Message m2
|
||||
WHERE m2.sender.id = :userId OR m2.receiver.id = :userId
|
||||
GROUP BY
|
||||
CASE
|
||||
WHEN m2.sender.id = :userId THEN m2.receiver.id
|
||||
ELSE m2.sender.id
|
||||
END
|
||||
)
|
||||
ORDER BY m.createdAt DESC
|
||||
""")
|
||||
List<Message> findDialogs(Long userId);
|
||||
|
||||
// 🔥 количество непрочитанных от пользователя
|
||||
@Query("""
|
||||
SELECT COUNT(m) FROM Message m
|
||||
WHERE m.receiver.id = :userId
|
||||
AND m.sender.id = :otherUserId
|
||||
AND m.isRead = false
|
||||
""")
|
||||
int countUnread(Long userId, Long otherUserId);
|
||||
|
||||
// 🔥 пометить как прочитанные
|
||||
@Modifying
|
||||
@Query("""
|
||||
UPDATE Message m
|
||||
SET m.isRead = true
|
||||
WHERE m.receiver.id = :userId
|
||||
AND m.sender.id = :otherUserId
|
||||
AND m.isRead = false
|
||||
""")
|
||||
void markAsRead(Long userId, Long otherUserId);
|
||||
|
||||
@Query("""
|
||||
SELECT new com.krylov.refound.dto.DialogResponse(
|
||||
CAST(
|
||||
CASE
|
||||
WHEN m.sender.id = :userId THEN m.receiver.id
|
||||
ELSE m.sender.id
|
||||
END AS long
|
||||
),
|
||||
CASE
|
||||
WHEN m.sender.id = :userId THEN m.receiver.email
|
||||
ELSE m.sender.email
|
||||
END,
|
||||
m.content,
|
||||
m.createdAt,
|
||||
SUM(CASE WHEN m.receiver.id = :userId AND m.isRead = false THEN 1 ELSE 0 END)
|
||||
)
|
||||
FROM Message m
|
||||
WHERE m.sender.id = :userId OR m.receiver.id = :userId
|
||||
GROUP BY
|
||||
CASE WHEN m.sender.id = :userId THEN m.receiver.id ELSE m.sender.id END,
|
||||
CASE WHEN m.sender.id = :userId THEN m.receiver.email ELSE m.sender.email END,
|
||||
m.content,
|
||||
m.createdAt
|
||||
""")
|
||||
List<DialogResponse> findDialogsWithUnread(Long userId);
|
||||
}
|
||||
@@ -1,4 +1,45 @@
|
||||
package com.krylov.refound.repository;
|
||||
|
||||
public interface PostRepository {
|
||||
}
|
||||
import com.krylov.refound.entity.Post;
|
||||
import com.krylov.refound.enums.PostType;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificationExecutor<Post> {
|
||||
|
||||
Page<Post> findByType(PostType type, Pageable pageable);
|
||||
|
||||
@Query(value = """
|
||||
SELECT * FROM Posts p
|
||||
WHERE
|
||||
(:type IS NULL OR p.type = :type)
|
||||
AND (:city IS NULL OR p.city = :city)
|
||||
AND (:category IS NULL OR p.category = :category)
|
||||
AND (
|
||||
:lat IS NULL OR :lng IS NULL OR :radius IS NULL OR
|
||||
(
|
||||
6371 * acos(
|
||||
cos(radians(:lat)) * cos(radians(p.latitude)) *
|
||||
cos(radians(p.longitude) - radians(:lng)) +
|
||||
sin(radians(:lat)) * sin(radians(p.latitude))
|
||||
)
|
||||
) <= :radius
|
||||
)
|
||||
""",
|
||||
countQuery = "SELECT count(*) FROM Posts p",
|
||||
nativeQuery = true)
|
||||
Page<Post> searchWithGeo(
|
||||
@Param("type") String type,
|
||||
@Param("city") String city,
|
||||
@Param("category") String category,
|
||||
@Param("lat") Double lat,
|
||||
@Param("lng") Double lng,
|
||||
@Param("radius") Double radius,
|
||||
Pageable pageable
|
||||
);
|
||||
|
||||
}
|
||||
@@ -1,4 +1,9 @@
|
||||
package com.krylov.refound.repository;
|
||||
|
||||
public interface UserRepository {
|
||||
import com.krylov.refound.entity.User;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
Optional<User> findByEmail(String email);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user