diff --git a/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java b/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java new file mode 100644 index 00000000..c6859f53 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java @@ -0,0 +1,99 @@ +package com.talkka.server.bookmark.controller; + +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.talkka.server.bookmark.dto.BookmarkReqDto; +import com.talkka.server.bookmark.dto.BookmarkRespDto; +import com.talkka.server.bookmark.exception.BookmarkNotFoundException; +import com.talkka.server.bookmark.exception.BookmarkUserNotFoundException; +import com.talkka.server.bookmark.exception.enums.InvalidTransportTypeEnumException; +import com.talkka.server.bookmark.service.BookmarkService; +import com.talkka.server.oauth.domain.OAuth2UserInfo; +import com.talkka.server.review.exception.ContentAccessException; + +import lombok.RequiredArgsConstructor; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/bookmark") +public class BookmarkController { + private final BookmarkService bookmarkService; + + // 본인이 작성한 북마크 리스트만 조회 + @GetMapping("") + public ResponseEntity getBookmarkList(@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo) { + List bookmarks = bookmarkService.getBookmarkByUserId(oAuth2UserInfo.getUserId()); + return ResponseEntity.ok(bookmarks); + } + + @GetMapping("/{bookmarkId}") + public ResponseEntity getBookmark(@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo, + @PathVariable Long bookmarkId) { + ResponseEntity response; + try { + BookmarkRespDto bookmark = bookmarkService.getBookmarkById(oAuth2UserInfo.getUserId(), bookmarkId); + response = ResponseEntity.ok(bookmark); + } catch (BookmarkNotFoundException | BookmarkUserNotFoundException exception) { + response = ResponseEntity.badRequest().body(exception.getMessage()); + } catch (ContentAccessException exception) { + response = ResponseEntity.status(HttpStatus.FORBIDDEN).body(exception.getMessage()); + } + return response; + } + + @PostMapping("") + public ResponseEntity createBookmark(@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo, + BookmarkReqDto bookmarkReqDto) { + ResponseEntity response; + try { + BookmarkRespDto bookmark = bookmarkService.createBookmark(bookmarkReqDto, oAuth2UserInfo.getUserId()); + return ResponseEntity.ok(bookmark); + } catch (BookmarkUserNotFoundException | InvalidTransportTypeEnumException exception) { + response = ResponseEntity.badRequest().body(exception.getMessage()); + } + return response; + } + + @PutMapping("{bookmarkId}") + public ResponseEntity updateBookmark(@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo, + BookmarkReqDto bookmarkReqDto, @PathVariable Long bookmarkId) { + ResponseEntity response; + try { + BookmarkRespDto bookmark = bookmarkService.updateBookmark(bookmarkReqDto, oAuth2UserInfo.getUserId(), + bookmarkId); + response = ResponseEntity.ok(bookmark); + } catch (BookmarkNotFoundException | BookmarkUserNotFoundException + | InvalidTransportTypeEnumException exception) { + response = ResponseEntity.badRequest().body(exception.getMessage()); + } catch (ContentAccessException exception) { + response = ResponseEntity.status(HttpStatus.FORBIDDEN).body(exception.getMessage()); + } + return response; + } + + @DeleteMapping("/{bookmarkId}") + public ResponseEntity deleteBookmark(@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo, + @PathVariable Long bookmarkId) { + ResponseEntity response; + try { + bookmarkService.deleteBookmark(oAuth2UserInfo.getUserId(), bookmarkId); + response = ResponseEntity.ok().build(); + } catch (BookmarkNotFoundException | BookmarkUserNotFoundException exception) { + response = ResponseEntity.badRequest().body(exception.getMessage()); + } catch (ContentAccessException exception) { + response = ResponseEntity.status(HttpStatus.FORBIDDEN).body(exception.getMessage()); + } + return response; + } +} diff --git a/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkDetailRespDto.java b/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkDetailRespDto.java index 92394719..285b0d23 100644 --- a/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkDetailRespDto.java +++ b/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkDetailRespDto.java @@ -1,11 +1,12 @@ package com.talkka.server.bookmark.dto; import com.talkka.server.bookmark.dao.BookmarkDetailEntity; +import com.talkka.server.bookmark.enums.TransportType; import com.talkka.server.subway.enums.Updown; public record BookmarkDetailRespDto( Integer seq, - String type, + TransportType type, Long subwayStationId, Updown subwayUpdown, Long busRouteStationId @@ -13,7 +14,7 @@ public record BookmarkDetailRespDto( public static BookmarkDetailRespDto of(BookmarkDetailEntity entity) { return new BookmarkDetailRespDto( entity.getSeq(), - entity.getType().getType(), + entity.getType(), entity.getSubwayStationId(), entity.getSubwayUpdown(), entity.getBusRouteStationId() diff --git a/server/src/main/java/com/talkka/server/bookmark/enums/TransportType.java b/server/src/main/java/com/talkka/server/bookmark/enums/TransportType.java index 456cdb99..443cb812 100644 --- a/server/src/main/java/com/talkka/server/bookmark/enums/TransportType.java +++ b/server/src/main/java/com/talkka/server/bookmark/enums/TransportType.java @@ -6,12 +6,9 @@ @Getter public enum TransportType { - BUS("bus"), SUBWAY("subway"); + BUS, SUBWAY; - private final String type; - - TransportType(String type) { - this.type = type; + TransportType() { } public static TransportType valueOfEnumString(String enumValue) { diff --git a/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java b/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java index 9b5ed087..f16f5964 100644 --- a/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java +++ b/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java @@ -11,6 +11,7 @@ import com.talkka.server.bookmark.dto.BookmarkRespDto; import com.talkka.server.bookmark.exception.BookmarkNotFoundException; import com.talkka.server.bookmark.exception.BookmarkUserNotFoundException; +import com.talkka.server.bookmark.exception.enums.InvalidTransportTypeEnumException; import com.talkka.server.common.validator.ContentAccessValidator; import com.talkka.server.review.exception.ContentAccessException; import com.talkka.server.user.dao.UserEntity; @@ -27,39 +28,48 @@ public class BookmarkService { private final UserRepository userRepository; private final ContentAccessValidator contentAccessValidator; - @Transactional - public BookmarkRespDto createBookmark(BookmarkReqDto dto, Long userId) throws BookmarkUserNotFoundException { + public BookmarkRespDto getBookmarkById(Long userId, Long bookmarkId) throws + BookmarkNotFoundException, + BookmarkUserNotFoundException, + ContentAccessException { + // 본인이 작성한 북마크만 조회 하도록 변경 UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); - BookmarkEntity bookmark = dto.toEntity(user); - dto.details().stream() - .map(detail -> detail.toEntity(bookmark)) - .forEach(bookmark.getDetails()::add); - return BookmarkRespDto.of(bookmarkRepository.save(bookmark)); - } - - public BookmarkRespDto getByBookmarkId(Long id) throws BookmarkNotFoundException { - BookmarkEntity bookmark = bookmarkRepository.findById(id).orElseThrow(BookmarkUserNotFoundException::new); + BookmarkEntity bookmark = bookmarkRepository.findById(bookmarkId) + .orElseThrow(BookmarkUserNotFoundException::new); + contentAccessValidator.validateOwnerContentAccess(userId, user.getGrade(), bookmark.getUser().getId()); return BookmarkRespDto.of(bookmark); } - public List getByUserId(Long userId) { + public List getBookmarkByUserId(Long userId) { return bookmarkRepository.findByUserId(userId).stream() .map(BookmarkRespDto::of) .toList(); } @Transactional - public BookmarkRespDto updateBookmark(BookmarkReqDto dto, Long bookmarkId, Long userId) throws + public BookmarkRespDto createBookmark(BookmarkReqDto dto, Long userId) throws BookmarkUserNotFoundException, + InvalidTransportTypeEnumException { + UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); + BookmarkEntity bookmark = dto.toEntity(user); + dto.details().stream() + .map(detail -> detail.toEntity(bookmark)) + .forEach(bookmark.getDetails()::add); + return BookmarkRespDto.of(bookmarkRepository.save(bookmark)); + } + + @Transactional + public BookmarkRespDto updateBookmark(BookmarkReqDto dto, Long userId, Long bookmarkId) throws BookmarkUserNotFoundException, BookmarkNotFoundException, + InvalidTransportTypeEnumException, ContentAccessException { UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); BookmarkEntity bookmark = bookmarkRepository.findById(bookmarkId) - .orElseThrow(BookmarkUserNotFoundException::new); + .orElseThrow(BookmarkNotFoundException::new); // 작성자거나 관리자가 아니면 ContentAccessException 발생 - contentAccessValidator.validateOwnerContentAccess(user.getId(), user.getGrade(), bookmark.getId()); + contentAccessValidator.validateOwnerContentAccess(user.getId(), user.getGrade(), bookmark.getUser().getId()); // 기존 북마크 상세를 전부 지우고 전체를 새로 저장함 bookmarkDetailRepository.deleteByBookmarkId(bookmarkId); @@ -69,7 +79,7 @@ public BookmarkRespDto updateBookmark(BookmarkReqDto dto, Long bookmarkId, Long } @Transactional - public Long deleteBookmark(Long bookmarkId, Long userId) throws + public Long deleteBookmark(Long userId, Long bookmarkId) throws BookmarkUserNotFoundException, BookmarkNotFoundException {