From 827c98e080833f8b78bb32186914d07bd63795b9 Mon Sep 17 00:00:00 2001 From: jcw1031 Date: Wed, 8 May 2024 11:27:51 +0900 Subject: [PATCH 1/3] refactor: remove logging service --- .../controller/AnnouncementController.java | 18 +++--------------- .../knu/likeknu/controller/BusController.java | 17 +++-------------- .../likeknu/service/AnnouncementService.java | 18 +++++------------- .../controller/AnnouncementControllerTest.java | 5 +---- .../service/AnnouncementServiceTest.java | 2 +- 5 files changed, 13 insertions(+), 47 deletions(-) diff --git a/src/main/java/ac/knu/likeknu/controller/AnnouncementController.java b/src/main/java/ac/knu/likeknu/controller/AnnouncementController.java index 1d6aed0..672419d 100644 --- a/src/main/java/ac/knu/likeknu/controller/AnnouncementController.java +++ b/src/main/java/ac/knu/likeknu/controller/AnnouncementController.java @@ -5,14 +5,10 @@ import ac.knu.likeknu.controller.dto.base.PageResponseDto; import ac.knu.likeknu.domain.constants.Campus; import ac.knu.likeknu.domain.constants.Category; -import ac.knu.likeknu.logging.domain.value.LogType; -import ac.knu.likeknu.logging.service.LoggingService; import ac.knu.likeknu.service.AnnouncementService; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -25,29 +21,21 @@ public class AnnouncementController { private final AnnouncementService announcementService; - private final LoggingService loggingService; - public AnnouncementController(AnnouncementService announcementService, LoggingService loggingService) { + public AnnouncementController(AnnouncementService announcementService) { this.announcementService = announcementService; - this.loggingService = loggingService; } @GetMapping("/{category}") public PageResponseDto> recentAnnouncementList( @RequestParam("campus") Campus campus, @RequestParam(name = "page", defaultValue = "1") int page, - @RequestParam(name = "keyword", required = false, defaultValue = "") String keyword, @PathVariable("category") String category, - @RequestHeader(name = "Device-Id", required = false) String deviceId, - @RequestParam(name = "deviceId", required = false) String nativeDeviceId + @RequestParam(name = "deviceId", required = false) String deviceId ) { - if (StringUtils.hasText(keyword)) { - loggingService.addLog(LogType.SEARCH_ANNOUNCEMENT, deviceId, category, keyword); - } - PageDto pageDto = PageDto.of(page); List studentNewsList = - announcementService.getAnnouncements(campus, Category.of(category), pageDto, keyword.trim(), nativeDeviceId); + announcementService.getAnnouncements(campus, Category.of(category), pageDto, deviceId); return PageResponseDto.of(studentNewsList, pageDto); } diff --git a/src/main/java/ac/knu/likeknu/controller/BusController.java b/src/main/java/ac/knu/likeknu/controller/BusController.java index ed049a0..44ab082 100644 --- a/src/main/java/ac/knu/likeknu/controller/BusController.java +++ b/src/main/java/ac/knu/likeknu/controller/BusController.java @@ -6,13 +6,10 @@ import ac.knu.likeknu.controller.dto.shuttlebus.ShuttleListResponse; import ac.knu.likeknu.domain.constants.Campus; import ac.knu.likeknu.domain.constants.RouteType; -import ac.knu.likeknu.logging.domain.value.LogType; -import ac.knu.likeknu.logging.service.LoggingService; import ac.knu.likeknu.service.CityBusService; import ac.knu.likeknu.service.ShuttleBusService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -25,24 +22,17 @@ public class BusController { private final CityBusService cityBusService; private final ShuttleBusService shuttleBusService; - private final LoggingService loggingService; - public BusController(CityBusService cityBusService, ShuttleBusService shuttleBusService, LoggingService loggingService) { + public BusController(CityBusService cityBusService, ShuttleBusService shuttleBusService) { this.cityBusService = cityBusService; this.shuttleBusService = shuttleBusService; - this.loggingService = loggingService; } @GetMapping("/city-bus/{type}") private ResponseDto> cityBusesArrivalTime( @RequestParam("campus") Campus campus, @PathVariable("type") String type, - @RequestParam(value = "isRefresh", required = false) boolean isRefresh, - @RequestHeader(name = "Device-Id", required = false) String deviceId + @RequestParam(value = "isRefresh", required = false) boolean isRefresh ) { - if (isRefresh) { - loggingService.addLog(LogType.REFRESH_CITY_BUS, deviceId); - } - RouteType routeType = RouteType.of(type); List cityBusesArrivalTime = cityBusService.getCityBusesArrivalTime(campus, routeType); return ResponseDto.of(cityBusesArrivalTime); @@ -56,9 +46,8 @@ public ResponseDto> eachRouteShuttleBuses(@RequestPara @GetMapping("/shuttle-bus/{shuttleId}") public ResponseDto> shuttleBusesArrivalTime( - @PathVariable("shuttleId") String shuttleId, @RequestHeader(name = "Device-Id", required = false) String deviceId + @PathVariable("shuttleId") String shuttleId ) { - loggingService.addLog(LogType.SELECT_SHUTTLE, deviceId, shuttleId); List shuttleBusesArrivalTime = shuttleBusService.getShuttleBusesArrivalTime(shuttleId); return ResponseDto.of(shuttleBusesArrivalTime); } diff --git a/src/main/java/ac/knu/likeknu/service/AnnouncementService.java b/src/main/java/ac/knu/likeknu/service/AnnouncementService.java index 20acf43..fc25304 100644 --- a/src/main/java/ac/knu/likeknu/service/AnnouncementService.java +++ b/src/main/java/ac/knu/likeknu/service/AnnouncementService.java @@ -35,27 +35,19 @@ public AnnouncementService(AnnouncementRepository announcementRepository, Device this.deviceRepository = deviceRepository; } - public List getAnnouncements(Campus campus, Category category, PageDto pageDto, String keyword, String nativeDeviceId) { + public List getAnnouncements(Campus campus, Category category, PageDto pageDto, String deviceId) { int requestPage = pageDto.getCurrentPage() - 1; PageRequest pageRequest = PageRequest.of(requestPage, DEFAULT_ANNOUNCEMENT_PAGE_SIZE, Sort.by(Order.desc("announcementDate"), Order.desc("collectedAt"))); - Page announcementsPage; - if (keyword != null && !keyword.isEmpty()) { - announcementsPage = announcementRepository.findByCampusInAndCategoryAndAnnouncementTitleContains( - Set.of(campus, Campus.ALL), category, keyword, pageRequest - ); - } else { - announcementsPage = announcementRepository.findByCampusInAndCategory( - Set.of(campus, Campus.ALL), category, pageRequest - ); - } + Page announcementsPage = + announcementRepository.findByCampusInAndCategory(Set.of(campus, Campus.ALL), category, pageRequest); pageDto.updateTotalPages(announcementsPage.getTotalPages()); pageDto.updateTotalElements(announcementsPage.getTotalElements()); - if (nativeDeviceId != null) { - Optional findDevice = deviceRepository.findById(nativeDeviceId); + if (deviceId != null) { + Optional findDevice = deviceRepository.findById(deviceId); if (findDevice.isEmpty()) { return announcementsPage.stream() .map(AnnouncementListResponse::of) diff --git a/src/test/java/ac/knu/likeknu/controller/AnnouncementControllerTest.java b/src/test/java/ac/knu/likeknu/controller/AnnouncementControllerTest.java index 29a7aa6..ea91b80 100644 --- a/src/test/java/ac/knu/likeknu/controller/AnnouncementControllerTest.java +++ b/src/test/java/ac/knu/likeknu/controller/AnnouncementControllerTest.java @@ -6,7 +6,6 @@ import ac.knu.likeknu.domain.constants.Campus; import ac.knu.likeknu.domain.constants.Category; import ac.knu.likeknu.domain.constants.Tag; -import ac.knu.likeknu.logging.service.LoggingService; import ac.knu.likeknu.service.AnnouncementService; import ac.knu.likeknu.service.SlackService; import ac.knu.likeknu.utils.TestInstanceFactory; @@ -44,8 +43,6 @@ class AnnouncementControllerTest { private AnnouncementService announcementService; @MockBean private SlackService slackService; - @MockBean - private LoggingService loggingService; @DisplayName("학생소식 조회 API 요청에 성공한다.") @Test @@ -56,7 +53,7 @@ void fetchStudentNewsApiSuccess() throws Exception { Announcement announcement3 = TestInstanceFactory.createAnnouncement("Test C", "https://testc.com", Tag.LIBRARY); // when - when(announcementService.getAnnouncements(eq(Campus.CHEONAN), eq(Category.STUDENT_NEWS), any(PageDto.class), any(), any())) + when(announcementService.getAnnouncements(eq(Campus.CHEONAN), eq(Category.STUDENT_NEWS), any(PageDto.class), any())) .thenReturn(List.of( AnnouncementListResponse.of(announcement1), AnnouncementListResponse.of(announcement2), diff --git a/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java b/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java index b74d335..fc5a9bd 100644 --- a/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java +++ b/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java @@ -63,7 +63,7 @@ void getStudentNewsSuccess() throws Exception { .thenReturn(Optional.of(new Device("deviceA", null, Campus.SINGWAN, LocalDateTime.now(), null, null))); List announcementList = - announcementService.getAnnouncements(Campus.CHEONAN, Category.STUDENT_NEWS, pageDto, "", ""); + announcementService.getAnnouncements(Campus.CHEONAN, Category.STUDENT_NEWS, pageDto, ""); // then AnnouncementListResponse announcementResponse = announcementList.get(0); From 3a490681097f7459dd5e7bab73d740eb417b9f76 Mon Sep 17 00:00:00 2001 From: jcw1031 Date: Thu, 9 May 2024 21:24:16 +0900 Subject: [PATCH 2/3] refactor: remove unused code --- .../dto/device/request/DeviceRegistrationRequest.java | 4 +--- src/main/java/ac/knu/likeknu/domain/Device.java | 4 ++-- src/main/java/ac/knu/likeknu/service/DeviceService.java | 5 ----- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java b/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java index 184d915..595ffa1 100644 --- a/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java +++ b/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java @@ -1,12 +1,10 @@ package ac.knu.likeknu.controller.dto.device.request; import ac.knu.likeknu.domain.constants.Campus; -import com.fasterxml.jackson.annotation.JsonAlias; public record DeviceRegistrationRequest( String deviceId, - @JsonAlias("platform") - String userAgent, + String platform, String modelName, String osVersion, Campus campus, diff --git a/src/main/java/ac/knu/likeknu/domain/Device.java b/src/main/java/ac/knu/likeknu/domain/Device.java index c91ef1f..765e8f8 100644 --- a/src/main/java/ac/knu/likeknu/domain/Device.java +++ b/src/main/java/ac/knu/likeknu/domain/Device.java @@ -94,7 +94,7 @@ public Device(String id, String fcmToken, Campus campus, LocalDateTime registere public static Device of(DeviceRegistrationRequest request) { return Device.builder() .id(request.deviceId()) - .campus(Campus.CHEONAN) + .campus(Campus.SINGWAN) .registeredAt(LocalDateTime.now()) .themeColor(request.themeColor()) .favoriteCafeteria(request.favoriteCafeteria()) @@ -111,7 +111,7 @@ public void updateNotification(boolean isTurnOnNotification) { } public void update(DeviceRegistrationRequest deviceRequest) { - this.platform = deviceRequest.userAgent(); + this.platform = deviceRequest.platform(); this.campus = deviceRequest.campus(); this.themeColor = deviceRequest.themeColor(); this.favoriteCafeteria = deviceRequest.favoriteCafeteria(); diff --git a/src/main/java/ac/knu/likeknu/service/DeviceService.java b/src/main/java/ac/knu/likeknu/service/DeviceService.java index 6583e48..2741935 100644 --- a/src/main/java/ac/knu/likeknu/service/DeviceService.java +++ b/src/main/java/ac/knu/likeknu/service/DeviceService.java @@ -28,11 +28,6 @@ public DeviceService(DeviceRepository deviceRepository) { } public void registerDevice(DeviceRegistrationRequest deviceRequest) { - String userAgent = deviceRequest.userAgent(); - if (userAgent.contains("Googlebot") || userAgent.contains("AdsBot") || userAgent.contains("vercel")) { - return; - } - String deviceId = deviceRequest.deviceId(); Device device = deviceRepository.findById(deviceId) .orElseGet(() -> Device.of(deviceRequest)); From fa69ce6957e5fd691be2372fb7cf03f0e08e0e78 Mon Sep 17 00:00:00 2001 From: jcw1031 Date: Thu, 9 May 2024 21:31:11 +0900 Subject: [PATCH 3/3] hotfix: app version collect --- .../dto/device/request/DeviceRegistrationRequest.java | 1 + src/main/java/ac/knu/likeknu/domain/Device.java | 10 +++++++--- .../db/migration/mysql/V12__add_app_version_column.sql | 2 ++ .../knu/likeknu/service/AnnouncementServiceTest.java | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/db/migration/mysql/V12__add_app_version_column.sql diff --git a/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java b/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java index 595ffa1..71f33e1 100644 --- a/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java +++ b/src/main/java/ac/knu/likeknu/controller/dto/device/request/DeviceRegistrationRequest.java @@ -7,6 +7,7 @@ public record DeviceRegistrationRequest( String platform, String modelName, String osVersion, + String appVersion, Campus campus, String themeColor, String favoriteCafeteria diff --git a/src/main/java/ac/knu/likeknu/domain/Device.java b/src/main/java/ac/knu/likeknu/domain/Device.java index 765e8f8..d5b31c5 100644 --- a/src/main/java/ac/knu/likeknu/domain/Device.java +++ b/src/main/java/ac/knu/likeknu/domain/Device.java @@ -54,6 +54,8 @@ public class Device { private String osVersion; + private String appVersion; + private String themeColor; private String favoriteCafeteria; @@ -82,11 +84,12 @@ protected Device() { } @Builder - public Device(String id, String fcmToken, Campus campus, LocalDateTime registeredAt, String themeColor, String favoriteCafeteria) { + public Device(String id, String fcmToken, Campus campus, LocalDateTime registeredAt, String appVersion, String themeColor, String favoriteCafeteria) { this.id = id; this.fcmToken = fcmToken; this.campus = campus; this.registeredAt = registeredAt; + this.appVersion = appVersion; this.themeColor = themeColor; this.favoriteCafeteria = favoriteCafeteria; } @@ -96,8 +99,6 @@ public static Device of(DeviceRegistrationRequest request) { .id(request.deviceId()) .campus(Campus.SINGWAN) .registeredAt(LocalDateTime.now()) - .themeColor(request.themeColor()) - .favoriteCafeteria(request.favoriteCafeteria()) .build(); } @@ -123,5 +124,8 @@ public void update(DeviceRegistrationRequest deviceRequest) { if (deviceRequest.osVersion() != null) { this.osVersion = deviceRequest.osVersion(); } + if (deviceRequest.appVersion() != null) { + this.appVersion = deviceRequest.appVersion(); + } } } diff --git a/src/main/resources/db/migration/mysql/V12__add_app_version_column.sql b/src/main/resources/db/migration/mysql/V12__add_app_version_column.sql new file mode 100644 index 0000000..975a4aa --- /dev/null +++ b/src/main/resources/db/migration/mysql/V12__add_app_version_column.sql @@ -0,0 +1,2 @@ +ALTER TABLE device + ADD COLUMN app_version VARCHAR(14) AFTER os_version; diff --git a/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java b/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java index fc5a9bd..77a51ab 100644 --- a/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java +++ b/src/test/java/ac/knu/likeknu/service/AnnouncementServiceTest.java @@ -60,7 +60,7 @@ void getStudentNewsSuccess() throws Exception { eq(Set.of(Campus.ALL, Campus.CHEONAN)), eq(Category.STUDENT_NEWS), any(Pageable.class) )).thenReturn(new PageImpl<>(List.of(announcement1, announcement2, announcement3), PageRequest.of(0, 10), 1)); when(deviceRepository.findById(any(String.class))) - .thenReturn(Optional.of(new Device("deviceA", null, Campus.SINGWAN, LocalDateTime.now(), null, null))); + .thenReturn(Optional.of(new Device("deviceA", null, Campus.SINGWAN, LocalDateTime.now(), null, null, null))); List announcementList = announcementService.getAnnouncements(Campus.CHEONAN, Category.STUDENT_NEWS, pageDto, "");