From 694a2ee8e1a70446990ffa20d4be4e717ff80a0e Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Tue, 3 Dec 2024 18:23:48 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20slack=20=EC=9E=90=EB=8F=99=ED=99=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=B8=EC=A7=80=20=EA=B8=B0=EB=8A=A5=20=EA=B0=9C?= =?UTF-8?q?=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/ScheduleService.java | 18 ++-- .../schedule/infrastructure/SlackService.java | 85 +++++++++++++++++++ .../persistence/ScheduleJpaRepository.java | 14 +++ .../presentation/ScheduleController.java | 14 +-- 4 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 BE/error/src/main/java/com/example/demo/schedule/infrastructure/SlackService.java diff --git a/BE/error/src/main/java/com/example/demo/schedule/application/service/ScheduleService.java b/BE/error/src/main/java/com/example/demo/schedule/application/service/ScheduleService.java index 098c5ee..8744c16 100644 --- a/BE/error/src/main/java/com/example/demo/schedule/application/service/ScheduleService.java +++ b/BE/error/src/main/java/com/example/demo/schedule/application/service/ScheduleService.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -73,10 +74,17 @@ public List getPrivateSchedule() { public List findWeekendSchedule() { //List schedules = new ArrayList<>(); - List test = scheduleJpaRepository.findWeekendPublicSchedule(); - if (test.size() == 0) { - System.out.println("empty"); - } - return test; + +// List test = scheduleJpaRepository.findWeekendPublicSchedule(); +// if (test.size() == 0) { +// System.out.println("empty"); +// } +// return test; + + LocalDateTime startDate = LocalDateTime.now(); + LocalDateTime endDate = startDate.plusDays(5); + + List schedules = scheduleJpaRepository.findWeekPublic(startDate, endDate); + return schedules; } } \ No newline at end of file diff --git a/BE/error/src/main/java/com/example/demo/schedule/infrastructure/SlackService.java b/BE/error/src/main/java/com/example/demo/schedule/infrastructure/SlackService.java new file mode 100644 index 0000000..f762a0f --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/schedule/infrastructure/SlackService.java @@ -0,0 +1,85 @@ +package com.example.demo.schedule.infrastructure; + +import com.example.demo.auth.infra.oauth.slack.exception.SlackApiException; +import com.example.demo.schedule.application.service.ScheduleService; +import com.example.demo.schedule.infrastructure.persistence.ScheduleEntity; +import com.slack.api.Slack; +import com.slack.api.methods.MethodsClient; +import com.slack.api.methods.request.chat.ChatPostMessageRequest; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +@Service +@Slf4j +@RequiredArgsConstructor +@PropertySource("classpath:/application.properties") +public class SlackService { + + @Value("${spring.config.activate.on-profile.oauth.provider.slack.token}") + private String slackToken; + private final ScheduleService scheduleService; + private final static String CHANNEL_NAME = "에코노베이션-일정-소개-페이지"; + + public void sendSlackMessage(String message, String channel){ + + + try{ + MethodsClient methods = Slack.getInstance().methods(slackToken); + + ChatPostMessageRequest request = ChatPostMessageRequest.builder() + .channel(CHANNEL_NAME) + .text(message) + .build(); + + methods.chatPostMessage(request); + + log.info("Slack " + channel + " 에 메시지 보냄"); + + } catch (SlackApiException | IOException e) { + log.error(e.getMessage()); + } catch (com.slack.api.methods.SlackApiException e) { + throw new RuntimeException(e); + } + } + + public String makeSlackMessage() { + List schedules = scheduleService.findWeekendSchedule(); + + if (schedules.isEmpty()) { + return "이번주는 일정이 존재하지 않습니다."; + } + + StringBuilder messageBuilder = new StringBuilder(); + + for (int i = 0; i < schedules.size(); i++) { + if (i > 0) { + messageBuilder.append("\n\n"); // 일정 사이에 한 줄 추가 + } + messageBuilder.append(formatSchedule(schedules.get(i))); + } + + return messageBuilder.toString(); + } + + public String formatSchedule(ScheduleEntity schedule) { + LocalDateTime startDate = schedule.getEventStartDate(); + + // 날짜와 시간을 지정된 형식으로 변환 + String formattedDate = startDate.format(DateTimeFormatter.ofPattern("M월 d일 (E) HH:mm", Locale.KOREAN)); + + return String.format("(%s)\n- 일시: %s\n- 장소: %s", + schedule.getEventName(), + formattedDate, + schedule.getEventPlace()); + } +} diff --git a/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java b/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java index 5c55c0a..26e1b92 100644 --- a/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java +++ b/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java @@ -32,6 +32,20 @@ public ScheduleEntity save(ScheduleEntity entity) { } } + + + public List findWeekPublic(LocalDateTime startDate, LocalDateTime endDate) { + return em.createQuery("SELECT s FROM ScheduleEntity s " + + "WHERE s.scheduleType = :scheduleType " + + "AND s.eventStartDate BETWEEN :startDate AND :endDate", ScheduleEntity.class) + .setParameter("scheduleType", "PUBLIC") // scheduleType 파라미터 설정 + .setParameter("startDate", startDate) // startDate 파라미터 설정 + .setParameter("endDate", endDate) // endDate 파라미터 설정 + .getResultList(); + } + + + public List findWeekendPublicSchedule() { List entity = em.createQuery("SELECT s FROM ScheduleEntity s \n" + "WHERE s.scheduleType = 'PUBLIC' \n" + diff --git a/BE/error/src/main/java/com/example/demo/schedule/presentation/ScheduleController.java b/BE/error/src/main/java/com/example/demo/schedule/presentation/ScheduleController.java index c0b260e..dee22c5 100644 --- a/BE/error/src/main/java/com/example/demo/schedule/presentation/ScheduleController.java +++ b/BE/error/src/main/java/com/example/demo/schedule/presentation/ScheduleController.java @@ -6,6 +6,7 @@ import com.example.demo.common.presentation.response.MessageCode; import com.example.demo.schedule.application.dto.*; import com.example.demo.schedule.application.service.ScheduleService; +import com.example.demo.schedule.infrastructure.SlackService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -19,6 +20,7 @@ public class ScheduleController { private final ScheduleService scheduleService; + private final SlackService slackService; @PostMapping @@ -71,12 +73,12 @@ public ApiResponse>> getPrivate() { return ApiResponseGenerator.success(response, HttpStatus.OK, MessageCode.GETALL); } -// @GetMapping("slack/test") -// public void slackTest() { -// -// String event = slackService.makeSlackMessage(); -// slackService.sendSlackMessage(event, "test"); -// } + @GetMapping("slack/test") + public void slackTest() { + + String event = slackService.makeSlackMessage(); + slackService.sendSlackMessage(event, "test"); + } // 일정 조회를 어떻게 리팩토링 할 수 있을까?