Skip to content

Commit a8d31ab

Browse files
author
Eugen
committed
Merge pull request eugenp#157 from Doha2012/master
modify reddit schedule
2 parents f9248df + 9054203 commit a8d31ab

File tree

10 files changed

+183
-38
lines changed

10 files changed

+183
-38
lines changed

spring-security-oauth/src/main/java/org/baeldung/persistence/model/Post.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Post {
2222

2323
private String url;
2424

25+
private boolean sendReplies;
26+
2527
private Date submissionDate;
2628

2729
private boolean isSent;
@@ -68,6 +70,14 @@ public void setUrl(String url) {
6870
this.url = url;
6971
}
7072

73+
public boolean isSendReplies() {
74+
return sendReplies;
75+
}
76+
77+
public void setSendReplies(boolean sendReplies) {
78+
this.sendReplies = sendReplies;
79+
}
80+
7181
public Date getSubmissionDate() {
7282
return submissionDate;
7383
}

spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
import org.slf4j.Logger;
1818
import org.slf4j.LoggerFactory;
1919
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.http.HttpStatus;
2021
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
2122
import org.springframework.security.oauth2.common.OAuth2AccessToken;
2223
import org.springframework.stereotype.Controller;
2324
import org.springframework.ui.Model;
2425
import org.springframework.util.LinkedMultiValueMap;
2526
import org.springframework.util.MultiValueMap;
27+
import org.springframework.web.bind.annotation.PathVariable;
2628
import org.springframework.web.bind.annotation.RequestMapping;
2729
import org.springframework.web.bind.annotation.RequestMethod;
2830
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.ResponseStatus;
2932

3033
import com.fasterxml.jackson.databind.JsonNode;
3134

@@ -96,6 +99,9 @@ public final String schedule(final Model model, @RequestParam final Map<String,
9699
post.setTitle(formParams.get("title"));
97100
post.setSubreddit(formParams.get("sr"));
98101
post.setUrl(formParams.get("url"));
102+
if (formParams.containsKey("sendreplies")) {
103+
post.setSendReplies(true);
104+
}
99105
post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
100106
post.setSubmissionResponse("Not sent yet");
101107
if (post.getSubmissionDate().before(new Date())) {
@@ -116,34 +122,56 @@ public final String getScheduledPosts(final Model model) {
116122
return "postListView";
117123
}
118124

125+
// === post actions
126+
127+
@RequestMapping(value = "/deletePost/{id}", method = RequestMethod.DELETE)
128+
@ResponseStatus(HttpStatus.OK)
129+
public void deletePost(@PathVariable("id") final Long id) {
130+
postReopsitory.delete(id);
131+
}
132+
133+
@RequestMapping(value = "/editPost/{id}", method = RequestMethod.GET)
134+
public String showEditPostForm(final Model model, @PathVariable Long id) {
135+
final Post post = postReopsitory.findOne(id);
136+
model.addAttribute("post", post);
137+
model.addAttribute("dateValue", dateFormat.format(post.getSubmissionDate()));
138+
return "editPostForm";
139+
}
140+
141+
@RequestMapping(value = "/updatePost/{id}", method = RequestMethod.POST)
142+
public String updatePost(Model model, @PathVariable("id") final Long id, @RequestParam final Map<String, String> formParams) throws ParseException {
143+
final Post post = postReopsitory.findOne(id);
144+
post.setTitle(formParams.get("title"));
145+
post.setSubreddit(formParams.get("sr"));
146+
post.setUrl(formParams.get("url"));
147+
if (formParams.containsKey("sendreplies")) {
148+
post.setSendReplies(true);
149+
} else {
150+
post.setSendReplies(false);
151+
}
152+
post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
153+
if (post.getSubmissionDate().before(new Date())) {
154+
model.addAttribute("msg", "Invalid date");
155+
return "submissionResponse";
156+
}
157+
postReopsitory.save(post);
158+
return "redirect:/posts";
159+
}
160+
119161
// === private
120162

121163
private final MultiValueMap<String, String> constructParams(final Map<String, String> formParams) {
122164
final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
123165
param.add(RedditApiConstants.API_TYPE, "json");
124166
param.add(RedditApiConstants.KIND, "link");
125167
param.add(RedditApiConstants.RESUBMIT, "true");
126-
param.add(RedditApiConstants.SENDREPLIES, "false");
127168
param.add(RedditApiConstants.THEN, "comments");
128169
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
129170
param.add(entry.getKey(), entry.getValue());
130171
}
131172
return param;
132173
}
133174

134-
private final Map<String, String> constructParams2(final Map<String, String> formParams) {
135-
final Map<String, String> param = new HashMap<String, String>();
136-
param.put(RedditApiConstants.API_TYPE, "json");
137-
param.put(RedditApiConstants.KIND, "link");
138-
param.put(RedditApiConstants.RESUBMIT, "true");
139-
param.put(RedditApiConstants.SENDREPLIES, "false");
140-
param.put(RedditApiConstants.THEN, "comments");
141-
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
142-
param.put(entry.getKey(), entry.getValue());
143-
}
144-
return param;
145-
}
146-
147175
private final String needsCaptcha() {
148176
final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
149177
return result;

spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ private void submitPost(Post post) {
6060
param.add(RedditApiConstants.API_TYPE, "json");
6161
param.add(RedditApiConstants.KIND, "link");
6262
param.add(RedditApiConstants.RESUBMIT, "true");
63-
param.add(RedditApiConstants.SENDREPLIES, "false");
6463
param.add(RedditApiConstants.THEN, "comments");
64+
if (post.isSendReplies()) {
65+
param.add(RedditApiConstants.SENDREPLIES, "true");
66+
}
6567

6668
logger.info("Submit link with these parameters: " + param.entrySet());
6769
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<html>
3+
<head>
4+
5+
<title>Schedule to Reddit</title>
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
7+
<link rel="stylesheet" href="<c:url value="/resources/datetime-picker.css" />">
8+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
9+
<script src="<c:url value="/resources/datetime-picker.js" />"></script>
10+
11+
</head>
12+
<body>
13+
<nav class="navbar navbar-default">
14+
<div class="container-fluid">
15+
<!-- Brand and toggle get grouped for better mobile display -->
16+
<div class="navbar-header">
17+
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
18+
<span class="sr-only">Toggle navigation</span>
19+
<span class="icon-bar"></span>
20+
<span class="icon-bar"></span>
21+
<span class="icon-bar"></span>
22+
</button>
23+
<a class="navbar-brand" href="info">Schedule to Reddit</a>
24+
</div>
25+
26+
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
27+
28+
<!-- Collect the nav links, forms, and other content for toggling -->
29+
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
30+
<ul class="nav navbar-nav">
31+
<li><a href="posts">My Scheduled Posts</a></li>
32+
<li><a href="post">Post to Reddit</a></li>
33+
<li><a href="postSchedule">Schedule Post to Reddit</a></li>
34+
</ul>
35+
36+
</div><!-- /.navbar-collapse -->
37+
</div><!-- /.container-fluid -->
38+
</nav>
39+
<div class="container">
40+
<h1>Edit Scheduled Post</h1>
41+
<form action="<c:url value="/updatePost/${post.getId()}" />" method="post">
42+
<div class="row">
43+
<input type="hidden" name="id" value="${post.getId()}"/>
44+
<div class="form-group">
45+
<label class="col-sm-3">Title</label>
46+
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" value="${post.getTitle()}" required/></span>
47+
</div>
48+
<br><br>
49+
<div class="form-group">
50+
<label class="col-sm-3">Url</label>
51+
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" value="${post.getUrl()}" required/></span>
52+
</div>
53+
<br><br>
54+
<div class="form-group">
55+
<label class="col-sm-3">Subreddit</label>
56+
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" value="${post.getSubreddit()}" required/></span>
57+
</div>
58+
<br><br>
59+
<div class="col-sm-3">
60+
<input type="checkbox" name="sendreplies" value="true" <c:if test="${post.isSendReplies()=='true'}"> checked </c:if> /> Send replies to my inbox
61+
</div>
62+
<br><br>
63+
64+
<label class="col-sm-3">Submission Date</label>
65+
<span class="col-sm-9"><input type="text" name="date" class="form-control" value="${dateValue}"></span>
66+
<script type="text/javascript">
67+
$(function(){
68+
$('*[name=date]').appendDtpicker({"inline": true});
69+
});
70+
</script>
71+
72+
<br><br>
73+
74+
<button type="submit" class="btn btn-primary">Save Changes</button>
75+
</div>
76+
</form>
77+
</div>
78+
</body>
79+
</html>

spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
22
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
33

4-
<html xmlns="http://www.w3.org/1999/xhtml">
4+
<html>
55
<head>
66

7-
<title>Spring Security OAuth</title>
7+
<title>Schedule to Reddit</title>
88
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
99

1010
</head>
@@ -19,7 +19,7 @@
1919
<span class="icon-bar"></span>
2020
<span class="icon-bar"></span>
2121
</button>
22-
<a class="navbar-brand" href="#">Schedule to Reddit</a>
22+
<a class="navbar-brand" href="info">Schedule to Reddit</a>
2323
</div>
2424

2525
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
@@ -43,16 +43,33 @@
4343
<th>Post title</th>
4444
<th>Submission Date</th>
4545
<th>Status</th>
46+
<th>Actions</th>
4647
</tr>
4748
</thead>
4849
<c:forEach var="post" items="${posts}" >
4950
<tr <c:if test="${post.isSent()}"> class="success"</c:if>>
5051
<td><c:out value="${post.getTitle()}"/></td>
5152
<td><fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${post.getSubmissionDate()}" /></td>
5253
<td><c:out value="${post.getSubmissionResponse()}"/></td>
54+
<td>
55+
<a href="editPost/${post.getId()}" class="btn btn-warning" >Edit</a>
56+
<a href="#" class="btn btn-danger" onclick="deletePost(${post.getId()})">Delete</a>
57+
</td>
5358
</tr>
5459
</c:forEach>
5560
</table>
5661
</div>
62+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
63+
<script>
64+
function deletePost(id){
65+
$.ajax({
66+
url: 'deletePost/'+id,
67+
type: 'DELETE',
68+
success: function(result) {
69+
window.location.href="posts"
70+
}
71+
});
72+
}
73+
</script>
5774
</body>
5875
</html>

spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2-
<html xmlns="http://www.w3.org/1999/xhtml">
2+
<html>
33
<head>
44

5-
<title>Spring Security OAuth</title>
5+
<title>Schedule to Reddit</title>
66
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
77

88
</head>

spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2-
<html xmlns="http://www.w3.org/1999/xhtml">
2+
<html>
33
<head>
44

5-
<title>Spring Security OAuth</title>
5+
<title>Schedule to Reddit</title>
66
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
77
<link rel="stylesheet" href="<c:url value="/resources/datetime-picker.css" />">
88
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
@@ -20,7 +20,7 @@
2020
<span class="icon-bar"></span>
2121
<span class="icon-bar"></span>
2222
</button>
23-
<a class="navbar-brand" href="#">Schedule to Reddit</a>
23+
<a class="navbar-brand" href="info">Schedule to Reddit</a>
2424
</div>
2525

2626
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
@@ -42,17 +42,21 @@
4242
<div class="row">
4343
<div class="form-group">
4444
<label class="col-sm-3">Title</label>
45-
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" /></span>
45+
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" required/></span>
4646
</div>
4747
<br><br>
4848
<div class="form-group">
4949
<label class="col-sm-3">Url</label>
50-
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" /></span>
50+
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" required/></span>
5151
</div>
5252
<br><br>
5353
<div class="form-group">
5454
<label class="col-sm-3">Subreddit</label>
55-
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" /></span>
55+
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" required/></span>
56+
</div>
57+
<br><br>
58+
<div class="col-sm-3">
59+
<input type="checkbox" name="sendreplies" value="true"/> Send replies to my inbox
5660
</div>
5761
<br><br>
5862

@@ -65,6 +69,7 @@
6569
</script>
6670

6771
<br><br>
72+
6873
<button type="submit" class="btn btn-primary">Schedule</button>
6974
</div>
7075
</form>

spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2-
<html xmlns="http://www.w3.org/1999/xhtml">
2+
<html>
33
<head>
44

5-
<title>Spring Security OAuth</title>
5+
<title>Schedule to Reddit</title>
66
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
77

88
</head>
@@ -17,7 +17,7 @@
1717
<span class="icon-bar"></span>
1818
<span class="icon-bar"></span>
1919
</button>
20-
<a class="navbar-brand" href="#">Schedule to Reddit</a>
20+
<a class="navbar-brand" href="info">Schedule to Reddit</a>
2121
</div>
2222

2323
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
@@ -39,17 +39,21 @@
3939
<div class="row">
4040
<div class="form-group">
4141
<label class="col-sm-3">Title</label>
42-
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" /></span>
42+
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" required/></span>
4343
</div>
4444
<br><br>
4545
<div class="form-group">
4646
<label class="col-sm-3">Url</label>
47-
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" /></span>
47+
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" required /></span>
4848
</div>
4949
<br><br>
5050
<div class="form-group">
5151
<label class="col-sm-3">Subreddit</label>
52-
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" /></span>
52+
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" required/></span>
53+
</div>
54+
<br><br>
55+
<div class="col-sm-3">
56+
<input type="checkbox" name="sendreplies" value="true"/> Send replies to my inbox
5357
</div>
5458
<br><br>
5559

0 commit comments

Comments
 (0)