87 lines
3.1 KiB
Java
87 lines
3.1 KiB
Java
package com.krylov.refound.controller;
|
|
|
|
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.List;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
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;
|
|
import org.springframework.web.bind.annotation.PatchMapping;
|
|
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;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/v1/posts")
|
|
@RequiredArgsConstructor
|
|
@Slf4j
|
|
public class PostController {
|
|
|
|
private final PostService service;
|
|
|
|
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
public PostRequest create(@ModelAttribute PostRequest request, @RequestPart(value = "image", required = false) MultipartFile image) {
|
|
log.info(request.toString());
|
|
return service.create(request, image);
|
|
}
|
|
|
|
@GetMapping
|
|
public List<PostResponse> getFeed() {
|
|
return service.getFeed();
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public List<PostResponse> getById(@PathVariable Long 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 = "image", required = false) MultipartFile image
|
|
) {
|
|
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);
|
|
|
|
return service.update(id, request, image);
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public void delete(@PathVariable Long id) {
|
|
service.delete(id);
|
|
}
|
|
|
|
@PatchMapping("/{id}/status")
|
|
public PostResponse updateStatus(@PathVariable Long id, @RequestBody StatusUpdateRequest request) {
|
|
return service.updateStatus(id, request.getStatus());
|
|
}
|
|
}
|