modify schedule reddit

This commit is contained in:
DOHA 2015-03-08 21:59:07 +02:00
parent 87283a97f9
commit dc58d2a801
10 changed files with 179 additions and 34 deletions

View File

@ -22,6 +22,8 @@ public class Post {
private String url;
private boolean sendReplies;
private Date submissionDate;
private boolean isSent;
@ -68,6 +70,14 @@ public class Post {
this.url = url;
}
public boolean isSendReplies() {
return sendReplies;
}
public void setSendReplies(boolean sendReplies) {
this.sendReplies = sendReplies;
}
public Date getSubmissionDate() {
return submissionDate;
}

View File

@ -17,15 +17,18 @@ import org.baeldung.reddit.util.RedditApiConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.fasterxml.jackson.databind.JsonNode;
@ -96,6 +99,9 @@ public class RedditController {
post.setTitle(formParams.get("title"));
post.setSubreddit(formParams.get("sr"));
post.setUrl(formParams.get("url"));
if (formParams.containsKey("sendreplies")) {
post.setSendReplies(true);
}
post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
post.setSubmissionResponse("Not sent yet");
if (post.getSubmissionDate().before(new Date())) {
@ -116,6 +122,42 @@ public class RedditController {
return "postListView";
}
// === post actions
@RequestMapping(value = "/deletePost/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void deletePost(@PathVariable("id") final Long id) {
postReopsitory.delete(id);
}
@RequestMapping(value = "/editPost/{id}", method = RequestMethod.GET)
public String showEditPostForm(final Model model, @PathVariable Long id) {
final Post post = postReopsitory.findOne(id);
model.addAttribute("post", post);
model.addAttribute("dateValue", dateFormat.format(post.getSubmissionDate()));
return "editPostForm";
}
@RequestMapping(value = "/updatePost/{id}", method = RequestMethod.POST)
public String updatePost(Model model, @PathVariable("id") final Long id, @RequestParam final Map<String, String> formParams) throws ParseException {
final Post post = postReopsitory.findOne(id);
post.setTitle(formParams.get("title"));
post.setSubreddit(formParams.get("sr"));
post.setUrl(formParams.get("url"));
if (formParams.containsKey("sendreplies")) {
post.setSendReplies(true);
} else {
post.setSendReplies(false);
}
post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
if (post.getSubmissionDate().before(new Date())) {
model.addAttribute("msg", "Invalid date");
return "submissionResponse";
}
postReopsitory.save(post);
return "redirect:/posts";
}
// === private
private final MultiValueMap<String, String> constructParams(final Map<String, String> formParams) {
@ -123,7 +165,6 @@ public class RedditController {
param.add(RedditApiConstants.API_TYPE, "json");
param.add(RedditApiConstants.KIND, "link");
param.add(RedditApiConstants.RESUBMIT, "true");
param.add(RedditApiConstants.SENDREPLIES, "false");
param.add(RedditApiConstants.THEN, "comments");
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
param.add(entry.getKey(), entry.getValue());
@ -131,19 +172,6 @@ public class RedditController {
return param;
}
private final Map<String, String> constructParams2(final Map<String, String> formParams) {
final Map<String, String> param = new HashMap<String, String>();
param.put(RedditApiConstants.API_TYPE, "json");
param.put(RedditApiConstants.KIND, "link");
param.put(RedditApiConstants.RESUBMIT, "true");
param.put(RedditApiConstants.SENDREPLIES, "false");
param.put(RedditApiConstants.THEN, "comments");
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
param.put(entry.getKey(), entry.getValue());
}
return param;
}
private final String needsCaptcha() {
final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
return result;

View File

@ -60,8 +60,10 @@ public class ScheduledTasks {
param.add(RedditApiConstants.API_TYPE, "json");
param.add(RedditApiConstants.KIND, "link");
param.add(RedditApiConstants.RESUBMIT, "true");
param.add(RedditApiConstants.SENDREPLIES, "false");
param.add(RedditApiConstants.THEN, "comments");
if (post.isSendReplies()) {
param.add(RedditApiConstants.SENDREPLIES, "true");
}
logger.info("Submit link with these parameters: " + param.entrySet());
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);

View File

@ -0,0 +1,79 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="<c:url value="/resources/datetime-picker.css" />">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="<c:url value="/resources/datetime-picker.js" />"></script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="postSchedule">Schedule to Reddit</a>
</div>
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="posts">My Scheduled Posts</a></li>
<li><a href="post">Post to Reddit</a></li>
<li><a href="postSchedule">Schedule Post to Reddit</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container">
<h1>Edit Scheduled Post</h1>
<form action="<c:url value="/updatePost/${post.getId()}" />" method="post">
<div class="row">
<input type="hidden" name="id" value="${post.getId()}"/>
<div class="form-group">
<label class="col-sm-3">Title</label>
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" value="${post.getTitle()}" required/></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Url</label>
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" value="${post.getUrl()}" required/></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Subreddit</label>
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" value="${post.getSubreddit()}" required/></span>
</div>
<br><br>
<div class="col-sm-3">
<input type="checkbox" name="sendreplies" value="true" <c:if test="${post.isSendReplies()=='true'}"> checked </c:if> /> Send replies to my inbox
</div>
<br><br>
<label class="col-sm-3">Submission Date</label>
<span class="col-sm-9"><input type="text" name="date" class="form-control" value="${dateValue}"></span>
<script type="text/javascript">
$(function(){
$('*[name=date]').appendDtpicker({"inline": true});
});
</script>
<br><br>
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</body>
</html>

View File

@ -1,10 +1,10 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Spring Security OAuth</title>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
@ -43,6 +43,7 @@
<th>Post title</th>
<th>Submission Date</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<c:forEach var="post" items="${posts}" >
@ -50,9 +51,25 @@
<td><c:out value="${post.getTitle()}"/></td>
<td><fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${post.getSubmissionDate()}" /></td>
<td><c:out value="${post.getSubmissionResponse()}"/></td>
<td>
<a href="editPost/${post.getId()}" class="btn btn-warning" >Edit</a>
<a href="#" class="btn btn-danger" onclick="deletePost(${post.getId()})">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function deletePost(id){
$.ajax({
url: 'deletePost/'+id,
type: 'DELETE',
success: function(result) {
window.location.href="posts"
}
});
}
</script>
</body>
</html>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Spring Security OAuth</title>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Spring Security OAuth</title>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="<c:url value="/resources/datetime-picker.css" />">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
@ -42,17 +42,21 @@
<div class="row">
<div class="form-group">
<label class="col-sm-3">Title</label>
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" /></span>
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" required/></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Url</label>
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" /></span>
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" required/></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Subreddit</label>
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" /></span>
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" required/></span>
</div>
<br><br>
<div class="col-sm-3">
<input type="checkbox" name="sendreplies" value="true"/> Send replies to my inbox
</div>
<br><br>
@ -65,6 +69,7 @@
</script>
<br><br>
<button type="submit" class="btn btn-primary">Schedule</button>
</div>
</form>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Spring Security OAuth</title>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
@ -39,17 +39,21 @@
<div class="row">
<div class="form-group">
<label class="col-sm-3">Title</label>
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" /></span>
<span class="col-sm-9"><input name="title" placeholder="title" class="form-control" required/></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Url</label>
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" /></span>
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" required /></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Subreddit</label>
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" /></span>
<span class="col-sm-9"><input name="sr" placeholder="Subreddit" class="form-control" required/></span>
</div>
<br><br>
<div class="col-sm-3">
<input type="checkbox" name="sendreplies" value="true"/> Send replies to my inbox
</div>
<br><br>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Spring Security OAuth</title>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>

View File

@ -4,13 +4,13 @@
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spring Security OAuth</title>
<title>Schedule to Reddit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Welcome to Spring Security OAuth</h1>
<h1>Schedule to Reddit</h1>
<a href="info" class="btn btn-primary">Login with Reddit</a>
</div>
</body>
</html>