Skip to content

Commit 365a972

Browse files
author
Eugen
committed
Merge pull request eugenp#208 from Doha2012/master
more reddit improvements
2 parents 02ae0e1 + 79c2194 commit 365a972

File tree

11 files changed

+197
-16
lines changed

11 files changed

+197
-16
lines changed

spring-security-oauth/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@
167167
<artifactId>h2</artifactId>
168168
<version>1.4.187</version>
169169
</dependency>
170+
171+
<!-- postgresql -->
172+
173+
<dependency>
174+
<groupId>org.postgresql</groupId>
175+
<artifactId>postgresql</artifactId>
176+
<version>9.4-1201-jdbc41</version>
177+
</dependency>
178+
170179

171180
<!-- web -->
172181

spring-security-oauth/src/main/java/org/baeldung/config/PersistenceJPAConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@Configuration
2222
@EnableTransactionManagement
23-
@PropertySource({ "classpath:persistence-${envTarget:prod}.properties" })
23+
@PropertySource({ "classpath:persistence-${envTarget:dev}.properties" })
2424
@ComponentScan({ "org.baeldung.persistence" })
2525
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
2626
public class PersistenceJPAConfig {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import javax.persistence.GeneratedValue;
88
import javax.persistence.GenerationType;
99
import javax.persistence.Id;
10+
import javax.persistence.Table;
1011

1112
@Entity
13+
@Table(name = "APP_USER")
1214
public class User {
1315

1416
@Id

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ public final String predicatePostResponse(@RequestParam(value = "title") final S
142142
return (result == RedditClassifier.GOOD) ? "{Good Response}" : "{Bad response}";
143143
}
144144

145+
@RequestMapping(value = "/checkIfAlreadySubmitted", method = RequestMethod.POST)
146+
@ResponseBody
147+
public String checkIfAlreadySubmitted(@RequestParam("url") final String url, @RequestParam("sr") final String sr) {
148+
logger.info("check if already submitted");
149+
final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/r/" + sr + "/search?q=url:" + url + "&restrict_sr=on", JsonNode.class);
150+
logger.info(node.toString());
151+
return node.get("data").get("children").toString();
152+
}
153+
154+
@RequestMapping(value = "/subredditAutoComplete")
155+
@ResponseBody
156+
public String subredditAutoComplete(@RequestParam("term") final String term) {
157+
final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
158+
param.add("query", term);
159+
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com//api/search_reddit_names", param, JsonNode.class);
160+
return node.get("names").toString();
161+
}
162+
145163
// === post actions
146164

147165
@RequestMapping(value = "/deletePost/{id}", method = RequestMethod.DELETE)
@@ -164,6 +182,11 @@ public String updatePost(Model model, @PathVariable("id") final Long id, @Reques
164182
post.setTitle(formParams.get("title"));
165183
post.setSubreddit(formParams.get("sr"));
166184
post.setUrl(formParams.get("url"));
185+
186+
post.setNoOfAttempts(Integer.parseInt(formParams.get("attempt")));
187+
post.setTimeInterval(Integer.parseInt(formParams.get("interval")));
188+
post.setMinScoreRequired(Integer.parseInt(formParams.get("score")));
189+
167190
if (formParams.containsKey("sendreplies")) {
168191
post.setSendReplies(true);
169192
} else {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
################### DataSource Configuration ##########################
2+
jdbc.driverClassName=com.mysql.jdbc.Driver
3+
jdbc.url=jdbc:mysql://localhost:3306/oauth_reddit?createDatabaseIfNotExist=true
4+
jdbc.user=tutorialuser
5+
jdbc.pass=tutorialmy5ql
6+
init-db=false
7+
################### Hibernate Configuration ##########################
8+
hibernate.dialect=org.hibernate.dialect.MySQLDialect
9+
hibernate.show_sql=false
10+
hibernate.hbm2ddl.auto=update

spring-security-oauth/src/main/resources/persistence-prod.properties renamed to spring-security-oauth/src/main/resources/persistence-production.properties

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
################### DataSource Configuration ##########################
22
jdbc.driverClassName=org.postgresql.Driver
3-
jdbc.url=jdbc:postgres://fkqrbxdwqbrzhv:Y83Axvgz5iC_tyRZQxI0KEmUEG@ec2-107-20-222-114.compute-1.amazonaws.com:5432/dfmoej36meltnj
4-
jdbc.user=fkqrbxdwqbrzhv
5-
jdbc.pass=Y83Axvgz5iC_tyRZQxI0KEmUEG
6-
3+
jdbc.url=jdbc:postgresql://ec2-184-73-253-4.compute-1.amazonaws.com:5432/d3th4rkdb2g3p6?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
4+
jdbc.user=fhjspqyblvawvp
5+
jdbc.pass=gRKGJbdHXslq0XgdI03j2Y4YoE
76
################### Hibernate Configuration ##########################
87
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
98
hibernate.show_sql=false
10-
hibernate.hbm2ddl.auto=update
9+
hibernate.hbm2ddl.auto=create-drop

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,42 @@ border-color: #ddd;
5050
</span>
5151
</div>
5252
<br/><br/>
53-
54-
<label class="col-sm-3">Submission Date</label>
53+
<br/>
54+
<hr/>
55+
<br/>
56+
<div class="form-group">
57+
<label class="col-sm-3">Resubmit If:</label>
58+
59+
<span class="col-sm-2">Votes didn't exceed </span>
60+
<span class="col-sm-1">
61+
<input type="number" class="form-control input-sm" th:value="${post.getMinScoreRequired()}" name="score" required="required"/>
62+
</span>
63+
64+
<span class="col-sm-3">within &nbsp;&nbsp;
65+
<select name="interval">
66+
<option value="0" th:selected="${post.getTimeInterval() == 0}">None</option>
67+
<option value="45" th:selected="${post.getTimeInterval() == 45}">45 minutes</option>
68+
<option value="60" th:selected="${post.getTimeInterval() == 60}">1 hour</option>
69+
<option value="120" th:selected="${post.getTimeInterval() == 120}">2 hours</option>
70+
</select>
71+
</span>
72+
73+
<span class="col-sm-3">try resubmitting &nbsp;&nbsp;
74+
<select name="attempt">
75+
<option value="0" th:selected="${post.getNoOfAttempts() == 0}">No</option>
76+
<option value="1" th:selected="${post.getNoOfAttempts() == 1}">1</option>
77+
<option value="2" th:selected="${post.getNoOfAttempts() == 2}">2</option>
78+
<option value="3" th:selected="${post.getNoOfAttempts() == 3}">3</option>
79+
<option value="4" th:selected="${post.getNoOfAttempts() == 4}">4</option>
80+
<option value="5" th:selected="${post.getNoOfAttempts() == 5}">5</option>
81+
</select>
82+
&nbsp;&nbsp; times.
83+
</span>
84+
85+
86+
</div>
87+
<br/><br/>
88+
<label class="col-sm-3">Submission Date (<span th:text="${#dates.format(#calendars.createToday(), 'z')}">UTC</span>)</label>
5589
<span class="col-sm-9"><input type="text" name="date" class="form-control" th:value="${dateValue}" readonly="readonly"/></span>
5690
<script type="text/javascript">
5791
$(function(){

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
<th>Post title</th>
1717
<th>Submission Date</th>
1818
<th>Status</th>
19+
<th>Resubmit Attempts left</th>
1920
<th>Actions</th>
2021
</tr>
2122
</thead>
2223
<tr th:each="post : ${posts}">
2324
<td th:text="${post.getTitle()}"></td>
2425
<td th:text="${#calendars.format(post.getSubmissionDate(),'dd MMMM yyyy HH:mm z')}"></td>
2526
<td th:text="${post.getSubmissionResponse()}"></td>
27+
<td th:if="${post.getNoOfAttempts() > 0}" th:text="${post.getNoOfAttempts()}"></td>
28+
<td th:unless="${post.getNoOfAttempts() > 0}">-</td>
2629
<td>
2730
<a th:href="@{/editPost/{id}(id=${post.getId()})}" class="btn btn-warning" >Edit</a>
2831
<a href="#" class="btn btn-danger" th:onclick="'javascript:confirmDelete(\'' +${post.getId()}+ '\') '">Delete</a>

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<title>Schedule to Reddit</title>
55
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"/>
66
<link rel="stylesheet" th:href="@{/resources/datetime-picker.css}"/>
7+
<link rel="stylesheet" th:href="@{/resources/autocomplete.css}"/>
78
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
9+
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
810
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
911
<script th:src="@{/resources/datetime-picker.js}"></script>
1012
<script th:src="@{/resources/validator.js}"></script>
@@ -39,15 +41,21 @@ border-color: #ddd;
3941
<br/><br/>
4042
<div class="form-group">
4143
<label class="col-sm-3">Subreddit</label>
42-
<span class="col-sm-9"><input name="sr" placeholder="Subreddit (e.g. kitten)" class="form-control" required="required" data-minlength="3"/></span>
44+
<span class="col-sm-9"><input id="sr" name="sr" placeholder="Subreddit (e.g. kitten)" class="form-control" required="required" data-minlength="3"/></span>
4345
</div>
4446
<br/><br/>
4547
<div>
4648
<label class="col-sm-3">Send replies to my inbox</label> <span class="col-sm-9"><input type="checkbox" name="sendreplies" value="true"/></span>
4749
</div>
48-
<br/>
50+
<br/><br/>
51+
<div>
52+
<span class="col-sm-2"><a href="#" class="btn btn-default" onclick="checkIfAlreadySubmitted()">Check if already submitted</a></span>
53+
<span class="col-sm-1"></span>
54+
<span class="col-sm-9"><span id="checkResult" class="alert alert-info" style="display:none"></span></span>
55+
</div>
56+
<br/><br/>
4957
<hr/>
50-
<br/>
58+
5159
<div class="form-group">
5260
<label class="col-sm-3">Resubmit If:</label>
5361

@@ -81,7 +89,7 @@ border-color: #ddd;
8189
<br/><br/>
8290

8391

84-
<label class="col-sm-3">Submission Date</label>
92+
<label class="col-sm-3">Submission Date (<span th:text="${#dates.format(#calendars.createToday(), 'z')}">UTC</span>)</label>
8593
<span class="col-sm-9"><input type="text" name="date" class="form-control" readonly="readonly"/></span>
8694
<script type="text/javascript">
8795
/*<![CDATA[*/
@@ -98,5 +106,35 @@ border-color: #ddd;
98106
</div>
99107
</form>
100108
</div>
109+
<script>
110+
$(function() {
111+
$( "#sr" ).autocomplete({
112+
source: "subredditAutoComplete"
113+
});
114+
});
115+
</script>
116+
117+
<script>
118+
/*<![CDATA[*/
119+
function checkIfAlreadySubmitted(){
120+
var url = $("input[name='url']").val();
121+
var sr = $("input[name='sr']").val();
122+
console.log(url);
123+
if(url.length >3 && sr.length > 3){
124+
$.post("checkIfAlreadySubmitted",{url: url, sr: sr}, function(data){
125+
var result = JSON.parse(data);
126+
if(result.length == 0){
127+
$("#checkResult").show().html("Not submitted before");
128+
}else{
129+
$("#checkResult").show().html('Already submitted <b><a target="_blank" href="http://www.reddit.com'+result[0].data.permalink+'">here</a></b>');
130+
}
131+
});
132+
}
133+
else{
134+
$("#checkResult").show().html("Too short url and/or subreddit");
135+
}
136+
}
137+
/*]]>*/
138+
</script>
101139
</body>
102140
</html>

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
<title>Schedule to Reddit</title>
55
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"/>
6+
<link rel="stylesheet" th:href="@{/resources/autocomplete.css}"/>
67
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
8+
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
79
<script th:src="@{/resources/validator.js}"></script>
810
<style type="text/css">
911
.btn.disabled{
@@ -36,11 +38,19 @@ border-color: #ddd;
3638
<br/><br/>
3739
<div class="form-group">
3840
<label class="col-sm-3">Subreddit</label>
39-
<span class="col-sm-9"><input name="sr" placeholder="Subreddit (e.g. kitten)" class="form-control" data-minlength="3" required="required"/></span>
41+
<span class="col-sm-9"><input id="sr" name="sr" placeholder="Subreddit (e.g. kitten)" class="form-control" data-minlength="3" required="required"/></span>
4042
</div>
4143
<br/><br/>
4244
<div>
43-
<label class="col-sm-3">Send replies to my inbox</label> <span class="col-sm-9"><input type="checkbox" name="sendreplies" value="true"/></span>
45+
<label class="col-sm-3">Send replies to my inbox</label>
46+
<span class="col-sm-9"><input type="checkbox" name="sendreplies" value="true"/></span>
47+
</div>
48+
<br/>
49+
<br/>
50+
<div>
51+
<span class="col-sm-2"><a href="#" class="btn btn-default" onclick="checkIfAlreadySubmitted()">Check if already submitted</a></span>
52+
<span class="col-sm-1"></span>
53+
<span class=" col-sm-9"><span id="checkResult" class="alert alert-info" style="display:none"></span></span>
4454
</div>
4555
<br/><br/>
4656

@@ -62,8 +72,6 @@ border-color: #ddd;
6272
<div th:if="${session.PREDICTION_FEATURE.isActive()}">
6373
<button id="checkbtn" class="btn btn-default disabled" onclick="predicateResponse()">Predicate Response</button>
6474
<span id="prediction"></span>
65-
66-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
6775
<script type="text/javascript">
6876
/*<![CDATA[*/
6977
$("input").change(function() {
@@ -89,5 +97,35 @@ function predicateResponse(){
8997
</div>
9098
</div>
9199
</div>
100+
<script>
101+
$(function() {
102+
$( "#sr" ).autocomplete({
103+
source: "subredditAutoComplete"
104+
});
105+
});
106+
</script>
107+
108+
<script>
109+
/*<![CDATA[*/
110+
function checkIfAlreadySubmitted(){
111+
var url = $("input[name='url']").val();
112+
var sr = $("input[name='sr']").val();
113+
console.log(url);
114+
if(url.length >3 && sr.length > 3){
115+
$.post("checkIfAlreadySubmitted",{url: url, sr: sr}, function(data){
116+
var result = JSON.parse(data);
117+
if(result.length == 0){
118+
$("#checkResult").show().html("Not submitted before");
119+
}else{
120+
$("#checkResult").show().html('Already submitted <b><a target="_blank" href="http://www.reddit.com'+result[0].data.permalink+'">here</a></b>');
121+
}
122+
});
123+
}
124+
else{
125+
$("#checkResult").show().html("Too short url and/or subreddit");
126+
}
127+
}
128+
/*]]>*/
129+
</script>
92130
</body>
93131
</html>

0 commit comments

Comments
 (0)