Skip to content
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

develop -> main #1146

Merged
merged 10 commits into from
Dec 20, 2024
11 changes: 11 additions & 0 deletions src/main/java/in/koreatech/koin/domain/bus/controller/BusApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;
import in.koreatech.koin.domain.bus.dto.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -139,4 +140,14 @@ ResponseEntity<BusScheduleResponse> getBusRouteSchedule(
@RequestParam BusStation depart,
@RequestParam BusStation arrival
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "버스 긴급 공지 조회")
@GetMapping("/notice")
ResponseEntity<BusNoticeResponse> getNotice();
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
package in.koreatech.koin.domain.bus.controller;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.domain.bus.dto.BusCourseResponse;
import in.koreatech.koin.domain.bus.dto.BusRemainTimeResponse;
import in.koreatech.koin.domain.bus.dto.BusRouteCommand;
import in.koreatech.koin.domain.bus.dto.BusScheduleResponse;
import in.koreatech.koin.domain.bus.dto.BusTimetableResponse;
import in.koreatech.koin.domain.bus.dto.CityBusTimetableResponse;
import in.koreatech.koin.domain.bus.dto.ShuttleBusRoutesResponse;
import in.koreatech.koin.domain.bus.dto.ShuttleBusTimetableResponse;
import in.koreatech.koin.domain.bus.dto.SingleBusTimeResponse;
import in.koreatech.koin.domain.bus.dto.*;
import in.koreatech.koin.domain.bus.model.BusTimetable;
import in.koreatech.koin.domain.bus.model.enums.BusRouteType;
import in.koreatech.koin.domain.bus.model.enums.BusStation;
import in.koreatech.koin.domain.bus.model.enums.BusType;
import in.koreatech.koin.domain.bus.model.enums.CityBusDirection;
import in.koreatech.koin.domain.bus.service.BusService;
import in.koreatech.koin.domain.bus.service.ShuttleBusService;
import in.koreatech.koin.domain.community.article.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -113,4 +101,10 @@ public ResponseEntity<BusScheduleResponse> getBusRouteSchedule(
BusScheduleResponse busSchedule = busService.getBusSchedule(request);
return ResponseEntity.ok().body(busSchedule);
}

