-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/#17 google calendar #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
69f4a52
d146e46
91c83b7
395f312
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,3 +41,6 @@ out/ | |
|
|
||
| ### Docs ### | ||
| docs/ | ||
|
|
||
| ### Google Credentials ### | ||
| src/main/resources/google-credentials.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package com.nailagent.backend.global.calendar; | ||
|
|
||
| import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
| import com.google.api.client.json.gson.GsonFactory; | ||
| import com.google.api.services.calendar.Calendar; | ||
| import com.google.api.services.calendar.CalendarScopes; | ||
| import com.google.api.services.calendar.model.Event; | ||
| import com.google.api.services.calendar.model.EventDateTime; | ||
| import com.google.auth.http.HttpCredentialsAdapter; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.nailagent.backend.domain.reservation.entity.Reservation; | ||
| import com.nailagent.backend.global.exception.CustomException; | ||
| import com.nailagent.backend.global.exception.ErrorCode; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.core.io.ResourceLoader; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.time.LocalDateTime; | ||
| import java.time.LocalTime; | ||
| import java.time.ZoneId; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| public class GoogleCalendarService { | ||
|
|
||
| private Calendar calendar; | ||
| private final String calendarId; | ||
|
|
||
| public GoogleCalendarService( | ||
| @Value("${google.calendar.id}") String calendarId, | ||
| @Value("${google.calendar.credentials-path}") String credentialsPath, | ||
| ResourceLoader resourceLoader | ||
| ) { | ||
| this.calendarId = calendarId; | ||
|
|
||
| try { | ||
| InputStream credentialsStream = resourceLoader.getResource(credentialsPath).getInputStream(); | ||
| GoogleCredentials credentials = GoogleCredentials | ||
| .fromStream(credentialsStream) | ||
| .createScoped(List.of(CalendarScopes.CALENDAR)); | ||
|
|
||
| this.calendar = new Calendar.Builder( | ||
| GoogleNetHttpTransport.newTrustedTransport(), | ||
| GsonFactory.getDefaultInstance(), | ||
| new HttpCredentialsAdapter(credentials) | ||
| ).setApplicationName("NailAgent").build(); | ||
| } catch (Exception e) { | ||
| log.error("Google Calendar ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์คํจ - ์บ๋ฆฐ๋ ์ฐ๋์ด ๋นํ์ฑํ๋ฉ๋๋ค", e); | ||
| this.calendar = null; | ||
| } | ||
| } | ||
|
Comment on lines
+34
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์บ๋ฆฐ๋ ์ด๊ธฐํ ์คํจ๊ฐ ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์ ๊ธฐ๋ ์คํจ๋ก ์ด์ด์ง ์ ์์ต๋๋ค. ์์ฑ์์์ ์์ธ๊ฐ ๋๋ฉด ๋น ์์ฑ์ด ์ค๋จ๋์ด ์์ฝ ๊ธฐ๋ฅ๊น์ง ๊ฐ์ด ์ฃฝ์ต๋๋ค. ์บ๋ฆฐ๋ ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์คํจ๋ ๋ก๊ทธ ํ ๋นํ์ฑ ๋ชจ๋๋ก ์ ํํ๊ณ , ๐ค Prompt for AI Agents |
||
|
|
||
| public String createEvent(Reservation reservation) { | ||
| if (calendar == null) throw new CustomException(ErrorCode.GOOGLE_CALENDAR_SYNC_FAILED); | ||
| try { | ||
| Event event = buildEvent(reservation); | ||
| Event created = calendar.events().insert(calendarId, event).execute(); | ||
| log.info("Google Calendar ์ด๋ฒคํธ ์์ฑ: {}", created.getId()); | ||
| return created.getId(); | ||
| } catch (IOException e) { | ||
| log.error("Google Calendar ์ด๋ฒคํธ ์์ฑ ์คํจ", e); | ||
| throw new CustomException(ErrorCode.GOOGLE_CALENDAR_SYNC_FAILED); | ||
| } | ||
|
Comment on lines
+58
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ๋ ๐ ๏ธ ์ ์ ์์ ์ (์์ง) public String createEvent(Reservation reservation) {
try {
Event event = buildEvent(reservation);
Event created = calendar.events().insert(calendarId, event).execute();
log.info("Google Calendar ์ด๋ฒคํธ ์์ฑ: {}", created.getId());
return created.getId();
- } catch (IOException e) {
+ } catch (IOException | RuntimeException e) {
log.error("Google Calendar ์ด๋ฒคํธ ์์ฑ ์คํจ", e);
return null;
}
} private Event buildEvent(Reservation reservation) {
String[] timeRange = reservation.getReserveTime().split("-");
+ if (timeRange.length != 2) {
+ throw new IllegalArgumentException("Invalid reserveTime format: " + reservation.getReserveTime());
+ }
LocalTime startTime = LocalTime.parse(timeRange[0]);
LocalTime endTime = LocalTime.parse(timeRange[1]);Also applies to: 63-70, 82-86 ๐ค Prompt for AI Agents |
||
| } | ||
|
|
||
| public void updateEvent(String eventId, Reservation reservation) { | ||
| if (calendar == null) return; | ||
| try { | ||
| Event event = buildEvent(reservation); | ||
| calendar.events().update(calendarId, eventId, event).execute(); | ||
| log.info("Google Calendar ์ด๋ฒคํธ ์์ : {}", eventId); | ||
| } catch (IOException e) { | ||
| log.error("Google Calendar ์ด๋ฒคํธ ์์ ์คํจ: eventId={}", eventId, e); | ||
| } | ||
| } | ||
|
|
||
| public void deleteEvent(String eventId) { | ||
| if (calendar == null) return; | ||
| try { | ||
| calendar.events().delete(calendarId, eventId).execute(); | ||
| log.info("Google Calendar ์ด๋ฒคํธ ์ญ์ : {}", eventId); | ||
| } catch (IOException e) { | ||
| log.error("Google Calendar ์ด๋ฒคํธ ์ญ์ ์คํจ: eventId={}", eventId, e); | ||
| } | ||
| } | ||
|
|
||
| private Event buildEvent(Reservation reservation) { | ||
| // reserveTime ํ์: "17:00-18:30" | ||
| String[] timeRange = reservation.getReserveTime().split("-"); | ||
| if (timeRange.length != 2) { | ||
| throw new IllegalArgumentException("Invalid reserveTime format: " + reservation.getReserveTime()); | ||
| } | ||
| LocalTime startTime = LocalTime.parse(timeRange[0]); | ||
| LocalTime endTime = LocalTime.parse(timeRange[1]); | ||
|
|
||
| LocalDateTime startDt = reservation.getReserveDate().atTime(startTime); | ||
| LocalDateTime endDt = reservation.getReserveDate().atTime(endTime); | ||
|
|
||
| String summary = String.format("[%s] %s", reservation.getService(), reservation.getName()); | ||
| String description = String.format( | ||
| "๊ณ ๊ฐ๋ช : %s\n์ฐ๋ฝ์ฒ: %s\n์๋น์ค: %s\n์คํ ์ ๊ฑฐ: %s\n๋ด๋น: %s\n์์ฝ๊ธ: %s์", | ||
| reservation.getName(), | ||
| reservation.getPhoneNum(), | ||
| reservation.getService(), | ||
| Boolean.TRUE.equals(reservation.getOffRemoval()) ? "O" : "X", | ||
| reservation.getDesigner(), | ||
| reservation.getDepositAmount() != null ? reservation.getDepositAmount() : 0 | ||
| ); | ||
|
|
||
| return new Event() | ||
| .setSummary(summary) | ||
| .setDescription(description) | ||
| .setStart(new EventDateTime() | ||
| .setDateTime(toGoogleDateTime(startDt)) | ||
| .setTimeZone("Asia/Seoul")) | ||
| .setEnd(new EventDateTime() | ||
| .setDateTime(toGoogleDateTime(endDt)) | ||
| .setTimeZone("Asia/Seoul")); | ||
| } | ||
|
|
||
| private com.google.api.client.util.DateTime toGoogleDateTime(LocalDateTime ldt) { | ||
| return new com.google.api.client.util.DateTime( | ||
| Date.from(ldt.atZone(ZoneId.of("Asia/Seoul")).toInstant()) | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.