Merge pull request #157 from Doha2012/master

modify reddit schedule
This commit is contained in:
Eugen 2015-03-08 22:47:49 +02:00
commit a8d31ab9de
10 changed files with 183 additions and 38 deletions

View File

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

View File

@ -17,15 +17,18 @@ import org.baeldung.reddit.util.RedditApiConstants;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
@ -96,6 +99,9 @@ public class RedditController {
post.setTitle(formParams.get("title")); post.setTitle(formParams.get("title"));
post.setSubreddit(formParams.get("sr")); post.setSubreddit(formParams.get("sr"));
post.setUrl(formParams.get("url")); post.setUrl(formParams.get("url"));
if (formParams.containsKey("sendreplies")) {
post.setSendReplies(true);
}
post.setSubmissionDate(dateFormat.parse(formParams.get("date"))); post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
post.setSubmissionResponse("Not sent yet"); post.setSubmissionResponse("Not sent yet");
if (post.getSubmissionDate().before(new Date())) { if (post.getSubmissionDate().before(new Date())) {
@ -116,6 +122,42 @@ public class RedditController {
return "postListView"; 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
private final MultiValueMap<String, String> constructParams(final Map<String, String> formParams) { 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.API_TYPE, "json");
param.add(RedditApiConstants.KIND, "link"); param.add(RedditApiConstants.KIND, "link");
param.add(RedditApiConstants.RESUBMIT, "true"); param.add(RedditApiConstants.RESUBMIT, "true");
param.add(RedditApiConstants.SENDREPLIES, "false");
param.add(RedditApiConstants.THEN, "comments"); param.add(RedditApiConstants.THEN, "comments");
for (final Map.Entry<String, String> entry : formParams.entrySet()) { for (final Map.Entry<String, String> entry : formParams.entrySet()) {
param.add(entry.getKey(), entry.getValue()); param.add(entry.getKey(), entry.getValue());
@ -131,19 +172,6 @@ public class RedditController {
return param; 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() { private final String needsCaptcha() {
final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class); final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
return result; return result;

View File

@ -60,8 +60,10 @@ public class ScheduledTasks {
param.add(RedditApiConstants.API_TYPE, "json"); param.add(RedditApiConstants.API_TYPE, "json");
param.add(RedditApiConstants.KIND, "link"); param.add(RedditApiConstants.KIND, "link");
param.add(RedditApiConstants.RESUBMIT, "true"); param.add(RedditApiConstants.RESUBMIT, "true");
param.add(RedditApiConstants.SENDREPLIES, "false");
param.add(RedditApiConstants.THEN, "comments"); param.add(RedditApiConstants.THEN, "comments");
if (post.isSendReplies()) {
param.add(RedditApiConstants.SENDREPLIES, "true");
}
logger.info("Submit link with these parameters: " + param.entrySet()); logger.info("Submit link with these parameters: " + param.entrySet());
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class); 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="info">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="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml"> <html>
<head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head> </head>
@ -19,7 +19,7 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="#">Schedule to Reddit</a> <a class="navbar-brand" href="info">Schedule to Reddit</a>
</div> </div>
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p> <p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
@ -43,6 +43,7 @@
<th>Post title</th> <th>Post title</th>
<th>Submission Date</th> <th>Submission Date</th>
<th>Status</th> <th>Status</th>
<th>Actions</th>
</tr> </tr>
</thead> </thead>
<c:forEach var="post" items="${posts}" > <c:forEach var="post" items="${posts}" >
@ -50,9 +51,25 @@
<td><c:out value="${post.getTitle()}"/></td> <td><c:out value="${post.getTitle()}"/></td>
<td><fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${post.getSubmissionDate()}" /></td> <td><fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${post.getSubmissionDate()}" /></td>
<td><c:out value="${post.getSubmissionResponse()}"/></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> </tr>
</c:forEach> </c:forEach>
</table> </table>
</div> </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> </body>
</html> </html>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml"> <html>
<head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head> </head>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml"> <html>
<head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="<c:url value="/resources/datetime-picker.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="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
@ -20,7 +20,7 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="#">Schedule to Reddit</a> <a class="navbar-brand" href="info">Schedule to Reddit</a>
</div> </div>
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p> <p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
@ -42,17 +42,21 @@
<div class="row"> <div class="row">
<div class="form-group"> <div class="form-group">
<label class="col-sm-3">Title</label> <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> </div>
<br><br> <br><br>
<div class="form-group"> <div class="form-group">
<label class="col-sm-3">Url</label> <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> </div>
<br><br> <br><br>
<div class="form-group"> <div class="form-group">
<label class="col-sm-3">Subreddit</label> <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> </div>
<br><br> <br><br>
@ -65,6 +69,7 @@
</script> </script>
<br><br> <br><br>
<button type="submit" class="btn btn-primary">Schedule</button> <button type="submit" class="btn btn-primary">Schedule</button>
</div> </div>
</form> </form>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml"> <html>
<head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head> </head>
@ -17,7 +17,7 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="#">Schedule to Reddit</a> <a class="navbar-brand" href="info">Schedule to Reddit</a>
</div> </div>
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p> <p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>
@ -39,17 +39,21 @@
<div class="row"> <div class="row">
<div class="form-group"> <div class="form-group">
<label class="col-sm-3">Title</label> <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> </div>
<br><br> <br><br>
<div class="form-group"> <div class="form-group">
<label class="col-sm-3">Url</label> <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> </div>
<br><br> <br><br>
<div class="form-group"> <div class="form-group">
<label class="col-sm-3">Subreddit</label> <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> </div>
<br><br> <br><br>

View File

@ -1,8 +1,8 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml"> <html>
<head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head> </head>
@ -17,7 +17,7 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="#">Schedule to Reddit</a> <a class="navbar-brand" href="info">Schedule to Reddit</a>
</div> </div>
<p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p> <p class="navbar-text navbar-right">Logged in as <b><c:out value="${username}"/></b>&nbsp;&nbsp;&nbsp;</p>

View File

@ -4,13 +4,13 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <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"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head> </head>
<body> <body>
<div class="container"> <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> <a href="info" class="btn btn-primary">Login with Reddit</a>
</div>
</body> </body>
</html> </html>