[BAEL-14089] - Moved code for Entity to DTO article
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PostApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PostApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ModelMapper modelMapper() {
|
||||
return new ModelMapper();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package com.baeldung.modelmapper.controller;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.baeldung.modelmapper.dto.PostDto;
|
||||
import com.baeldung.modelmapper.model.Post;
|
||||
import com.baeldung.modelmapper.service.IPostService;
|
||||
import com.baeldung.modelmapper.service.IUserService;
|
||||
|
||||
@Controller
|
||||
public class PostRestController {
|
||||
|
||||
@Autowired
|
||||
private IPostService postService;
|
||||
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@Autowired
|
||||
private ModelMapper modelMapper;
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public List<PostDto> getPosts(
|
||||
@PathVariable("page") int page,
|
||||
@PathVariable("size") int size,
|
||||
@PathVariable("sortDir") String sortDir,
|
||||
@PathVariable("sort") String sort) {
|
||||
|
||||
List<Post> posts = postService.getPostsList(page, size, sortDir, sort);
|
||||
return posts.stream()
|
||||
.map(post -> convertToDto(post))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@ResponseBody
|
||||
public PostDto createPost(@RequestBody PostDto postDto) throws ParseException {
|
||||
Post post = convertToEntity(postDto);
|
||||
Post postCreated = postService.createPost(post);
|
||||
return convertToDto(postCreated);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public PostDto getPost(@PathVariable("id") Long id) {
|
||||
return convertToDto(postService.getPostById(id));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void updatePost(@RequestBody PostDto postDto) throws ParseException {
|
||||
Post post = convertToEntity(postDto);
|
||||
postService.updatePost(post);
|
||||
}
|
||||
|
||||
|
||||
private PostDto convertToDto(Post post) {
|
||||
PostDto postDto = modelMapper.map(post, PostDto.class);
|
||||
postDto.setSubmissionDate(post.getSubmissionDate(),
|
||||
userService.getCurrentUser().getPreference().getTimezone());
|
||||
return postDto;
|
||||
}
|
||||
|
||||
private Post convertToEntity(PostDto postDto) throws ParseException {
|
||||
Post post = modelMapper.map(postDto, Post.class);
|
||||
post.setSubmissionDate(postDto.getSubmissionDateConverted(
|
||||
userService.getCurrentUser().getPreference().getTimezone()));
|
||||
|
||||
if (postDto.getId() != null) {
|
||||
Post oldPost = postService.getPostById(postDto.getId());
|
||||
post.setRedditID(oldPost.getRedditID());
|
||||
post.setSent(oldPost.isSent());
|
||||
}
|
||||
return post;
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.baeldung.modelmapper.dto;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class PostDto {
|
||||
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String url;
|
||||
|
||||
private String date;
|
||||
|
||||
private UserDto user;
|
||||
|
||||
public Date getSubmissionDateConverted(String timezone) throws ParseException {
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
|
||||
return dateFormat.parse(this.date);
|
||||
}
|
||||
|
||||
public void setSubmissionDate(Date date, String timezone) {
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
|
||||
this.date = dateFormat.format(date);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public UserDto getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(UserDto user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.modelmapper.dto;
|
||||
|
||||
public class UserDto {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.baeldung.modelmapper.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Post {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String url;
|
||||
|
||||
private String date;
|
||||
|
||||
private String redditID;
|
||||
|
||||
private Date submissionDate;
|
||||
|
||||
private boolean sent;
|
||||
|
||||
private String userName;
|
||||
|
||||
public Post() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isSent() {
|
||||
return sent;
|
||||
}
|
||||
|
||||
public void setSent(boolean sent) {
|
||||
this.sent = sent;
|
||||
}
|
||||
|
||||
public String getRedditID() {
|
||||
return redditID;
|
||||
}
|
||||
|
||||
public void setRedditID(String redditID) {
|
||||
this.redditID = redditID;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Date getSubmissionDate() {
|
||||
return submissionDate;
|
||||
}
|
||||
|
||||
public void setSubmissionDate(Date submissionDate) {
|
||||
this.submissionDate = submissionDate;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.modelmapper.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Preference {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String timezone;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTimezone() {
|
||||
return timezone;
|
||||
}
|
||||
|
||||
public void setTimezone(String timezone) {
|
||||
this.timezone = timezone;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.modelmapper.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne
|
||||
Preference preference;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Preference getPreference() {
|
||||
return preference;
|
||||
}
|
||||
|
||||
public void setPreference(Preference preference) {
|
||||
this.preference = preference;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.modelmapper.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.modelmapper.model.Post;
|
||||
import com.baeldung.modelmapper.model.User;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, Long>, PagingAndSortingRepository<Post, Long> {
|
||||
|
||||
@Query("select u from Post u where u.userName=:userName")
|
||||
Page<Post> findByUser(@Param("userName") String userName, Pageable pageReq);
|
||||
|
||||
default Page<Post> findByUser(User user, Pageable pageReq) {
|
||||
return findByUser(user.getName(), pageReq);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.modelmapper.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.modelmapper.model.Post;
|
||||
|
||||
public interface IPostService {
|
||||
|
||||
List<Post> getPostsList(int page, int size, String sortDir, String sort);
|
||||
|
||||
void updatePost(Post post);
|
||||
|
||||
Post createPost(Post post);
|
||||
|
||||
Post getPostById(Long id);
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.modelmapper.service;
|
||||
|
||||
import com.baeldung.modelmapper.model.User;
|
||||
|
||||
public interface IUserService {
|
||||
|
||||
User getCurrentUser();
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.modelmapper.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.modelmapper.model.Post;
|
||||
import com.baeldung.modelmapper.repository.PostRepository;
|
||||
|
||||
@Service
|
||||
public class PostService implements IPostService {
|
||||
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@Override
|
||||
public List<Post> getPostsList(int page, int size, String sortDir, String sort) {
|
||||
|
||||
PageRequest pageReq
|
||||
= PageRequest.of(page, size, Sort.Direction.fromString(sortDir), sort);
|
||||
|
||||
Page<Post> posts = postRepository.findByUser(userService.getCurrentUser(), pageReq);
|
||||
return posts.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePost(Post post) {
|
||||
postRepository.save(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post createPost(Post post) {
|
||||
return postRepository.save(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long id) {
|
||||
return postRepository.getOne(id);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.modelmapper.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.modelmapper.model.Preference;
|
||||
import com.baeldung.modelmapper.model.User;
|
||||
|
||||
@Service
|
||||
public class UserService implements IUserService {
|
||||
|
||||
@Override
|
||||
public User getCurrentUser() {
|
||||
|
||||
Preference preference = new Preference();
|
||||
preference.setId(1L);
|
||||
preference.setTimezone("Asia/Calcutta");
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("Micheal");
|
||||
user.setPreference(preference);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user