fixed address for map

This commit is contained in:
SlimusMinus
2026-10-15 00:02:10 +03:00
parent 9eae82f6df
commit 01ac227bdb
7 changed files with 35 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ public class PostResponse {
private String category;
private String city;
private String district;
private String address;
// Координаты нужны фронту, чтобы кнопка «место на карте» открывала точку
// объявления, а не общий вид карты. Без этих полей /detail/{id} отдавал
// null, и MapView падал на дефолтный центр.

View File

@@ -27,6 +27,7 @@ public class AdminPostResponse {
private String category;
private String city;
private String district;
private String address;
private Double latitude;
private Double longitude;
private String phone;

View File

@@ -38,6 +38,9 @@ public class Post {
private PostCategory category;
private String city;
private String district;
// Полный адрес из формы; city/district — нормализованные данные геокодера
@Column(length = 500)
private String address;
private Double latitude;
private Double longitude;
private LocalDateTime createdAt;

View File

@@ -72,6 +72,8 @@ public class PostService {
post.setStatus(PostStatus.MODERATION);
post.setUser(currentUser);
post.setIsReward(request.getReward());
// Адрес из формы сохраняем как есть; геокодер заполняет только city/district
post.setAddress(normalizeAddress(request.getAddress()));
if (request.getLatitude() == null || request.getLongitude() == null) {
post.setCity(request.getAddress());
} else {
@@ -199,6 +201,7 @@ public class PostService {
post.setType(request.getType());
post.setIsReward(request.getReward());
post.setReward(request.getRewardText());
post.setAddress(normalizeAddress(request.getAddress()));
if (request.getLatitude() != null && request.getLongitude() != null) {
post.setLatitude(request.getLatitude());
@@ -410,4 +413,8 @@ public class PostService {
return toResponse(post, true);
}
}
private static String normalizeAddress(String address) {
return StringUtils.hasText(address) ? address.trim() : null;
}
}

View File

@@ -139,6 +139,9 @@ public class AdminPostService {
if (StringUtils.hasText(category)) {
post.setCategory(PostCategory.fromDisplayName(category));
}
if (StringUtils.hasText(address)) {
post.setAddress(address.trim());
}
if (latitude != null && longitude != null) {
post.setLatitude(latitude);
post.setLongitude(longitude);
@@ -303,6 +306,7 @@ public class AdminPostService {
.category(post.getCategory() == null ? null : post.getCategory().getDisplayName())
.city(post.getCity())
.district(post.getDistrict())
.address(post.getAddress())
.latitude(post.getLatitude())
.longitude(post.getLongitude())
.phone(post.getPhone())

View File

@@ -30,3 +30,4 @@ databaseChangeLog:
- include: { file: db/changelog/add/017-messages-chat-id.yaml }
- include: { file: db/changelog/alter/014-posts-rules-accepted-type.yaml }
- include: { file: db/changelog/alter/015-chats-unique-per-post.yaml }
- include: { file: db/changelog/add/018-add-address-posts.yaml }

View File

@@ -284,6 +284,23 @@ class AdminPostServiceTest {
assertThat(post.getCity()).isEqualTo("Тверь");
}
@Test
void updatePost_keepsClientAddressAndTakesCityFromGeocoder() {
Post post = post(1L, PostStatus.ACTIVE);
when(postRepository.findById(1L)).thenReturn(java.util.Optional.of(post));
when(postRepository.save(any(Post.class))).thenAnswer(inv -> inv.getArgument(0));
when(geocodingService.getAddress("55.0", "37.0"))
.thenReturn(new com.krylov.refound.dto.AddressInfo("Москва", "Тверской район"));
AdminPostResponse dto = service.updatePost(1L, "LOSS", "Кот", " Москва, Тверская улица, 7 ",
55.0, 37.0, null, null, null, false, null, List.of(), "[]");
assertThat(post.getAddress()).isEqualTo("Москва, Тверская улица, 7");
assertThat(post.getCity()).isEqualTo("Москва");
assertThat(post.getDistrict()).isEqualTo("Тверской район");
assertThat(dto.getAddress()).isEqualTo("Москва, Тверская улица, 7");
}
@Test
void updateStatus_closesPostAndIncrementsStatistics() {
Post post = post(1L, PostStatus.ACTIVE);