Skip to content

Commit 01e92aa

Browse files
committed
refactor: 자기 자신에겐 지도에서 보이도록 수정
1 parent 3affe29 commit 01e92aa

2 files changed

Lines changed: 87 additions & 13 deletions

File tree

src/main/java/org/devkor/apu/saerok_server/domain/collection/core/repository/CollectionRepository.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ public List<UserBirdCollection> findNearby(Point ref, double radiusMeters, Long
5757
String sqlMineOnly = """
5858
SELECT c.*
5959
FROM user_bird_collection c
60-
LEFT JOIN bird b ON b.id = c.bird_id
6160
WHERE ST_DWithin(
6261
c.location::geography,
6362
CAST(:refPoint AS geography),
6463
:radius
6564
)
6665
AND c.user_id = :userId
67-
AND (c.bird_id IS NULL OR b.conservation_grade = 'NONE')
6866
ORDER BY ST_Distance(
6967
c.location::geography,
7068
CAST(:refPoint AS geography)
@@ -93,7 +91,11 @@ WHERE ST_DWithin(
9391
c.access_level = 'PUBLIC'
9492
OR (CAST(:userId AS bigint) IS NOT NULL AND c.user_id = :userId)
9593
)
96-
AND (c.bird_id IS NULL OR b.conservation_grade = 'NONE')
94+
AND (
95+
c.bird_id IS NULL
96+
OR b.conservation_grade = 'NONE'
97+
OR (CAST(:userId AS bigint) IS NOT NULL AND c.user_id = :userId)
98+
)
9799
ORDER BY ST_Distance(
98100
c.location::geography,
99101
CAST(:refPoint AS geography)
@@ -115,14 +117,12 @@ public long countNearbyCandidates(Point ref, double radiusMeters, Long userId, b
115117
String sql = """
116118
SELECT COUNT(*)
117119
FROM user_bird_collection c
118-
LEFT JOIN bird b ON b.id = c.bird_id
119120
WHERE ST_DWithin(
120121
c.location::geography,
121122
CAST(:refPoint AS geography),
122123
:radius
123124
)
124125
AND c.user_id = :userId
125-
AND (c.bird_id IS NULL OR b.conservation_grade = 'NONE')
126126
""";
127127

128128
var query = em.createNativeQuery(sql)
@@ -146,7 +146,11 @@ WHERE ST_DWithin(
146146
c.access_level = 'PUBLIC'
147147
OR (CAST(:userId AS bigint) IS NOT NULL AND c.user_id = :userId)
148148
)
149-
AND (c.bird_id IS NULL OR b.conservation_grade = 'NONE')
149+
AND (
150+
c.bird_id IS NULL
151+
OR b.conservation_grade = 'NONE'
152+
OR (CAST(:userId AS bigint) IS NOT NULL AND c.user_id = :userId)
153+
)
150154
""";
151155

152156
var query = em.createNativeQuery(sql)
@@ -177,14 +181,12 @@ WITH candidates AS (
177181
ST_Distance(c.location::geography, CAST(:refPoint AS geography)) AS dist,
178182
ST_SnapToGrid(ST_Transform(c.location, 3857), :gridSize, :gridSize) AS cell_id
179183
FROM user_bird_collection c
180-
LEFT JOIN bird b ON b.id = c.bird_id
181184
WHERE ST_DWithin(
182185
c.location::geography,
183186
CAST(:refPoint AS geography),
184187
:radius
185188
)
186189
AND c.user_id = :userId
187-
AND (c.bird_id IS NULL OR b.conservation_grade = 'NONE')
188190
), ranked AS (
189191
SELECT id,
190192
dist,
@@ -223,7 +225,11 @@ WHERE ST_DWithin(
223225
c.access_level = 'PUBLIC'
224226
OR (CAST(:userId AS bigint) IS NOT NULL AND c.user_id = :userId)
225227
)
226-
AND (c.bird_id IS NULL OR b.conservation_grade = 'NONE')
228+
AND (
229+
c.bird_id IS NULL
230+
OR b.conservation_grade = 'NONE'
231+
OR (CAST(:userId AS bigint) IS NOT NULL AND c.user_id = :userId)
232+
)
227233
), ranked AS (
228234
SELECT id,
229235
dist,

src/test/java/org/devkor/apu/saerok_server/domain/collection/core/repository/CollectionRepositoryTest.java

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ void findNearby_excludesProtectedBirdCollections() throws Exception {
210210
}
211211

212212
@Test
213-
@DisplayName("mineOnly 주변 조회도 보호등급 새 컬렉션을 결과에서 제외한다")
214-
void findNearby_mineOnly_excludesProtectedBirdCollections() throws Exception {
213+
@DisplayName("mineOnly 주변 조회는 내 보호등급 새 컬렉션도 결과에 포함한다")
214+
void findNearby_mineOnly_includesMyProtectedBirdCollections() throws Exception {
215215
// given
216216
User owner = newUser();
217217
Point ref = gf.createPoint(new Coordinate(126.9780, 37.5665));
@@ -235,11 +235,46 @@ void findNearby_mineOnly_excludesProtectedBirdCollections() throws Exception {
235235
List<UserBirdCollection> result = collectionRepository.findNearby(ref, 1_000, owner.getId(), true, null);
236236
long candidateCount = collectionRepository.countNearbyCandidates(ref, 1_000, owner.getId(), true);
237237

238+
// then
239+
assertEquals(2, result.size());
240+
assertEquals(2L, candidateCount);
241+
assertTrue(result.stream().anyMatch(c -> c.getId().equals(normalCollection.getId())));
242+
assertTrue(result.stream().anyMatch(c -> c.getId().equals(protectedCollection.getId())));
243+
}
244+
245+
@Test
246+
@DisplayName("로그인 주변 조회는 내 보호등급 새 컬렉션만 포함하고 타인의 보호등급 새 컬렉션은 제외한다")
247+
void findNearby_all_includesMyProtectedBirdCollectionsOnly() throws Exception {
248+
// given
249+
User me = newUser();
250+
User other = newUser();
251+
Point ref = gf.createPoint(new Coordinate(126.9780, 37.5665));
252+
Point near = gf.createPoint(new Coordinate(126.9781, 37.5664));
253+
254+
Bird myProtectedBird = new BirdBuilder(em)
255+
.korName("my-protected-nearby-" + System.nanoTime())
256+
.conservationGrade(ConservationGrade.GRADE_I)
257+
.build();
258+
Bird otherProtectedBird = new BirdBuilder(em)
259+
.korName("other-protected-nearby-" + System.nanoTime())
260+
.conservationGrade(ConservationGrade.GRADE_II)
261+
.build();
262+
263+
UserBirdCollection myProtectedCollection = newCollection(me, myProtectedBird, near, AccessLevelType.PRIVATE);
264+
UserBirdCollection otherProtectedCollection = newCollection(other, otherProtectedBird, near, AccessLevelType.PUBLIC);
265+
266+
em.flush();
267+
em.clear();
268+
269+
// when
270+
List<UserBirdCollection> result = collectionRepository.findNearby(ref, 1_000, me.getId(), false, null);
271+
long candidateCount = collectionRepository.countNearbyCandidates(ref, 1_000, me.getId(), false);
272+
238273
// then
239274
assertEquals(1, result.size());
240275
assertEquals(1L, candidateCount);
241-
assertTrue(result.stream().anyMatch(c -> c.getId().equals(normalCollection.getId())));
242-
assertFalse(result.stream().anyMatch(c -> c.getId().equals(protectedCollection.getId())));
276+
assertTrue(result.stream().anyMatch(c -> c.getId().equals(myProtectedCollection.getId())));
277+
assertFalse(result.stream().anyMatch(c -> c.getId().equals(otherProtectedCollection.getId())));
243278
}
244279

245280
@Test
@@ -317,4 +352,37 @@ void findNearbyEven_excludesProtectedBirdCollections() throws Exception {
317352
assertTrue(result.stream().anyMatch(c -> c.getId().equals(normalCollection.getId())));
318353
assertFalse(result.stream().anyMatch(c -> c.getId().equals(protectedCollection.getId())));
319354
}
355+
356+
@Test
357+
@DisplayName("findNearbyEven mineOnly는 내 보호등급 새 컬렉션도 결과에 포함한다")
358+
void findNearbyEven_mineOnly_includesMyProtectedBirdCollections() throws Exception {
359+
// given
360+
User owner = newUser();
361+
Point ref = gf.createPoint(new Coordinate(126.9780, 37.5665));
362+
Point near = gf.createPoint(new Coordinate(126.9781, 37.5664));
363+
364+
Bird protectedBird = new BirdBuilder(em)
365+
.korName("protected-even-mine-nearby-" + System.nanoTime())
366+
.conservationGrade(ConservationGrade.GRADE_I)
367+
.build();
368+
369+
UserBirdCollection protectedCollection = newCollection(owner, protectedBird, near, AccessLevelType.PRIVATE);
370+
371+
em.flush();
372+
em.clear();
373+
374+
// when
375+
List<UserBirdCollection> result = collectionRepository.findNearbyEven(
376+
ref,
377+
1_000,
378+
owner.getId(),
379+
true,
380+
10,
381+
80
382+
);
383+
384+
// then
385+
assertEquals(1, result.size());
386+
assertTrue(result.stream().anyMatch(c -> c.getId().equals(protectedCollection.getId())));
387+
}
320388
}

0 commit comments

Comments
 (0)