fixed controllers

This commit is contained in:
SlimusMinus
2026-07-31 01:08:53 +03:00
parent 74f7c88843
commit 2468c26b84
32 changed files with 536 additions and 432 deletions

View File

@@ -1,22 +1,17 @@
package com.krylov.refound.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.krylov.refound.dto.PostRequest;
import com.krylov.refound.dto.PostResponse;
import com.krylov.refound.dto.StatusUpdateRequest;
import com.krylov.refound.enums.PostType;
import com.krylov.refound.service.PostService;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
@@ -27,78 +22,53 @@ 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;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
@RequestMapping("/api/v1/posts")
@RequiredArgsConstructor
@Slf4j
public class PostController {
private final PostService service;
private final ObjectMapper objectMapper;
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public PostRequest create(@ModelAttribute PostRequest request, @RequestParam(value = "images", required = false) List<MultipartFile> images) {
log.info(request.toString());
log.info("create post {}", request.toString());
return service.create(request, images);
}
@GetMapping
public Page<PostResponse> getFeed(
@RequestParam(required = false) String search,
@PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
public Page<PostResponse> getFeed(@RequestParam(required = false) String search, @PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
log.info("get all feed");
return service.getFeed(search, pageable);
}
@GetMapping("/{id}")
public List<PostResponse> getById(@PathVariable Long id) {
log.info("get feed by id {}", id);
return service.getById(id);
}
@PutMapping(value = "/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public PostResponse update(
@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,
@RequestParam(value = "images", required = false) List<MultipartFile> images,
@RequestParam(value = "existingImages", required = false) 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;
try {
existingUrls = existingImagesJson != null ? Arrays.asList(
objectMapper.readValue(existingImagesJson, String[].class)) : List.of();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return service.update(id, request, images, existingUrls);
public PostResponse update(@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,
@RequestParam(value = "images", required = false) List<MultipartFile> images, @RequestParam(value = "existingImages", required = false) String existingImagesJson) {
log.info("update post {}", id);
return service.update(id, type, title, address, latitude, longitude, description, category, phone, images, existingImagesJson);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
log.info("delete feed by id {}", id);
service.delete(id);
}
@PatchMapping("/{id}/status")
public PostResponse updateStatus(@PathVariable Long id,
@RequestBody StatusUpdateRequest request) {
public PostResponse updateStatus(@PathVariable Long id, @RequestBody StatusUpdateRequest request) {
log.info("update feed {} on status {}", id, request.getStatus());
return service.updateStatus(id, request.getStatus());
}
}