fixed controllers
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.krylov.refound.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.krylov.refound.ai.facade.ContentModerationFacade;
|
||||
import com.krylov.refound.dto.PostRequest;
|
||||
import com.krylov.refound.dto.PostResponse;
|
||||
@@ -9,6 +11,7 @@ import com.krylov.refound.entity.User;
|
||||
import com.krylov.refound.enums.ErrorCode;
|
||||
import com.krylov.refound.enums.PostCategory;
|
||||
import com.krylov.refound.enums.PostStatus;
|
||||
import com.krylov.refound.enums.PostType;
|
||||
import com.krylov.refound.exception.ApiException;
|
||||
import com.krylov.refound.mapper.PostMapper;
|
||||
import com.krylov.refound.repository.FavoriteRepository;
|
||||
@@ -20,6 +23,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -52,6 +56,7 @@ public class PostService {
|
||||
private final FileStorageService fileStorageService;
|
||||
private final ContentModerationFacade contentModerationFacade;
|
||||
private final StatisticsService statisticsService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public PostRequest create(PostRequest request, List<MultipartFile> images) {
|
||||
|
||||
@@ -153,6 +158,35 @@ public class PostService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public PostResponse update(
|
||||
Long id,
|
||||
String type,
|
||||
String title,
|
||||
String address,
|
||||
Double latitude,
|
||||
Double longitude,
|
||||
String description,
|
||||
String category,
|
||||
String phone,
|
||||
List<MultipartFile> images,
|
||||
String existingImagesJson) {
|
||||
|
||||
PostRequest request = new PostRequest();
|
||||
request.setType(PostType.from(type));
|
||||
request.setTitle(title);
|
||||
request.setDescription(description);
|
||||
request.setCategory(category);
|
||||
request.setPhone(phone);
|
||||
request.setLatitude(latitude);
|
||||
request.setLongitude(longitude);
|
||||
request.setAddress(address);
|
||||
|
||||
List<String> existingUrls = parseExistingImages(existingImagesJson);
|
||||
|
||||
return update(id, request, images, existingUrls);
|
||||
}
|
||||
|
||||
// старый метод остаётся как есть — рефакторить будем позже
|
||||
public PostResponse update(Long id, PostRequest request, List<MultipartFile> images,
|
||||
List<String> existingUrls) {
|
||||
Post post = repository.findById(id)
|
||||
@@ -217,6 +251,21 @@ public class PostService {
|
||||
return mapper.toResponse(updated);
|
||||
}
|
||||
|
||||
private List<String> parseExistingImages(String existingImagesJson) {
|
||||
if (existingImagesJson == null) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return Arrays.asList(objectMapper.readValue(existingImagesJson, String[].class));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new ApiException(
|
||||
ErrorCode.VALIDATION_ERROR,
|
||||
"Некорректный формат existingImages",
|
||||
HttpStatus.BAD_REQUEST
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@CacheEvict(value = {"posts", "post"}, allEntries = true)
|
||||
public void delete(Long id) {
|
||||
Post post = repository.findById(id)
|
||||
|
||||
Reference in New Issue
Block a user