Add json view to DTOs.

This commit is contained in:
2019-01-24 22:07:43 +01:00
parent 55940c5fe7
commit f4a2fea869
8 changed files with 85 additions and 33 deletions
@@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codiki.core.entities.dto.PostDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.entities.persistence.Post;
import org.codiki.core.entities.persistence.User;
import org.codiki.core.repositories.PostRepository;
@@ -28,6 +29,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
@RestController
@RequestMapping("/api/posts")
public class PostController {
@@ -51,7 +54,8 @@ public class PostController {
return StreamSupport.stream(postRepository.findAll().spliterator(), false)
.map(PostDTO::new).collect(Collectors.toList());
}
@JsonView(View.PostDTO.class)
@GetMapping("/{postKey}")
public PostDTO getByKey(@PathVariable("postKey") final String pPostKey,
final HttpServletResponse response) {
@@ -61,7 +65,8 @@ public class PostController {
}
return result;
}
@JsonView(View.PostDTO.class)
@GetMapping("/{postKey}/source")
public PostDTO getByKeyAndSource(@PathVariable("postKey")final String pPostKey,
final HttpServletResponse response) {
@@ -76,40 +81,45 @@ public class PostController {
return result;
}
@JsonView(View.PostDTO.class)
@GetMapping("/last")
public List<PostDTO> getLast() {
return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME)).stream()
.map(PostDTO::new).collect(Collectors.toList());
public List<Post> getLast() {
return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME));
}
@JsonView(View.PostDTO.class)
@GetMapping("/search/{searchCriteria}")
public List<PostDTO> search(@PathVariable("searchCriteria") final String pSearchCriteria) {
return postService.search(pSearchCriteria);
}
@JsonView(View.PostDTO.class)
@GetMapping("/byCategory/{categoryId}")
public List<PostDTO> getByCategory(@PathVariable("categoryId") final Long pCategoryId) {
return postRepository.getByCategoryId(pCategoryId).stream()
.map(PostDTO::new).collect(Collectors.toList());
}
@JsonView(View.PostDTO.class)
@GetMapping("/myPosts")
public List<PostDTO> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
public List<Post> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
final Principal pPrincipal) throws IOException {
List<PostDTO> result = new LinkedList<>();
List<Post> result = new LinkedList<>();
final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
if(connectedUser.isPresent()) {
result = postRepository.getByCreator(connectedUser.get().getId())
.stream().map(PostDTO::new).collect(Collectors.toList());
// result = postRepository.getByCreator(connectedUser.get().getId())
// .stream().map(PostDTO::new).collect(Collectors.toList());
result = postRepository.getByCreator(connectedUser.get().getId());
} else {
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
return result;
}
@JsonView(View.PostDTO.class)
@PostMapping("/preview")
public PostDTO preview(@RequestBody final PostDTO pPost) {
final PostDTO result = new PostDTO();
@@ -123,7 +133,8 @@ public class PostController {
return result;
}
@JsonView(View.PostDTO.class)
@PostMapping("/")
public PostDTO insert(@RequestBody final PostDTO pPost, final HttpServletRequest pRequest,
final HttpServletResponse pResponse, final Principal pPrincipal) {