Merge pull request #7 from SlimusMinus/add-yandex_card
added card on post and feed
This commit is contained in:
@@ -2,7 +2,6 @@ package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.PostRequest;
|
||||
import com.krylov.refound.dto.PostResponse;
|
||||
import com.krylov.refound.entity.Post;
|
||||
import com.krylov.refound.enums.PostType;
|
||||
import com.krylov.refound.service.PostService;
|
||||
import java.util.List;
|
||||
@@ -15,7 +14,6 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
@@ -49,6 +47,9 @@ public class PostController {
|
||||
@PathVariable Long id,
|
||||
@RequestParam String type,
|
||||
@RequestParam String title,
|
||||
@RequestParam(required = false) String address,
|
||||
@RequestParam(required = false) Double latitude,
|
||||
@RequestParam(required = false) Double longitude,
|
||||
@RequestParam(required = false) String description,
|
||||
@RequestParam(required = false) String category,
|
||||
@RequestParam(required = false) String phone,
|
||||
@@ -60,6 +61,9 @@ public class PostController {
|
||||
request.setDescription(description);
|
||||
request.setCategory(category);
|
||||
request.setPhone(phone);
|
||||
request.setLatitude(latitude);
|
||||
request.setLongitude(longitude);
|
||||
request.setAddress(address);
|
||||
|
||||
return service.update(id, request, image);
|
||||
}
|
||||
|
||||
@@ -7,18 +7,15 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PostRequest {
|
||||
|
||||
@NotNull
|
||||
private PostType type;
|
||||
|
||||
@NotBlank
|
||||
private String title;
|
||||
|
||||
private String description;
|
||||
|
||||
private String category;
|
||||
|
||||
private String phone;
|
||||
|
||||
private String imageUrl;
|
||||
private Double latitude;
|
||||
private Double longitude;
|
||||
private String address;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.krylov.refound.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class NominatimService {
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public String getAddress(String lat, String lon) {
|
||||
String url = "https://nominatim.openstreetmap.org/reverse?lat=" + lat + "&lon=" + lon + "&format=json";
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("User-Agent", "MyApp/1.0")
|
||||
.build();
|
||||
JsonNode root;
|
||||
try{
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
root = mapper.readTree(response.body());
|
||||
} catch (InterruptedException | IOException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return root.path("display_name").asText();
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ public class PostService {
|
||||
private final PostRepository repository;
|
||||
private final PostMapper mapper;
|
||||
private final ImageRepository imageRepository;
|
||||
private final NominatimService nominatimService;
|
||||
|
||||
@CacheEvict(value = "posts", allEntries = true)
|
||||
public PostRequest create(PostRequest request, MultipartFile image) {
|
||||
@@ -95,6 +96,14 @@ public class PostService {
|
||||
post.setCategory(request.getCategory());
|
||||
post.setPhone(request.getPhone());
|
||||
post.setType(request.getType());
|
||||
if (request.getLatitude() != null && request.getLongitude() != null){
|
||||
post.setLatitude(request.getLatitude());
|
||||
post.setLongitude(request.getLongitude());
|
||||
String address = nominatimService.getAddress(request.getLatitude().toString(), request.getLongitude().toString());
|
||||
post.setCity(address);
|
||||
} else {
|
||||
post.setCity(request.getAddress());
|
||||
}
|
||||
|
||||
if (image != null && !image.isEmpty()) {
|
||||
// удаляем старые картинки с диска
|
||||
|
||||
Reference in New Issue
Block a user