1. 概要
この記事では、既存の機能に小さいながらも便利な改善を実装することで、小さなケーススタディアプリを前進させていきます。
2. より良いテーブル
jQuery DataTablesプラグインを使用して、アプリが以前使用していた古い基本的なテーブルを置き換えることから始めましょう。
2.1. ポストリポジトリとサービス
まず、ユーザーのスケジュールされた投稿をカウントするメソッドを追加します–もちろんSpringデータ構文を活用します。
public interface PostRepository extends JpaRepository<Post, Long> {
...
Long countByUser(User user);
}
次に、サービスレイヤーの実装を簡単に見てみましょう–ページネーションパラメーターに基づいてユーザーの投稿を取得します。
@Override
public List<SimplePostDto> getPostsList(int page, int size, String sortDir, String sort) {
PageRequest pageReq = new PageRequest(page, size, Sort.Direction.fromString(sortDir), sort);
Page<Post> posts = postRepository.findByUser(userService.getCurrentUser(), pageReq);
return constructDataAccordingToUserTimezone(posts.getContent());
}
ユーザーのタイムゾーンに基づいて日付を変換しています:
private List<SimplePostDto> constructDataAccordingToUserTimezone(List<Post> posts) {
String timeZone = userService.getCurrentUser().getPreference().getTimezone();
return posts.stream().map(post -> new SimplePostDto(
post, convertToUserTomeZone(post.getSubmissionDate(), timeZone)))
.collect(Collectors.toList());
}
private String convertToUserTomeZone(Date date, String timeZone) {
dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
return dateFormat.format(date);
}
2.2. ページネーションを使用したAPI
次に、APIを介して、完全なページ付けと並べ替えを使用してこの操作を公開します。
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<SimplePost> getScheduledPosts(
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
@RequestParam(value = "sortDir", required = false, defaultValue = "asc") String sortDir,
@RequestParam(value = "sort", required = false, defaultValue = "title") String sort,
HttpServletResponse response) {
response.addHeader("PAGING_INFO",
scheduledPostService.generatePagingInfo(page, size).toString());
return scheduledPostService.getPostsList(page, size, sortDir, sort);
}
カスタムヘッダーを使用してページネーション情報をクライアントに渡す方法に注意してください。 これを行うための、もう少し標準的な方法があります。これは、後で検討する方法です。
ただし、この実装は単純に十分です。ページング情報を生成する簡単な方法があります。
public PagingInfo generatePagingInfo(int page, int size) {
long total = postRepository.countByUser(userService.getCurrentUser());
return new PagingInfo(page, size, total);
}
そして、 PagingInfo 自体:
public class PagingInfo {
private long totalNoRecords;
private int totalNoPages;
private String uriToNextPage;
private String uriToPrevPage;
public PagingInfo(int page, int size, long totalNoRecords) {
this.totalNoRecords = totalNoRecords;
this.totalNoPages = Math.round(totalNoRecords / size);
if (page > 0) {
this.uriToPrevPage = "page=" + (page - 1) + "&size=" + size;
}
if (page < this.totalNoPages) {
this.uriToNextPage = "page=" + (page + 1) + "&size=" + size;
}
}
}
2.3. フロントエンド
最後に、単純なフロントエンドはカスタムJSメソッドを使用してAPIと対話し、jQueryDataTableパラメーターを処理します。
<table>
<thead><tr>
<th>Post title</th><th>Submission Date</th><th>Status</th>
<th>Resubmit Attempts left</th><th>Actions</th>
</tr></thead>
</table>
<script>
$(document).ready(function() {
$('table').dataTable( {
"processing": true,
"searching":false,
"columnDefs": [
{ "name": "title", "targets": 0 },
{ "name": "submissionDate", "targets": 1 },
{ "name": "submissionResponse", "targets": 2 },
{ "name": "noOfAttempts", "targets": 3 } ],
"columns": [
{ "data": "title" },
{ "data": "submissionDate" },
{ "data": "submissionResponse" },
{ "data": "noOfAttempts" }],
"serverSide": true,
"ajax": function(data, callback, settings) {
$.get('api/scheduledPosts', {
size: data.length,
page: (data.start/data.length),
sortDir: data.order[0].dir,
sort: data.columns[data.order[0].column].name
}, function(res,textStatus, request) {
var pagingInfo = request.getResponseHeader('PAGING_INFO');
var total = pagingInfo.split(",")[0].split("=")[1];
callback({recordsTotal: total, recordsFiltered: total,data: res});
});
}
} );
} );
</script>
2.4. ページングのAPIテスト
APIが公開されたので、いくつかの簡単なAPIテストを記述して、ページングメカニズムの基本が期待どおりに機能することを確認できます。
@Test
public void givenMoreThanOnePage_whenGettingUserScheduledPosts_thenNextPageExist()
throws ParseException, IOException {
createPost();
createPost();
createPost();
Response response = givenAuth().
params("page", 0, "size", 2).get(urlPrefix + "/api/scheduledPosts");
assertEquals(200, response.statusCode());
assertTrue(response.as(List.class).size() > 0);
String pagingInfo = response.getHeader("PAGING_INFO");
long totalNoRecords = Long.parseLong(pagingInfo.split(",")[0].split("=")[1]);
String uriToNextPage = pagingInfo.split(",")[2].replace("uriToNextPage=", "").trim();
assertTrue(totalNoRecords > 2);
assertEquals(uriToNextPage, "page=1&size=2");
}
@Test
public void givenMoreThanOnePage_whenGettingUserScheduledPostsForSecondPage_thenCorrect()
throws ParseException, IOException {
createPost();
createPost();
createPost();
Response response = givenAuth().
params("page", 1, "size", 2).get(urlPrefix + "/api/scheduledPosts");
assertEquals(200, response.statusCode());
assertTrue(response.as(List.class).size() > 0);
String pagingInfo = response.getHeader("PAGING_INFO");
long totalNoRecords = Long.parseLong(pagingInfo.split(",")[0].split("=")[1]);
String uriToPrevPage = pagingInfo.split(",")[3].replace("uriToPrevPage=", "").trim();
assertTrue(totalNoRecords > 2);
assertEquals(uriToPrevPage, "page=0&size=2");
}
3. 電子メール通知
次に、基本的な電子メール通知フローを構築します–ユーザーがスケジュールされた投稿が送信されているときに電子メールを受信します。
3.1. 電子メールの構成
まず、電子メールの構成を行いましょう。
@Bean
public JavaMailSenderImpl javaMailSenderImpl() {
JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl();
mailSenderImpl.setHost(env.getProperty("smtp.host"));
mailSenderImpl.setPort(env.getProperty("smtp.port", Integer.class));
mailSenderImpl.setProtocol(env.getProperty("smtp.protocol"));
mailSenderImpl.setUsername(env.getProperty("smtp.username"));
mailSenderImpl.setPassword(env.getProperty("smtp.password"));
Properties javaMailProps = new Properties();
javaMailProps.put("mail.smtp.auth", true);
javaMailProps.put("mail.smtp.starttls.enable", true);
mailSenderImpl.setJavaMailProperties(javaMailProps);
return mailSenderImpl;
}
SMTPを機能させるために必要なプロパティとともに:
smtp.host=email-smtp.us-east-1.amazonaws.com
smtp.port=465
smtp.protocol=smtps
smtp.username=example
smtp.password=
support.email=example@example.com
3.2. 投稿が公開されたときにイベントを発生させる
スケジュールされた投稿がRedditに正常に公開されたときに、イベントを発生させるようにしましょう。
private void updatePostFromResponse(JsonNode node, Post post) {
JsonNode errorNode = node.get("json").get("errors").get(0);
if (errorNode == null) {
...
String email = post.getUser().getPreference().getEmail();
eventPublisher.publishEvent(new OnPostSubmittedEvent(post, email));
}
...
}
3.3. イベントとリスナー
イベントの実装は非常に簡単です。
public class OnPostSubmittedEvent extends ApplicationEvent {
private Post post;
private String email;
public OnPostSubmittedEvent(Post post, String email) {
super(post);
this.post = post;
this.email = email;
}
}
そしてリスナー:
@Component
public class SubmissionListner implements ApplicationListener<OnPostSubmittedEvent> {
@Autowired
private JavaMailSender mailSender;
@Autowired
private Environment env;
@Override
public void onApplicationEvent(OnPostSubmittedEvent event) {
SimpleMailMessage email = constructEmailMessage(event);
mailSender.send(email);
}
private SimpleMailMessage constructEmailMessage(OnPostSubmittedEvent event) {
String recipientAddress = event.getEmail();
String subject = "Your scheduled post submitted";
SimpleMailMessage email = new SimpleMailMessage();
email.setTo(recipientAddress);
email.setSubject(subject);
email.setText(constructMailContent(event.getPost()));
email.setFrom(env.getProperty("support.email"));
return email;
}
private String constructMailContent(Post post) {
return "Your post " + post.getTitle() + " is submitted.\n" +
"http://www.reddit.com/r/" + post.getSubreddit() +
"/comments/" + post.getRedditID();
}
}
4. 総投票数の使用
次に、再送信オプションを簡素化するための作業を行います。これは、賛成率(理解するのが困難でした)ではなく、総投票数で機能するようになりました。
投稿スコアと賛成率を使用して、総投票数を計算できます。
- スコア=賛成票–反対票
- 総投票数=賛成票+反対票
- 賛成率=賛成/総投票数
など:
総投票数=Math.round(スコア/((2 *賛成率)– 1))
まず、スコアロジックを変更して、この総投票数を計算して追跡します。
public PostScores getPostScores(Post post) {
...
float ratio = node.get("upvote_ratio").floatValue();
postScore.setTotalVotes(Math.round(postScore.getScore() / ((2 * ratio) - 1)));
...
}
そしてもちろん、投稿が失敗したと見なされるかどうかをチェックするときにそれを利用します。
private boolean didPostGoalFail(Post post) {
PostScores postScores = getPostScores(post);
int totalVotes = postScores.getTotalVotes();
...
return (((score < post.getMinScoreRequired()) ||
(totalVotes < post.getMinTotalVotes())) &&
!((noOfComments > 0) && post.isKeepIfHasComments()));
}
最後に、もちろん、古いratioフィールドを使用から削除します。
5. 再送信オプションの検証
最後に、複雑な再送信オプションにいくつかの検証を追加することで、ユーザーを支援します。
5.1. ScheduledPostサービス
単純なcheckIfValidResubmitOptions()メソッドは次のとおりです。
private boolean checkIfValidResubmitOptions(Post post) {
if (checkIfAllNonZero(
post.getNoOfAttempts(),
post.getTimeInterval(),
post.getMinScoreRequired())) {
return true;
} else {
return false;
}
}
private boolean checkIfAllNonZero(int... args) {
for (int tmp : args) {
if (tmp == 0) {
return false;
}
}
return true;
}
新しい投稿をスケジュールするときに、この検証をうまく利用します。
public Post schedulePost(boolean isSuperUser, Post post, boolean resubmitOptionsActivated)
throws ParseException {
if (resubmitOptionsActivated && !checkIfValidResubmitOptions(post)) {
throw new InvalidResubmitOptionsException("Invalid Resubmit Options");
}
...
}
再送信ロジックがオンの場合、次のフィールドはゼロ以外の値である必要があることに注意してください。
- 試行回数
- 時間間隔
- 最低限必要なスコア
5.2. 例外処理
最後に–無効な入力の場合、InvalidResubmitOptionsExceptionはメインのエラー処理ロジックで処理されます。
@ExceptionHandler({ InvalidResubmitOptionsException.class })
public ResponseEntity<Object> handleInvalidResubmitOptions
(RuntimeException ex, WebRequest request) {
logger.error("400 Status Code", ex);
String bodyOfResponse = ex.getLocalizedMessage();
return new ResponseEntity<Object>(
bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST);
}
5.3. 再送信オプションのテスト
最後に、再送信オプションをテストしましょう。アクティブ化と非アクティブ化の両方の条件をテストします。
public class ResubmitOptionsLiveTest extends AbstractLiveTest {
private static final String date = "2016-01-01 00:00";
@Test
public void
givenResubmitOptionsDeactivated_whenSchedulingANewPost_thenCreated()
throws ParseException, IOException {
Post post = createPost();
Response response = withRequestBody(givenAuth(), post)
.queryParams("resubmitOptionsActivated", false)
.post(urlPrefix + "/api/scheduledPosts");
assertEquals(201, response.statusCode());
Post result = objectMapper.reader().forType(Post.class).readValue(response.asString());
assertEquals(result.getUrl(), post.getUrl());
}
@Test
public void
givenResubmitOptionsActivated_whenSchedulingANewPostWithZeroAttempts_thenInvalid()
throws ParseException, IOException {
Post post = createPost();
post.setNoOfAttempts(0);
post.setMinScoreRequired(5);
post.setTimeInterval(60);
Response response = withRequestBody(givenAuth(), post)
.queryParams("resubmitOptionsActivated", true)
.post(urlPrefix + "/api/scheduledPosts");
assertEquals(400, response.statusCode());
assertTrue(response.asString().contains("Invalid Resubmit Options"));
}
@Test
public void
givenResubmitOptionsActivated_whenSchedulingANewPostWithZeroMinScore_thenInvalid()
throws ParseException, IOException {
Post post = createPost();
post.setMinScoreRequired(0);
post.setNoOfAttempts(3);
post.setTimeInterval(60);
Response response = withRequestBody(givenAuth(), post)
.queryParams"resubmitOptionsActivated", true)
.post(urlPrefix + "/api/scheduledPosts");
assertEquals(400, response.statusCode());
assertTrue(response.asString().contains("Invalid Resubmit Options"));
}
@Test
public void
givenResubmitOptionsActivated_whenSchedulingANewPostWithZeroTimeInterval_thenInvalid()
throws ParseException, IOException {
Post post = createPost();
post.setTimeInterval(0);
post.setMinScoreRequired(5);
post.setNoOfAttempts(3);
Response response = withRequestBody(givenAuth(), post)
.queryParams("resubmitOptionsActivated", true)
.post(urlPrefix + "/api/scheduledPosts");
assertEquals(400, response.statusCode());
assertTrue(response.asString().contains("Invalid Resubmit Options"));
}
@Test
public void
givenResubmitOptionsActivated_whenSchedulingNewPostWithValidResubmitOptions_thenCreated()
throws ParseException, IOException {
Post post = createPost();
post.setMinScoreRequired(5);
post.setNoOfAttempts(3);
post.setTimeInterval(60);
Response response = withRequestBody(givenAuth(), post)
.queryParams("resubmitOptionsActivated", true)
.post(urlPrefix + "/api/scheduledPosts");
assertEquals(201, response.statusCode());
Post result = objectMapper.reader().forType(Post.class).readValue(response.asString());
assertEquals(result.getUrl(), post.getUrl());
}
private Post createPost() throws ParseException {
Post post = new Post();
post.setTitle(randomAlphabetic(6));
post.setUrl("test.com");
post.setSubreddit(randomAlphabetic(6));
post.setSubmissionDate(dateFormat.parse(date));
return post;
}
}
6. 結論
今回の記事では、ケーススタディアプリを正しい方向に移動する-使いやすさといういくつかの改善を行いました。
Redditスケジューラアプリの全体的な考え方は、ユーザーがアプリにアクセスし、作業を行い、終了することで、Redditへの新しい記事をすばやくスケジュールできるようにすることです。
そこに到達しています。