Merge pull request #5928 from amit2103/BAEL-10127

[BAEL-10127] - Added code for modelmapper article
This commit is contained in:
Loredana Crusoveanu 2018-12-16 17:27:19 +02:00 committed by GitHub
commit de3479ca78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 536 additions and 0 deletions

View File

@ -162,6 +162,12 @@
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>${modelmapper.version}</version>
</dependency>
</dependencies>
<build>
@ -259,6 +265,7 @@
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
<guava.version>18.0</guava.version>
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
<modelmapper.version>2.3.2</modelmapper.version>
</properties>
</project>

View File

@ -0,0 +1,20 @@
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();
}
}

View File

@ -0,0 +1,91 @@
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;
}
}

View File

@ -0,0 +1,71 @@
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;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.modelmapper.dto;
public class UserDto {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,98 @@
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;
}
}

View File

@ -0,0 +1,32 @@
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;
}
}

View File

@ -0,0 +1,44 @@
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;
}
}

View File

@ -0,0 +1,21 @@
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);
}
}

View File

@ -0,0 +1,17 @@
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);
}

View File

@ -0,0 +1,9 @@
package com.baeldung.modelmapper.service;
import com.baeldung.modelmapper.model.User;
public interface IUserService {
User getCurrentUser();
}

View File

@ -0,0 +1,47 @@
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);
}
}

View File

@ -0,0 +1,25 @@
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;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.modelmapper;
import static org.junit.Assert.assertEquals;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.junit.Test;
import org.modelmapper.ModelMapper;
import com.baeldung.modelmapper.dto.PostDto;
import com.baeldung.modelmapper.model.Post;
public class PostDtoUnitTest {
private ModelMapper modelMapper = new ModelMapper();
@Test
public void whenConvertPostEntityToPostDto_thenCorrect() {
Post post = new Post();
post.setId(Long.valueOf(1));
post.setTitle(randomAlphabetic(6));
post.setUrl("www.test.com");
PostDto postDto = modelMapper.map(post, PostDto.class);
assertEquals(post.getId(), postDto.getId());
assertEquals(post.getTitle(), postDto.getTitle());
assertEquals(post.getUrl(), postDto.getUrl());
}
@Test
public void whenConvertPostDtoToPostEntity_thenCorrect() {
PostDto postDto = new PostDto();
postDto.setId(Long.valueOf(1));
postDto.setTitle(randomAlphabetic(6));
postDto.setUrl("www.test.com");
Post post = modelMapper.map(postDto, Post.class);
assertEquals(postDto.getId(), post.getId());
assertEquals(postDto.getTitle(), post.getTitle());
assertEquals(postDto.getUrl(), post.getUrl());
}
}