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.PostRequest;
|
||||||
import com.krylov.refound.dto.PostResponse;
|
import com.krylov.refound.dto.PostResponse;
|
||||||
import com.krylov.refound.entity.Post;
|
|
||||||
import com.krylov.refound.enums.PostType;
|
import com.krylov.refound.enums.PostType;
|
||||||
import com.krylov.refound.service.PostService;
|
import com.krylov.refound.service.PostService;
|
||||||
import java.util.List;
|
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.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.PutMapping;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
@@ -49,6 +47,9 @@ public class PostController {
|
|||||||
@PathVariable Long id,
|
@PathVariable Long id,
|
||||||
@RequestParam String type,
|
@RequestParam String type,
|
||||||
@RequestParam String title,
|
@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 description,
|
||||||
@RequestParam(required = false) String category,
|
@RequestParam(required = false) String category,
|
||||||
@RequestParam(required = false) String phone,
|
@RequestParam(required = false) String phone,
|
||||||
@@ -60,6 +61,9 @@ public class PostController {
|
|||||||
request.setDescription(description);
|
request.setDescription(description);
|
||||||
request.setCategory(category);
|
request.setCategory(category);
|
||||||
request.setPhone(phone);
|
request.setPhone(phone);
|
||||||
|
request.setLatitude(latitude);
|
||||||
|
request.setLongitude(longitude);
|
||||||
|
request.setAddress(address);
|
||||||
|
|
||||||
return service.update(id, request, image);
|
return service.update(id, request, image);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,18 +7,15 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class PostRequest {
|
public class PostRequest {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private PostType type;
|
private PostType type;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
private String imageUrl;
|
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 PostRepository repository;
|
||||||
private final PostMapper mapper;
|
private final PostMapper mapper;
|
||||||
private final ImageRepository imageRepository;
|
private final ImageRepository imageRepository;
|
||||||
|
private final NominatimService nominatimService;
|
||||||
|
|
||||||
@CacheEvict(value = "posts", allEntries = true)
|
@CacheEvict(value = "posts", allEntries = true)
|
||||||
public PostRequest create(PostRequest request, MultipartFile image) {
|
public PostRequest create(PostRequest request, MultipartFile image) {
|
||||||
@@ -95,6 +96,14 @@ public class PostService {
|
|||||||
post.setCategory(request.getCategory());
|
post.setCategory(request.getCategory());
|
||||||
post.setPhone(request.getPhone());
|
post.setPhone(request.getPhone());
|
||||||
post.setType(request.getType());
|
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()) {
|
if (image != null && !image.isEmpty()) {
|
||||||
// удаляем старые картинки с диска
|
// удаляем старые картинки с диска
|
||||||
|
|||||||
Reference in New Issue
Block a user