-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- BookmarkService - BookmarkRepository - BookmarkDetailRepository
- Loading branch information
Showing
14 changed files
with
222 additions
and
46 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
server/src/main/java/com/talkka/server/bookmark/dao/BookmarkDetailRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.talkka.server.bookmark.dao; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookmarkDetailRepository extends JpaRepository<BookmarkDetailEntity, Long> { | ||
List<BookmarkDetailEntity> findByBookmarkId(Long bookmarkId); | ||
|
||
List<BookmarkDetailEntity> deleteByBookmarkId(Long bookmarkId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
server/src/main/java/com/talkka/server/bookmark/dao/BookmarkRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.talkka.server.bookmark.dao; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookmarkRepository extends JpaRepository<BookmarkEntity, Long> { | ||
List<BookmarkEntity> findByUserId(Long userId); | ||
|
||
boolean existsByIdAndUserId(Long bookmarkId, Long userId); | ||
} |
16 changes: 0 additions & 16 deletions
16
server/src/main/java/com/talkka/server/bookmark/dao/dto/BookmarkCreateDto.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
server/src/main/java/com/talkka/server/bookmark/dao/dto/BookmarkRespDto.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
...okmark/dao/dto/BookmarkDetailRespDto.java → ...r/bookmark/dto/BookmarkDetailRespDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
server/src/main/java/com/talkka/server/bookmark/dto/BookmarkReqDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.talkka.server.bookmark.dto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.talkka.server.bookmark.dao.BookmarkEntity; | ||
import com.talkka.server.user.dao.UserEntity; | ||
|
||
public record BookmarkReqDto( | ||
String name, | ||
List<BookmarkDetailReqDto> details | ||
) { | ||
public BookmarkEntity toEntity(UserEntity user) { | ||
return BookmarkEntity.builder() | ||
.name(name) | ||
.user(user) | ||
.details(new ArrayList<>()) | ||
.build(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
server/src/main/java/com/talkka/server/bookmark/dto/BookmarkRespDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.talkka.server.bookmark.dto; | ||
|
||
import java.util.List; | ||
|
||
import com.talkka.server.bookmark.dao.BookmarkEntity; | ||
import com.talkka.server.user.dto.UserDto; | ||
import com.talkka.server.user.dto.UserRespDto; | ||
|
||
public record BookmarkRespDto( | ||
Long id, | ||
String name, | ||
UserRespDto user, | ||
List<BookmarkDetailRespDto> details | ||
) { | ||
public static BookmarkRespDto of(BookmarkEntity bookmark) { | ||
return new BookmarkRespDto( | ||
bookmark.getId(), | ||
bookmark.getName(), | ||
UserRespDto.of(UserDto.of(bookmark.getUser())), | ||
bookmark.getDetails().stream() | ||
.map(BookmarkDetailRespDto::of) | ||
.toList() | ||
); | ||
} | ||
} |
21 changes: 20 additions & 1 deletion
21
server/src/main/java/com/talkka/server/bookmark/enums/TransportType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
package com.talkka.server.bookmark.enums; | ||
|
||
import com.talkka.server.bookmark.exception.enums.InvalidTransportTypeEnumException; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum TransportType { | ||
BUS, SUBWAY | ||
BUS("bus"), SUBWAY("subway"); | ||
|
||
private final String type; | ||
|
||
TransportType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public static TransportType valueOfEnumString(String enumValue) { | ||
try { | ||
return TransportType.valueOf(enumValue); | ||
} catch (IllegalArgumentException exception) { | ||
throw new InvalidTransportTypeEnumException(); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
server/src/main/java/com/talkka/server/bookmark/exception/BookmarkNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.talkka.server.bookmark.exception; | ||
|
||
public class BookmarkNotFoundException extends RuntimeException { | ||
private static final String MESSAGE = "존재하지 않는 북마크 입니다."; | ||
|
||
public BookmarkNotFoundException() { | ||
super(MESSAGE); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
server/src/main/java/com/talkka/server/bookmark/exception/BookmarkUserNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.talkka.server.bookmark.exception; | ||
|
||
public class BookmarkUserNotFoundException extends RuntimeException { | ||
private static final String MESSAGE = "존재하지 않는 사용자 입니다."; | ||
|
||
public BookmarkUserNotFoundException() { | ||
super(MESSAGE); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...in/java/com/talkka/server/bookmark/exception/enums/InvalidTransportTypeEnumException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.talkka.server.bookmark.exception.enums; | ||
|
||
import com.talkka.server.common.exception.InvalidTypeException; | ||
|
||
public class InvalidTransportTypeEnumException extends InvalidTypeException { | ||
private static final String MESSAGE = "잘못된 교통수단 구분입니다."; | ||
|
||
public InvalidTransportTypeEnumException() { | ||
super(MESSAGE); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.talkka.server.bookmark.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.talkka.server.bookmark.dao.BookmarkDetailRepository; | ||
import com.talkka.server.bookmark.dao.BookmarkEntity; | ||
import com.talkka.server.bookmark.dao.BookmarkRepository; | ||
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.common.validator.ContentAccessValidator; | ||
import com.talkka.server.review.exception.ContentAccessException; | ||
import com.talkka.server.user.dao.UserEntity; | ||
import com.talkka.server.user.dao.UserRepository; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BookmarkService { | ||
private final BookmarkRepository bookmarkRepository; | ||
private final BookmarkDetailRepository bookmarkDetailRepository; | ||
private final UserRepository userRepository; | ||
private final ContentAccessValidator contentAccessValidator; | ||
|
||
@Transactional | ||
public BookmarkRespDto createBookmark(BookmarkReqDto dto, Long userId) throws BookmarkUserNotFoundException { | ||
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); | ||
return BookmarkRespDto.of(bookmark); | ||
} | ||
|
||
public List<BookmarkRespDto> getByUserId(Long userId) { | ||
return bookmarkRepository.findByUserId(userId).stream() | ||
.map(BookmarkRespDto::of) | ||
.toList(); | ||
} | ||
|
||
@Transactional | ||
public BookmarkRespDto updateBookmark(BookmarkReqDto dto, Long bookmarkId, Long userId) throws | ||
BookmarkUserNotFoundException, | ||
BookmarkNotFoundException, | ||
ContentAccessException { | ||
|
||
UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); | ||
BookmarkEntity bookmark = bookmarkRepository.findById(bookmarkId) | ||
.orElseThrow(BookmarkUserNotFoundException::new); | ||
|
||
// 작성자거나 관리자가 아니면 ContentAccessException 발생 | ||
contentAccessValidator.validateOwnerContentAccess(user.getId(), user.getGrade(), bookmark.getId()); | ||
|
||
// 기존 북마크 상세를 전부 지우고 전체를 새로 저장함 | ||
bookmarkDetailRepository.deleteByBookmarkId(bookmarkId); | ||
bookmark.updateBookmark(dto); | ||
|
||
return BookmarkRespDto.of(bookmarkRepository.save(bookmark)); | ||
} | ||
|
||
@Transactional | ||
public Long deleteBookmark(Long bookmarkId, Long userId) throws | ||
BookmarkUserNotFoundException, | ||
BookmarkNotFoundException { | ||
|
||
UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); | ||
BookmarkEntity bookmark = bookmarkRepository.findById(bookmarkId) | ||
.orElseThrow(BookmarkUserNotFoundException::new); | ||
|
||
// 작성자거나 관리자가 아니면 ContentAccessException 발생 | ||
contentAccessValidator.validateOwnerContentAccess(user.getId(), user.getGrade(), bookmark.getId()); | ||
|
||
// 북마크와 북마크 상세를 삭제 | ||
bookmarkRepository.delete(bookmark); | ||
|
||
return bookmarkId; | ||
} | ||
} |