1717import org .slf4j .Logger ;
1818import org .slf4j .LoggerFactory ;
1919import org .springframework .beans .factory .annotation .Autowired ;
20+ import org .springframework .http .HttpStatus ;
2021import org .springframework .security .oauth2 .client .OAuth2RestTemplate ;
2122import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
2223import org .springframework .stereotype .Controller ;
2324import org .springframework .ui .Model ;
2425import org .springframework .util .LinkedMultiValueMap ;
2526import org .springframework .util .MultiValueMap ;
27+ import org .springframework .web .bind .annotation .PathVariable ;
2628import org .springframework .web .bind .annotation .RequestMapping ;
2729import org .springframework .web .bind .annotation .RequestMethod ;
2830import org .springframework .web .bind .annotation .RequestParam ;
31+ import org .springframework .web .bind .annotation .ResponseStatus ;
2932
3033import 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 ;
0 commit comments