|
| 1 | +package com.baeldung.modelmapper.controller; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +import org.modelmapper.ModelMapper; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.stereotype.Controller; |
| 11 | +import org.springframework.web.bind.annotation.PathVariable; |
| 12 | +import org.springframework.web.bind.annotation.RequestBody; |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 15 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 16 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 17 | + |
| 18 | +import com.baeldung.modelmapper.dto.PostDto; |
| 19 | +import com.baeldung.modelmapper.model.Post; |
| 20 | +import com.baeldung.modelmapper.service.IPostService; |
| 21 | +import com.baeldung.modelmapper.service.IUserService; |
| 22 | + |
| 23 | +@Controller |
| 24 | +public class PostRestController { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private IPostService postService; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private IUserService userService; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private ModelMapper modelMapper; |
| 34 | + |
| 35 | + @RequestMapping(method = RequestMethod.GET) |
| 36 | + @ResponseBody |
| 37 | + public List<PostDto> getPosts( |
| 38 | + @PathVariable("page") int page, |
| 39 | + @PathVariable("size") int size, |
| 40 | + @PathVariable("sortDir") String sortDir, |
| 41 | + @PathVariable("sort") String sort) { |
| 42 | + |
| 43 | + List<Post> posts = postService.getPostsList(page, size, sortDir, sort); |
| 44 | + return posts.stream() |
| 45 | + .map(post -> convertToDto(post)) |
| 46 | + .collect(Collectors.toList()); |
| 47 | + } |
| 48 | + |
| 49 | + @RequestMapping(method = RequestMethod.POST) |
| 50 | + @ResponseStatus(HttpStatus.CREATED) |
| 51 | + @ResponseBody |
| 52 | + public PostDto createPost(@RequestBody PostDto postDto) throws ParseException { |
| 53 | + Post post = convertToEntity(postDto); |
| 54 | + Post postCreated = postService.createPost(post); |
| 55 | + return convertToDto(postCreated); |
| 56 | + } |
| 57 | + |
| 58 | + @RequestMapping(value = "/{id}", method = RequestMethod.GET) |
| 59 | + @ResponseBody |
| 60 | + public PostDto getPost(@PathVariable("id") Long id) { |
| 61 | + return convertToDto(postService.getPostById(id)); |
| 62 | + } |
| 63 | + |
| 64 | + @RequestMapping(value = "/{id}", method = RequestMethod.PUT) |
| 65 | + @ResponseStatus(HttpStatus.OK) |
| 66 | + public void updatePost(@RequestBody PostDto postDto) throws ParseException { |
| 67 | + Post post = convertToEntity(postDto); |
| 68 | + postService.updatePost(post); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private PostDto convertToDto(Post post) { |
| 73 | + PostDto postDto = modelMapper.map(post, PostDto.class); |
| 74 | + postDto.setSubmissionDate(post.getSubmissionDate(), |
| 75 | + userService.getCurrentUser().getPreference().getTimezone()); |
| 76 | + return postDto; |
| 77 | + } |
| 78 | + |
| 79 | + private Post convertToEntity(PostDto postDto) throws ParseException { |
| 80 | + Post post = modelMapper.map(postDto, Post.class); |
| 81 | + post.setSubmissionDate(postDto.getSubmissionDateConverted( |
| 82 | + userService.getCurrentUser().getPreference().getTimezone())); |
| 83 | + |
| 84 | + if (postDto.getId() != null) { |
| 85 | + Post oldPost = postService.getPostById(postDto.getId()); |
| 86 | + post.setRedditID(oldPost.getRedditID()); |
| 87 | + post.setSent(oldPost.isSent()); |
| 88 | + } |
| 89 | + return post; |
| 90 | + } |
| 91 | +} |
0 commit comments