@GetMapping("/notice")
public ResponseEntity<BusNoticeResponse> getNotice() {
BusNoticeResponse busNoticeResponse = busService.getNotice();
return ResponseEntity.ok().body(busNoticeResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package in.koreatech.koin.domain.bus.dto;

import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.v3.oas.annotations.media.Schema;

import static com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;

@JsonNaming(value = SnakeCaseStrategy.class)
public record BusNoticeResponse(
@Schema(description = "공지글 번호", example = "17153", requiredMode = NOT_REQUIRED)
Integer id,

@Schema(description = "공지글 제목", example = "[긴급][총무팀]2024.11.27.(수, 오늘) 천안, 청주 야간 셔틀 20시 지연운행", requiredMode = NOT_REQUIRED)
String title
) {

public static BusNoticeResponse of(Integer id, String title) {
return new BusNoticeResponse(id, title);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public final class ExpressBusSchedule {
LocalTime.of(22, 5)
);

public static List<LocalTime> getExpressBusScheduleToKoreaTech() {
public static List<LocalTime> TerminalToKoreaTech() {
return KOREA_TECH_SCHEDULE;
}

public static List<LocalTime> getExpressBusScheduleToTerminal() {
public static List<LocalTime> KoreaTechToTerminal() {
return TERMINAL_SCHEDULE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package in.koreatech.koin.domain.bus.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
@RequiredArgsConstructor
public class BusNoticeRepository {

public static final String BUS_NOTICE_KEY = "busNoticeArticle";
private final RedisTemplate<String, Object> redisTemplate;

public Map<Object, Object> getBusNotice() {
Map<Object, Object> article = redisTemplate.opsForHash().entries(BUS_NOTICE_KEY);

if (article.isEmpty()) {
return null;
}

return article;
}
}
49 changes: 24 additions & 25 deletions src/main/java/in/koreatech/koin/domain/bus/service/BusService.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
package in.koreatech.koin.domain.bus.service;

import static in.koreatech.koin.domain.bus.model.enums.BusStation.getDirection;

import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.domain.bus.dto.BusCourseResponse;
import in.koreatech.koin.domain.bus.dto.BusRemainTimeResponse;
import in.koreatech.koin.domain.bus.dto.BusRouteCommand;
import in.koreatech.koin.domain.bus.dto.BusScheduleResponse;
import in.koreatech.koin.domain.bus.dto.*;
import in.koreatech.koin.domain.bus.dto.BusScheduleResponse.ScheduleInfo;
import in.koreatech.koin.domain.bus.dto.BusTimetableResponse;
import in.koreatech.koin.domain.bus.dto.CityBusTimetableResponse;
import in.koreatech.koin.domain.bus.dto.SingleBusTimeResponse;
import in.koreatech.koin.domain.bus.exception.BusIllegalStationException;
import in.koreatech.koin.domain.bus.exception.BusTypeNotFoundException;
import in.koreatech.koin.domain.bus.exception.BusTypeNotSupportException;
Expand All @@ -39,6 +15,7 @@
import in.koreatech.koin.domain.bus.model.mongo.BusCourse;
import in.koreatech.koin.domain.bus.model.mongo.CityBusTimetable;
import in.koreatech.koin.domain.bus.model.mongo.Route;
import in.koreatech.koin.domain.bus.repository.BusNoticeRepository;
import in.koreatech.koin.domain.bus.repository.BusRepository;
import in.koreatech.koin.domain.bus.repository.CityBusTimetableRepository;
import in.koreatech.koin.domain.bus.service.route.BusRouteStrategy;
Expand All @@ -49,6 +26,14 @@
import in.koreatech.koin.domain.version.service.VersionService;
import in.koreatech.koin.global.exception.KoinIllegalArgumentException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.*;
import java.time.format.TextStyle;
import java.util.*;

import static in.koreatech.koin.domain.bus.model.enums.BusStation.getDirection;

@Service
@Transactional(readOnly = true)
Expand All @@ -57,6 +42,7 @@ public class BusService {

private final Clock clock;
private final BusRepository busRepository;
private final BusNoticeRepository busNoticeRepository;
private final CityBusTimetableRepository cityBusTimetableRepository;
private final CityBusClient cityBusClient;
private final ExpressBusService expressBusService;
Expand Down Expand Up @@ -255,4 +241,17 @@ public BusScheduleResponse getBusSchedule(BusRouteCommand request) {
scheduleInfoList
);
}

public BusNoticeResponse getNotice() {
Map<Object, Object> article = busNoticeRepository.getBusNotice();

if (article == null || article.isEmpty()) {
return null;
}

return BusNoticeResponse.of(
(Integer) article.get("id"),
(String) article.get("title")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ExpressBusRouteStrategy implements BusRouteStrategy {

@Override
public List<ScheduleInfo> findSchedule(BusRouteCommand command) {
if(validCourse(command.depart(), command.arrive())) return Collections.emptyList();
if(validRoute(command.depart(), command.arrive())) return Collections.emptyList();
BusDirection direction = getRouteDirection(command.depart(), command.arrive());

return getStaticExpressBusScheduleTimeList(direction).stream()
Expand All @@ -36,8 +36,8 @@ public boolean support(BusRouteType type) {

private List<LocalTime> getStaticExpressBusScheduleTimeList(BusDirection direction) {
return switch (direction) {
case NORTH -> ExpressBusSchedule.getExpressBusScheduleToKoreaTech();
case SOUTH -> ExpressBusSchedule.getExpressBusScheduleToTerminal();
case NORTH -> ExpressBusSchedule.KoreaTechToTerminal();
case SOUTH -> ExpressBusSchedule.TerminalToKoreaTech();
};
}

Expand All @@ -46,7 +46,7 @@ private BusDirection getRouteDirection(BusStation depart, BusStation arrive) {
? BusDirection.NORTH : BusDirection.SOUTH;
}

private boolean validCourse(BusStation depart, BusStation arrive) {
private boolean validRoute(BusStation depart, BusStation arrive) {
return (depart == BusStation.STATION || arrive == BusStation.STATION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package in.koreatech.koin.domain.community.article.model.redis;

import in.koreatech.koin.domain.community.article.model.Article;
import org.springframework.data.annotation.Id;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.redis.core.RedisHash;

@Getter
@RedisHash(value = "busNoticeArticle")
public class BusNoticeArticle {

@Id
private Integer id;

private String title;

@Builder
private BusNoticeArticle(Integer id, String title) {
this.id = id;
this.title = title;
}

public static BusNoticeArticle from(Article article) {
return BusNoticeArticle.builder()
.id(article.getId())
.title(article.getTitle())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ default Article getNextArticle(Board board, Article article) {

@Query("SELECT a.title FROM Article a WHERE a.id = :id")
String getTitleById(@Param("id") Integer id);

@Query(value = "SELECT * FROM new_articles a "
+ "WHERE a.title REGEXP '통학버스|등교버스|셔틀버스|하교버스' AND a.is_notice = true "
+ "ORDER BY a.created_at DESC LIMIT 5", nativeQuery = true)
List<Article> findBusArticlesTop5OrderByCreatedAtDesc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package in.koreatech.koin.domain.community.article.repository.redis;

import in.koreatech.koin.domain.community.article.model.redis.BusNoticeArticle;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
@RequiredArgsConstructor
public class BusArticleRepository {

public static final String BUS_NOTICE_KEY = "busNoticeArticle";
private final RedisTemplate<String, Object> redisTemplate;

public void save(BusNoticeArticle article) {
Map<Object, Object> existingArticle = redisTemplate.opsForHash().entries(BUS_NOTICE_KEY);

if (!existingArticle.isEmpty()) {
Object existingId = existingArticle.get("id");
if (existingId.equals(article.getId())) {
return;
}
}

redisTemplate.delete(BUS_NOTICE_KEY);
redisTemplate.opsForHash().put(BUS_NOTICE_KEY, "id", article.getId());
redisTemplate.opsForHash().put(BUS_NOTICE_KEY, "title", article.getTitle());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ public void resetOldKeywordsAndIpMaps() {
log.error("많이 검색한 키워드 초기화 중에 오류가 발생했습니다.", e);
}
}

@Scheduled(cron = "0 0 * * * *")
public void getBusNoticeArticle() {
try {
articleService.updateBusNoticeArticle();
} catch (Exception e) {
log.error("버스 공지 게시글 조회 중에 오류가 발생했습니다.", e);
}
}
}
Loading
Loading