Skip to content

Commit

Permalink
wip: 交代メソッドの抽出
Browse files Browse the repository at this point in the history
  • Loading branch information
uesaka-seiya committed Dec 25, 2020
1 parent c24781c commit f48b0e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jp.co.esm.miffy.Controller;

import jp.co.esm.miffy.component.HookComponent;
import jp.co.esm.miffy.entity.Asf4Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
Expand All @@ -11,9 +10,9 @@

@Controller
@RequiredArgsConstructor
public class Asf4MemberController {
public class HookController {
private final jp.co.esm.miffy.service.HookService hookService;
private final HookComponent hookComponent;
//private final HookComponent hookComponent;

@GetMapping("asf4members")
public String index(Model model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public String index(Model model, Asf4Member asf4Member, Check check) {

@RequestMapping("/skip")
public String index(Model model) {
hookService.setNextCleaner();
hookComponent.postToHook();
return "redirect:/asf4members";
}
Expand Down
75 changes: 39 additions & 36 deletions src/main/java/jp/co/esm/miffy/service/HookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jp.co.esm.miffy.entity.Asf4Member;
import jp.co.esm.miffy.repository.Asf4MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -15,6 +16,9 @@
@RequiredArgsConstructor
public class HookService {
public final Asf4MemberRepository asf4MemberRepository;
// private final HookComponent hookComponent;

private String notificationCleanerIsUnknown; // 前回の掃除当番が行方不明の場合のエラーメッセージ(毎回リセット)

/**
* テーブルのデータ一覧を返す。
Expand All @@ -36,28 +40,40 @@ private Asf4Member getLastCleaner() {
}

/**
* 今日の掃除当番の人を特定する。
*
* @return 掃除当番をOptionalオブジェクトで返す。
* 今日の掃除当番に交代する。
*/
private Asf4Member getCleaner() throws NoSuchElementException {
@Scheduled(cron = "0 0 9 * * 1-5", zone = "Asia/Tokyo")
public void setNextCleaner() throws NoSuchElementException {
Asf4Member lastCleaner = getLastCleaner();
int cleanerId = lastCleaner.getId();
Optional<Asf4Member> cleanerOptional = asf4MemberRepository.findTopBySkipFalseAndIdGreaterThanOrderByIdAsc(cleanerId);
if (cleanerOptional.isEmpty()) {
cleanerOptional = asf4MemberRepository.findTopBySkipFalseOrderByIdAsc();
}
Asf4Member cleaner;
if (cleanerOptional.isPresent()) {
cleaner = cleanerOptional.get();
cleaner.setCleaner(true);
asf4MemberRepository.saveAndFlush(cleaner);
lastCleaner.setCleaner(false);
asf4MemberRepository.saveAndFlush(lastCleaner);
} else {
cleaner = null;
try {
int cleanerId = lastCleaner.getId();
Optional<Asf4Member> cleanerOptional = asf4MemberRepository.findTopBySkipFalseAndIdGreaterThanOrderByIdAsc(cleanerId);
if (cleanerOptional.isEmpty()) {
cleanerOptional = asf4MemberRepository.findTopBySkipFalseOrderByIdAsc();
}
if (cleanerOptional.isPresent()) {
cleaner = cleanerOptional.get();
cleaner.setCleaner(true);
asf4MemberRepository.saveAndFlush(cleaner);
lastCleaner.setCleaner(false);
asf4MemberRepository.saveAndFlush(lastCleaner);
}
} catch(NoSuchElementException e) {
notificationCleanerIsUnknown = makeErrorMessage(e);
// hookComponent.postToHook(); // ここで通知したい
System.out.println(notificationCleanerIsUnknown); // 通知の代わりの出力
notificationCleanerIsUnknown = null;
}
return cleaner;
}

String makeErrorMessage(NoSuchElementException e) {
e.printStackTrace();
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("all ");
errorMessage.append(e.getMessage());
errorMessage.append("前回掃除した人は誰?(・x・)");
return errorMessage.toString();
}

/**
Expand Down Expand Up @@ -121,28 +137,15 @@ public String makeRequest(AJD date) {
if (isHoliday(date)) {
return null;
}
String mainMessage = makeMainMessage();
// Asf4Member cleaner;
// try {
// cleaner = getCleaner();
// if (cleaner != null) {
// postIdobataId = cleaner.getIdobataId();
// mainMessage = "今日の掃除当番です";
// } else {
// postIdobataId = "here";
// mainMessage = "今日は誰もオフィスにいないみたい(・x・)";
// }
// } catch (NoSuchElementException e) {
// e.printStackTrace();
// postIdobataId = "all ";
// errorMessage = e.getMessage();
// mainMessage = "前回掃除した人は誰?(・x・)";
// }
StringBuilder request = new StringBuilder();
request.append("{");
request.append("\"source\":\"");
request.append("@");
request.append(mainMessage);
if (notificationCleanerIsUnknown == null) {
request.append(makeMainMessage());
} else {
request.append(notificationCleanerIsUnknown);
}
request.append("\"");
request.append("}");
return request.toString();
Expand Down

0 comments on commit f48b0e9

Please sign in to comment.