2020-06-17 17:54:21 +07:00
|
|
|
package com.cubetiqs.demo.rest;
|
|
|
|
|
|
|
|
import com.cubetiqs.demo.service.PostService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.data.domain.Pageable;
|
2020-06-17 20:22:26 +07:00
|
|
|
import org.springframework.http.ResponseEntity;
|
2020-06-17 17:54:21 +07:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2020-06-17 20:22:26 +07:00
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
2020-06-17 17:54:21 +07:00
|
|
|
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
|
2020-06-17 20:22:26 +07:00
|
|
|
public ResponseEntity<Object> getAllPosts(
|
|
|
|
Pageable pageable,
|
|
|
|
@RequestParam(value = "view", defaultValue = "list") String viewType,
|
|
|
|
@RequestParam(value = "q", defaultValue = "") String q
|
|
|
|
) {
|
|
|
|
if ("list".equalsIgnoreCase(viewType)) {
|
2020-06-18 19:47:20 +07:00
|
|
|
return ResponseEntity.ok(postService.findAllActives());
|
2020-06-17 20:22:26 +07:00
|
|
|
}
|
|
|
|
return ResponseEntity.ok(postService.findAll(pageable));
|
2020-06-17 17:54:21 +07:00
|
|
|
}
|
|
|
|
}
|