Skip to content

Commit d9ac59c

Browse files
committed
[BAEL-10127] - Added code for modelmapper article
1 parent 0f443d3 commit d9ac59c

14 files changed

Lines changed: 536 additions & 0 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@
162162
<groupId>javax.validation</groupId>
163163
<artifactId>validation-api</artifactId>
164164
</dependency>
165+
166+
<dependency>
167+
<groupId>org.modelmapper</groupId>
168+
<artifactId>modelmapper</artifactId>
169+
<version>${modelmapper.version}</version>
170+
</dependency>
165171
</dependencies>
166172

167173
<build>
@@ -259,6 +265,7 @@
259265
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
260266
<guava.version>18.0</guava.version>
261267
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
268+
<modelmapper.version>2.3.2</modelmapper.version>
262269
</properties>
263270

264271
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.modelmapper;
2+
3+
import org.modelmapper.ModelMapper;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
8+
@SpringBootApplication
9+
public class PostApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(PostApplication.class, args);
13+
}
14+
15+
@Bean
16+
public ModelMapper modelMapper() {
17+
return new ModelMapper();
18+
}
19+
20+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.modelmapper.dto;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
import java.util.TimeZone;
7+
8+
public class PostDto {
9+
10+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
11+
12+
private Long id;
13+
14+
private String title;
15+
16+
private String url;
17+
18+
private String date;
19+
20+
private UserDto user;
21+
22+
public Date getSubmissionDateConverted(String timezone) throws ParseException {
23+
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
24+
return dateFormat.parse(this.date);
25+
}
26+
27+
public void setSubmissionDate(Date date, String timezone) {
28+
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
29+
this.date = dateFormat.format(date);
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getTitle() {
41+
return title;
42+
}
43+
44+
public void setTitle(String title) {
45+
this.title = title;
46+
}
47+
48+
public String getUrl() {
49+
return url;
50+
}
51+
52+
public void setUrl(String url) {
53+
this.url = url;
54+
}
55+
56+
public String getDate() {
57+
return date;
58+
}
59+
60+
public void setDate(String date) {
61+
this.date = date;
62+
}
63+
64+
public UserDto getUser() {
65+
return user;
66+
}
67+
68+
public void setUser(UserDto user) {
69+
this.user = user;
70+
}
71+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.modelmapper.dto;
2+
3+
public class UserDto {
4+
5+
private String name;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.modelmapper.model;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
public class Post {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.AUTO)
15+
private Long id;
16+
17+
private String title;
18+
19+
private String url;
20+
21+
private String date;
22+
23+
private String redditID;
24+
25+
private Date submissionDate;
26+
27+
private boolean sent;
28+
29+
private String userName;
30+
31+
public Post() {
32+
33+
}
34+
35+
public boolean isSent() {
36+
return sent;
37+
}
38+
39+
public void setSent(boolean sent) {
40+
this.sent = sent;
41+
}
42+
43+
public String getRedditID() {
44+
return redditID;
45+
}
46+
47+
public void setRedditID(String redditID) {
48+
this.redditID = redditID;
49+
}
50+
51+
public Long getId() {
52+
return id;
53+
}
54+
55+
public void setId(Long id) {
56+
this.id = id;
57+
}
58+
59+
public String getTitle() {
60+
return title;
61+
}
62+
63+
public void setTitle(String title) {
64+
this.title = title;
65+
}
66+
67+
public String getUrl() {
68+
return url;
69+
}
70+
71+
public void setUrl(String url) {
72+
this.url = url;
73+
}
74+
75+
public String getDate() {
76+
return date;
77+
}
78+
79+
public void setDate(String date) {
80+
this.date = date;
81+
}
82+
83+
public Date getSubmissionDate() {
84+
return submissionDate;
85+
}
86+
87+
public void setSubmissionDate(Date submissionDate) {
88+
this.submissionDate = submissionDate;
89+
}
90+
91+
public String getUserName() {
92+
return userName;
93+
}
94+
95+
public void setUserName(String userName) {
96+
this.userName = userName;
97+
}
98+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.modelmapper.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Preference {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private Long id;
14+
15+
private String timezone;
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public String getTimezone() {
26+
return timezone;
27+
}
28+
29+
public void setTimezone(String timezone) {
30+
this.timezone = timezone;
31+
}
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.modelmapper.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.OneToOne;
8+
9+
@Entity
10+
public class User {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
private Long id;
15+
16+
private String name;
17+
18+
@OneToOne
19+
Preference preference;
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public Preference getPreference() {
38+
return preference;
39+
}
40+
41+
public void setPreference(Preference preference) {
42+
this.preference = preference;
43+
}
44+
}

0 commit comments

Comments
 (0)