backend-demo-tn/src/main/java/com/cubetiqs/demo/rest/PostController.java
2020-06-17 20:22:26 +07:00

34 lines
1.1 KiB
Java

package com.cubetiqs.demo.rest;
import com.cubetiqs.demo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = {"/posts"})
public class PostController {
private final PostService postService;
@Autowired
public PostController(PostService postService) {
this.postService = postService;
}
@GetMapping
public ResponseEntity<Object> getAllPosts(
Pageable pageable,
@RequestParam(value = "view", defaultValue = "list") String viewType,
@RequestParam(value = "q", defaultValue = "") String q
) {
if ("list".equalsIgnoreCase(viewType)) {
return ResponseEntity.ok(postService.findAll());
}
return ResponseEntity.ok(postService.findAll(pageable));
}
}