diff --git a/src/main/java/server/bookmark/BookMarkController.java b/src/main/java/server/bookmark/BookMarkController.java new file mode 100644 index 0000000..ddcef59 --- /dev/null +++ b/src/main/java/server/bookmark/BookMarkController.java @@ -0,0 +1,34 @@ +package server.bookmark; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import server.entity.BookMark; +import server.entity.User; +import server.oauth.user.UserRepository; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static server.enums.Values.HEADER_AUTHORIZATION; + +@Slf4j +@RestController +@RequestMapping("/api/v1/bookmark") +@RequiredArgsConstructor +public class BookMarkController { + private final UserRepository userRepository; + private final BookMarkRepository bookMarkRepository; + + @GetMapping("/search") + public List searchBookMark(@RequestHeader(value = HEADER_AUTHORIZATION) String authorizationHeader, @RequestParam("ticketIds") List ticketIds) { + //todo 헤더의 토큰을 통해 User 정보를 가져오는 로직 추가 필요 + User user = userRepository.findById("1").get(); + List bookMarks = bookMarkRepository.findAllByTicketIdInAndUserId(ticketIds, user); + List response = bookMarks.stream() + .map(bookMark -> BookMarkSearchResponse.of(bookMark.getTicket().getId(), true)) + .collect(Collectors.toList()); + return response; + } +} diff --git a/src/main/java/server/bookmark/BookMarkRepository.java b/src/main/java/server/bookmark/BookMarkRepository.java new file mode 100644 index 0000000..4d25890 --- /dev/null +++ b/src/main/java/server/bookmark/BookMarkRepository.java @@ -0,0 +1,16 @@ +package server.bookmark; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import server.entity.BookMark; +import server.entity.User; + +import java.util.List; + +@Repository +public interface BookMarkRepository extends JpaRepository { + @Query("SELECT b FROM BookMark b JOIN FETCH b.ticket WHERE b.ticket.id IN :ticketIds AND b.user= :user") + List findAllByTicketIdInAndUserId(@Param("ticketIds") List ticketId, @Param("user") User user); +} diff --git a/src/main/java/server/bookmark/BookMarkSearchResponse.java b/src/main/java/server/bookmark/BookMarkSearchResponse.java new file mode 100644 index 0000000..79d7b08 --- /dev/null +++ b/src/main/java/server/bookmark/BookMarkSearchResponse.java @@ -0,0 +1,25 @@ +package server.bookmark; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class BookMarkSearchResponse { + private Long ticketId; + private boolean isBookMarked; + + @Builder + public BookMarkSearchResponse(Long ticketId, boolean isBookMarked) { + this.ticketId = ticketId; + this.isBookMarked = isBookMarked; + } + + public static BookMarkSearchResponse of(Long ticketId, boolean isBookMarked) { + return BookMarkSearchResponse.builder() + .ticketId(ticketId) + .isBookMarked(isBookMarked) + .build(); + } +} diff --git a/src/main/java/server/enums/Values.java b/src/main/java/server/enums/Values.java new file mode 100644 index 0000000..546b9f1 --- /dev/null +++ b/src/main/java/server/enums/Values.java @@ -0,0 +1,8 @@ +package server.enums; + +import lombok.Getter; + +@Getter +public class Values { + public static final String HEADER_AUTHORIZATION = "Authorization"; +} diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index c1d0126..e2c3f98 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -106,7 +106,57 @@ INSERT INTO ticket (ticket_id, ticket_type, depart_date, depart_time, depart_sta (15760, '무궁화호 1503', '20240807', '20240807103100', '평택', '20240807151300', '여수EXPO', '23000원'), (15840, 'ITX-마음 1184', '20240807', '20240807193700', '평택', '20240807195500', '수원', '0원'), (15920, '새마을호 1051', '20240807', '20240807101100', '평택', '20240807125100', '익산', '16800원'), - (16000, 'ITX-새마을 1083', '20240807', '20240807172800', '평택', '20240807174000', '천안', '4800원'); + (16000, 'ITX-새마을 1083', '20240807', '20240807172800', '평택', '20240807174000', '천안', '4800원'), + (16100, '새마을호 1001', '20240807', '20240807060000', '부산', '20240807100000', '수원', '25000원'), + (16101, 'KTX 101', '20240808', '20240807070000', '부산', '20240807100000', '수원', '48000원'), + (16102, 'ITX-새마을 2010', '20240807', '20240807080000', '부산', '20240807110000', '수원', '30000원'), + (16103, '새마을호 1002', '20240807', '20240807090000', '부산', '20240807130000', '수원', '25000원'), + (16104, 'KTX 102', '20240807', '20240807100000', '부산', '20240807130000', '수원', '48000원'), + (16105, 'ITX-새마을 2011', '20240807', '20240807110000', '부산', '20240807140000', '수원', '30000원'), + (16106, '새마을호 1003', '20240807', '20240807120000', '부산', '20240807160000', '수원', '25000원'), + (16107, 'KTX 103', '20240807', '20240807130000', '부산', '20240807160000', '수원', '48000원'), + (16108, 'ITX-새마을 2012', '20240807', '20240807140000', '부산', '20240807170000', '수원', '30000원'), + (16109, '새마을호 1004', '20240807', '20240807150000', '부산', '20240807190000', '수원', '25000원'), + (16110, 'KTX 104', '20240807', '20240807160000', '부산', '20240807190000', '수원', '48000원'), + (16111, 'ITX-새마을 2013', '20240807', '20240807170000', '부산', '20240807200000', '수원', '30000원'), + (16112, '새마을호 1005', '20240807', '20240807180000', '부산', '20240807220000', '수원', '25000원'), + (16113, 'KTX 105', '20240807', '20240807190000', '부산', '20240807220000', '수원', '48000원'), + (16114, 'ITX-새마을 2014', '20240807', '20240807200000', '부산', '20240807230000', '수원', '30000원'), + (16115, '새마을호 1006', '20240807', '20240807210000', '부산', '20240808010000', '수원', '25000원'), + (16116, 'KTX 106', '20240807', '20240807220000', '부산', '20240808010000', '수원', '48000원'), + (16117, 'ITX-새마을 2015', '20240807', '20240807230000', '부산', '20240808020000', '수원', '30000원'), + (16118, '새마을호 1007', '20240807', '20240808000000', '부산', '20240808040000', '수원', '25000원'), + (16119, 'KTX 107', '20240807', '20240808010000', '부산', '20240808040000', '수원', '48000원'), + (16120, 'ITX-새마을 2016', '20240807', '20240808020000', '부산', '20240808050000', '수원', '30000원'), + (16121, '새마을호 1008', '20240807', '20240808030000', '부산', '20240808070000', '수원', '25000원'), + (16122, 'KTX 108', '20240807', '20240808040000', '부산', '20240808070000', '수원', '48000원'), + (16123, 'ITX-새마을 2017', '20240807', '20240808050000', '부산', '20240808080000', '수원', '30000원'), + (16124, '새마을호 1009', '20240807', '20240808060000', '부산', '20240808100000', '수원', '25000원'), + (16125, 'KTX 109', '20240807', '20240808070000', '부산', '20240808100000', '수원', '48000원'), + (16126, 'ITX-새마을 2018', '20240807', '20240808080000', '부산', '20240808110000', '수원', '30000원'), + (16127, '새마을호 1010', '20240807', '20240808090000', '부산', '20240808130000', '수원', '25000원'), + (16128, 'KTX 110', '20240807', '20240808100000', '부산', '20240808130000', '수원', '48000원'), + (16129, 'ITX-새마을 2019', '20240807', '20240808110000', '부산', '20240808140000', '수원', '30000원'), + (16130, '새마을호 1011', '20240807', '20240808120000', '부산', '20240808160000', '수원', '25000원'), + (16131, 'KTX 111', '20240807', '20240808130000', '부산', '20240808160000', '수원', '48000원'), + (16132, 'ITX-새마을 2020', '20240807', '20240808140000', '부산', '20240808170000', '수원', '30000원'), + (16133, '새마을호 1012', '20240807', '20240808150000', '부산', '20240808190000', '수원', '25000원'), + (16134, 'KTX 112', '20240807', '20240808160000', '부산', '20240808190000', '수원', '48000원'), + (16135, 'ITX-새마을 2021', '20240807', '20240808170000', '부산', '20240808200000', '수원', '30000원'), + (16136, '새마을호 1013', '20240807', '20240808180000', '부산', '20240808220000', '수원', '25000원'), + (16137, 'KTX 113', '20240807', '20240808190000', '부산', '20240808220000', '수원', '48000원'), + (16138, 'ITX-새마을 2022', '20240807', '20240808200000', '부산', '20240808230000', '수원', '30000원'), + (16139, '새마을호 1014', '20240807', '20240808210000', '부산', '20240809010000', '수원', '25000원'), + (16140, 'KTX 114', '20240807', '20240808220000', '부산', '20240809010000', '수원', '48000원'), + (16141, 'ITX-새마을 2023', '20240807', '20240808230000', '부산', '20240809020000', '수원', '30000원'), + (16142, '새마을호 1015', '20240807', '20240809000000', '부산', '20240809040000', '수원', '25000원'), + (16143, 'KTX 115', '20240807', '20240809010000', '부산', '20240809040000', '수원', '48000원'), + (16144, 'ITX-새마을 2024', '20240807', '20240809020000', '부산', '20240809050000', '수원', '30000원'), + (16145, '새마을호 1016', '20240807', '20240809030000', '부산', '20240809070000', '수원', '25000원'), + (16146, 'KTX 116', '20240807', '20240809040000', '부산', '20240809070000', '수원', '48000원'), + (16147, 'ITX-새마을 2025', '20240807', '20240809050000', '부산', '20240809080000', '수원', '30000원'), + (16148, '새마을호 1017', '20240807', '20240809060000', '부산', '20240809100000', '수원', '25000원'), + (16149, 'KTX 117', '20240807', '20240809070000', '부산', '20240809100000', '수원', '48000원');