diff --git a/src/main/java/toy/bookchat/bookchat/db_module/agony/repository/query/AgonyQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/db_module/agony/repository/query/AgonyQueryRepositoryImpl.java index 597e1c70..9b3569a1 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/agony/repository/query/AgonyQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/agony/repository/query/AgonyQueryRepositoryImpl.java @@ -2,9 +2,9 @@ import static toy.bookchat.bookchat.db_module.agony.QAgonyEntity.agonyEntity; import static toy.bookchat.bookchat.db_module.bookshelf.QBookShelfEntity.bookShelfEntity; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.extractOrderSpecifierFrom; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.numberBasedPagination; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.toSlice; +import static toy.bookchat.bookchat.support.RepositorySupport.extractOrderSpecifierFrom; +import static toy.bookchat.bookchat.support.RepositorySupport.numberBasedPagination; +import static toy.bookchat.bookchat.support.RepositorySupport.toSlice; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -18,74 +18,74 @@ @Repository public class AgonyQueryRepositoryImpl implements AgonyQueryRepository { - private final JPAQueryFactory queryFactory; + private final JPAQueryFactory queryFactory; - public AgonyQueryRepositoryImpl(JPAQueryFactory queryFactory) { - this.queryFactory = queryFactory; - } + public AgonyQueryRepositoryImpl(JPAQueryFactory queryFactory) { + this.queryFactory = queryFactory; + } - @Override - public Optional findUserBookShelfAgony(Long bookShelfId, Long agonyId, Long userId) { - return Optional.ofNullable( - queryFactory.select(agonyEntity) - .from(agonyEntity) - .join(bookShelfEntity) - .on(bookShelfEntity.id.eq(agonyEntity.bookShelfId) - .and(bookShelfEntity.userId.eq(userId))) - .where(agonyEntity.id.eq(agonyId) - .and(agonyEntity.bookShelfId.eq(bookShelfId))) - .fetchOne()); - } - - @Override - public Slice findUserBookShelfSliceOfAgonies(Long bookShelfId, Long userId, - Pageable pageable, - Long postCursorId) { - List contents = queryFactory.select(agonyEntity) + @Override + public Optional findUserBookShelfAgony(Long bookShelfId, Long agonyId, Long userId) { + return Optional.ofNullable( + queryFactory.select(agonyEntity) .from(agonyEntity) .join(bookShelfEntity) - .on( - agonyEntity.bookShelfId.eq(bookShelfEntity.id) - .and(bookShelfEntity.id.eq(bookShelfId)) - .and(bookShelfEntity.userId.eq(userId)) - ) - .where(numberBasedPagination(agonyEntity, agonyEntity.id, postCursorId, pageable)) - .limit(pageable.getPageSize()) - .orderBy(extractOrderSpecifierFrom(agonyEntity, pageable)) - .fetch(); + .on(bookShelfEntity.id.eq(agonyEntity.bookShelfId) + .and(bookShelfEntity.userId.eq(userId))) + .where(agonyEntity.id.eq(agonyId) + .and(agonyEntity.bookShelfId.eq(bookShelfId))) + .fetchOne()); + } - return toSlice(contents, pageable); - } + @Override + public Slice findUserBookShelfSliceOfAgonies(Long bookShelfId, Long userId, + Pageable pageable, + Long postCursorId) { + List contents = queryFactory.select(agonyEntity) + .from(agonyEntity) + .join(bookShelfEntity) + .on( + agonyEntity.bookShelfId.eq(bookShelfEntity.id) + .and(bookShelfEntity.id.eq(bookShelfId)) + .and(bookShelfEntity.userId.eq(userId)) + ) + .where(numberBasedPagination(agonyEntity, agonyEntity.id, postCursorId, pageable)) + .limit(pageable.getPageSize()) + .orderBy(extractOrderSpecifierFrom(agonyEntity, pageable)) + .fetch(); - @Override - public void deleteByAgoniesIds(Long bookShelfId, Long userId, List agoniesIds) { - queryFactory.delete(agonyEntity) - .where(agonyEntity.bookShelfId.in( - JPAExpressions.select(bookShelfEntity.id) - .from(bookShelfEntity) - .where(bookShelfEntity.userId.eq(userId))), - agonyEntity.id.in(agoniesIds) - ).execute(); - } + return toSlice(contents, pageable); + } - @Override - public void deleteAllByUserId(Long userId) { - queryFactory.delete(agonyEntity) - .where(agonyEntity.bookShelfId.in( + @Override + public void deleteByAgoniesIds(Long bookShelfId, Long userId, List agoniesIds) { + queryFactory.delete(agonyEntity) + .where(agonyEntity.bookShelfId.in( JPAExpressions.select(bookShelfEntity.id) .from(bookShelfEntity) - .where(bookShelfEntity.userId.eq(userId)) - )).execute(); - } + .where(bookShelfEntity.userId.eq(userId))), + agonyEntity.id.in(agoniesIds) + ).execute(); + } - @Override - public void deleteByBookShelfIdAndUserId(Long bookShelfId, Long userId) { - queryFactory.delete(agonyEntity) - .where(agonyEntity.bookShelfId.in( - JPAExpressions.select(bookShelfEntity.id) - .from(bookShelfEntity) - .where(bookShelfEntity.id.eq(bookShelfId) - .and(bookShelfEntity.userId.eq(userId))) - )).execute(); - } + @Override + public void deleteAllByUserId(Long userId) { + queryFactory.delete(agonyEntity) + .where(agonyEntity.bookShelfId.in( + JPAExpressions.select(bookShelfEntity.id) + .from(bookShelfEntity) + .where(bookShelfEntity.userId.eq(userId)) + )).execute(); + } + + @Override + public void deleteByBookShelfIdAndUserId(Long bookShelfId, Long userId) { + queryFactory.delete(agonyEntity) + .where(agonyEntity.bookShelfId.in( + JPAExpressions.select(bookShelfEntity.id) + .from(bookShelfEntity) + .where(bookShelfEntity.id.eq(bookShelfId) + .and(bookShelfEntity.userId.eq(userId))) + )).execute(); + } } diff --git a/src/main/java/toy/bookchat/bookchat/db_module/agonyrecord/repository/query/AgonyRecordQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/db_module/agonyrecord/repository/query/AgonyRecordQueryRepositoryImpl.java index ecf3721b..1ece177f 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/agonyrecord/repository/query/AgonyRecordQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/agonyrecord/repository/query/AgonyRecordQueryRepositoryImpl.java @@ -3,9 +3,9 @@ import static toy.bookchat.bookchat.db_module.agony.QAgonyEntity.agonyEntity; import static toy.bookchat.bookchat.db_module.agonyrecord.QAgonyRecordEntity.agonyRecordEntity; import static toy.bookchat.bookchat.db_module.bookshelf.QBookShelfEntity.bookShelfEntity; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.extractOrderSpecifierFrom; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.numberBasedPagination; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.toSlice; +import static toy.bookchat.bookchat.support.RepositorySupport.extractOrderSpecifierFrom; +import static toy.bookchat.bookchat.support.RepositorySupport.numberBasedPagination; +import static toy.bookchat.bookchat.support.RepositorySupport.toSlice; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; diff --git a/src/main/java/toy/bookchat/bookchat/db_module/bookshelf/repository/query/BookShelfQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/db_module/bookshelf/repository/query/BookShelfQueryRepositoryImpl.java index f097b08a..30c68830 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/bookshelf/repository/query/BookShelfQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/bookshelf/repository/query/BookShelfQueryRepositoryImpl.java @@ -2,7 +2,7 @@ import static toy.bookchat.bookchat.db_module.book.QBookEntity.bookEntity; import static toy.bookchat.bookchat.db_module.bookshelf.QBookShelfEntity.bookShelfEntity; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.extractOrderSpecifierFrom; +import static toy.bookchat.bookchat.support.RepositorySupport.extractOrderSpecifierFrom; import com.querydsl.jpa.impl.JPAQueryFactory; import java.time.LocalDate; diff --git a/src/main/java/toy/bookchat/bookchat/db_module/chat/repository/query/ChatQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/db_module/chat/repository/query/ChatQueryRepositoryImpl.java index e0688d26..5372057e 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/chat/repository/query/ChatQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/chat/repository/query/ChatQueryRepositoryImpl.java @@ -3,9 +3,9 @@ import static toy.bookchat.bookchat.db_module.chat.QChatEntity.chatEntity; import static toy.bookchat.bookchat.db_module.participant.QParticipantEntity.participantEntity; import static toy.bookchat.bookchat.db_module.user.QUserEntity.userEntity; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.extractOrderSpecifierFrom; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.numberBasedPagination; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.toSlice; +import static toy.bookchat.bookchat.support.RepositorySupport.extractOrderSpecifierFrom; +import static toy.bookchat.bookchat.support.RepositorySupport.numberBasedPagination; +import static toy.bookchat.bookchat.support.RepositorySupport.toSlice; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -18,37 +18,37 @@ @Repository public class ChatQueryRepositoryImpl implements ChatQueryRepository { - private final JPAQueryFactory queryFactory; + private final JPAQueryFactory queryFactory; - public ChatQueryRepositoryImpl(JPAQueryFactory queryFactory) { - this.queryFactory = queryFactory; - } + public ChatQueryRepositoryImpl(JPAQueryFactory queryFactory) { + this.queryFactory = queryFactory; + } - @Override - public Slice getChatRoomChats(Long roomId, Long postCursorId, Pageable pageable, Long userId) { - return toSlice(queryFactory.select(chatEntity) - .from(chatEntity) - .where(chatEntity.chatRoomId.eq(JPAExpressions.select(participantEntity.chatRoomId) - .from(participantEntity) - .where(participantEntity.userId.eq(userId) - .and(participantEntity.chatRoomId.eq(roomId)))), - numberBasedPagination(chatEntity, chatEntity.id, postCursorId, pageable) - ) - .limit(pageable.getPageSize()) - .orderBy(extractOrderSpecifierFrom(chatEntity, pageable)).fetch(), pageable); - } + @Override + public Slice getChatRoomChats(Long roomId, Long postCursorId, Pageable pageable, Long userId) { + return toSlice(queryFactory.select(chatEntity) + .from(chatEntity) + .where(chatEntity.chatRoomId.eq(JPAExpressions.select(participantEntity.chatRoomId) + .from(participantEntity) + .where(participantEntity.userId.eq(userId) + .and(participantEntity.chatRoomId.eq(roomId)))), + numberBasedPagination(chatEntity, chatEntity.id, postCursorId, pageable) + ) + .limit(pageable.getPageSize()) + .orderBy(extractOrderSpecifierFrom(chatEntity, pageable)).fetch(), pageable); + } - @Override - public Optional getUserChatRoomChat(Long chatId, Long userId) { - return Optional.ofNullable(queryFactory.select(chatEntity) - .from(chatEntity) - .join(userEntity).on(chatEntity.userId.eq(userEntity.id)) - .where(chatEntity.id.eq(chatId) - .and(chatEntity.chatRoomId.in( - JPAExpressions.select(participantEntity.chatRoomId) - .from(participantEntity) - .where(participantEntity.userId.eq(userId))) - )) - .fetchOne()); - } + @Override + public Optional getUserChatRoomChat(Long chatId, Long userId) { + return Optional.ofNullable(queryFactory.select(chatEntity) + .from(chatEntity) + .join(userEntity).on(chatEntity.userId.eq(userEntity.id)) + .where(chatEntity.id.eq(chatId) + .and(chatEntity.chatRoomId.in( + JPAExpressions.select(participantEntity.chatRoomId) + .from(participantEntity) + .where(participantEntity.userId.eq(userId))) + )) + .fetchOne()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java index 26e5df35..40e1d5c2 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java @@ -10,7 +10,7 @@ import static toy.bookchat.bookchat.db_module.chatroom.QHashTagEntity.hashTagEntity; import static toy.bookchat.bookchat.db_module.participant.QParticipantEntity.participantEntity; import static toy.bookchat.bookchat.db_module.user.QUserEntity.userEntity; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.toSlice; +import static toy.bookchat.bookchat.support.RepositorySupport.toSlice; import static toy.bookchat.bookchat.domain.participant.ParticipantStatus.HOST; import com.querydsl.core.types.dsl.BooleanExpression; diff --git a/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/ChatRoomsResponseSlice.java b/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/ChatRoomsResponseSlice.java index b7ee867d..5c36cf99 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/ChatRoomsResponseSlice.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/ChatRoomsResponseSlice.java @@ -4,7 +4,7 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import org.springframework.data.domain.Slice; -import toy.bookchat.bookchat.domain.common.CursorMeta; +import toy.bookchat.bookchat.support.CursorMeta; @Getter @EqualsAndHashCode(of = "chatRoomResponseList") diff --git a/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/UserChatRoomsResponseSlice.java b/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/UserChatRoomsResponseSlice.java index e7a87307..cf1de4b5 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/UserChatRoomsResponseSlice.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/chatroom/repository/query/dto/UserChatRoomsResponseSlice.java @@ -3,7 +3,7 @@ import java.util.List; import lombok.Getter; import org.springframework.data.domain.Slice; -import toy.bookchat.bookchat.domain.common.CursorMeta; +import toy.bookchat.bookchat.support.CursorMeta; @Getter public class UserChatRoomsResponseSlice { diff --git a/src/main/java/toy/bookchat/bookchat/db_module/scrap/repository/query/ScrapEntityQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/db_module/scrap/repository/query/ScrapEntityQueryRepositoryImpl.java index c1f61157..e39bbef6 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/scrap/repository/query/ScrapEntityQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/scrap/repository/query/ScrapEntityQueryRepositoryImpl.java @@ -2,7 +2,7 @@ import static toy.bookchat.bookchat.db_module.bookshelf.QBookShelfEntity.bookShelfEntity; import static toy.bookchat.bookchat.db_module.scrap.QScrapEntity.scrapEntity; -import static toy.bookchat.bookchat.domain.common.RepositorySupport.toSlice; +import static toy.bookchat.bookchat.support.RepositorySupport.toSlice; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; diff --git a/src/main/java/toy/bookchat/bookchat/db_module/user/UserEntity.java b/src/main/java/toy/bookchat/bookchat/db_module/user/UserEntity.java index c4977ca3..ca5e7dfa 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/user/UserEntity.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/user/UserEntity.java @@ -1,7 +1,7 @@ package toy.bookchat.bookchat.db_module.user; import static javax.persistence.EnumType.STRING; -import static toy.bookchat.bookchat.domain.common.Status.INACTIVE; +import static toy.bookchat.bookchat.support.Status.INACTIVE; import java.util.ArrayList; import java.util.List; @@ -19,7 +19,7 @@ import lombok.Getter; import org.hibernate.annotations.DynamicInsert; import toy.bookchat.bookchat.db_module.BaseEntity; -import toy.bookchat.bookchat.domain.common.Status; +import toy.bookchat.bookchat.support.Status; import toy.bookchat.bookchat.domain.user.ROLE; import toy.bookchat.bookchat.domain.user.ReadingTaste; import toy.bookchat.bookchat.security.oauth.OAuth2Provider; diff --git a/src/main/java/toy/bookchat/bookchat/db_module/user/repository/UserRepository.java b/src/main/java/toy/bookchat/bookchat/db_module/user/repository/UserRepository.java index 3cea6df2..eb142451 100644 --- a/src/main/java/toy/bookchat/bookchat/db_module/user/repository/UserRepository.java +++ b/src/main/java/toy/bookchat/bookchat/db_module/user/repository/UserRepository.java @@ -4,20 +4,20 @@ import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import toy.bookchat.bookchat.db_module.user.UserEntity; -import toy.bookchat.bookchat.domain.common.Status; +import toy.bookchat.bookchat.support.Status; import toy.bookchat.bookchat.security.oauth.OAuth2Provider; public interface UserRepository extends JpaRepository { - Optional findByNameAndStatus(String name, Status status); + Optional findByNameAndStatus(String name, Status status); - Optional findByName(String name); + Optional findByName(String name); - Optional findByEmailAndProvider(String email, OAuth2Provider provider); + Optional findByEmailAndProvider(String email, OAuth2Provider provider); - boolean existsByNickname(String nickname); + boolean existsByNickname(String nickname); - Optional findByIdAndStatus(Long userId, Status status); + Optional findByIdAndStatus(Long userId, Status status); - List findByIdIn(List userIds); + List findByIdIn(List userIds); } diff --git a/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/AgonyController.java b/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/AgonyController.java index aead36f3..3d523d75 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/AgonyController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/AgonyController.java @@ -5,6 +5,7 @@ import java.net.URI; import java.util.List; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import toy.bookchat.bookchat.domain.agony.api.v1.request.CreateBookAgonyRequest; import toy.bookchat.bookchat.domain.agony.api.v1.request.ReviseAgonyRequest; @@ -22,43 +24,88 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController +@RequestMapping("/v1/api/bookshelves/{bookShelfId}/agonies") public class AgonyController { - private final AgonyService agonyService; - - public AgonyController(AgonyService agonyService) { - this.agonyService = agonyService; - } + private final AgonyService agonyService; - @PostMapping("/v1/api/bookshelves/{bookShelfId}/agonies") - public ResponseEntity makeBookAgony(@PathVariable Long bookShelfId, @Valid @RequestBody CreateBookAgonyRequest request, @UserPayload TokenPayload tokenPayload) { - Long agonyId = agonyService.storeBookShelfAgony(request.toTarget(), tokenPayload.getUserId(), bookShelfId); + /** + * 책장에 고민 등록 + * + * @param bookShelfId + * @param request + * @param tokenPayload + * @return + */ + @PostMapping + public ResponseEntity makeBookAgony(@PathVariable Long bookShelfId, + @Valid @RequestBody CreateBookAgonyRequest request, @UserPayload TokenPayload tokenPayload) { + Long agonyId = agonyService.storeBookShelfAgony(request.toTarget(), tokenPayload.getUserId(), bookShelfId); - return ResponseEntity.status(CREATED) - .headers(hs -> hs.setLocation(URI.create("/v1/api/bookshelves/" + bookShelfId + "/agonies/" + agonyId))) - .build(); - } + return ResponseEntity.status(CREATED) + .headers(hs -> hs.setLocation(URI.create("/v1/api/bookshelves/" + bookShelfId + "/agonies/" + agonyId))) + .build(); + } - @GetMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}") - public AgonyResponse searchAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @UserPayload TokenPayload tokenPayload) { + /** + * 고민 조회 + * + * @param bookShelfId + * @param agonyId + * @param tokenPayload + * @return + */ + @GetMapping("/{agonyId}") + public AgonyResponse searchAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, + @UserPayload TokenPayload tokenPayload) { - return AgonyResponse.from(agonyService.searchAgony(bookShelfId, agonyId, tokenPayload.getUserId())); - } + return AgonyResponse.from(agonyService.searchAgony(bookShelfId, agonyId, tokenPayload.getUserId())); + } - @GetMapping("/v1/api/bookshelves/{bookShelfId}/agonies") - public SliceOfAgoniesResponse searchSliceOfAgonies(@PathVariable Long bookShelfId, Long postCursorId, Pageable pageable, @UserPayload TokenPayload tokenPayload) { - return new SliceOfAgoniesResponse(agonyService.searchSliceOfAgonies(bookShelfId, tokenPayload.getUserId(), pageable, postCursorId)); - } + /** + * 고민 리스트 조회 + * + * @param bookShelfId + * @param postCursorId + * @param pageable + * @param tokenPayload + * @return + */ + @GetMapping + public SliceOfAgoniesResponse searchSliceOfAgonies(@PathVariable Long bookShelfId, Long postCursorId, + Pageable pageable, @UserPayload TokenPayload tokenPayload) { + return new SliceOfAgoniesResponse( + agonyService.searchSliceOfAgonies(bookShelfId, tokenPayload.getUserId(), pageable, postCursorId)); + } - @DeleteMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agoniesIds}") - public void deleteAgony(@PathVariable Long bookShelfId, @PathVariable List agoniesIds, @UserPayload TokenPayload tokenPayload) { + /** + * 고민 삭제 + * + * @param bookShelfId + * @param agoniesIds + * @param tokenPayload + */ + @DeleteMapping("/{agoniesIds}") + public void deleteAgony(@PathVariable Long bookShelfId, @PathVariable List agoniesIds, + @UserPayload TokenPayload tokenPayload) { - agonyService.deleteAgony(bookShelfId, agoniesIds, tokenPayload.getUserId()); - } + agonyService.deleteAgony(bookShelfId, agoniesIds, tokenPayload.getUserId()); + } - @PutMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}") - public void reviseAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @Valid @RequestBody ReviseAgonyRequest request, @UserPayload TokenPayload tokenPayload) { - agonyService.reviseAgony(bookShelfId, agonyId, tokenPayload.getUserId(), request.toTarget()); - } + /** + * 고민 수정 + * + * @param bookShelfId + * @param agonyId + * @param request + * @param tokenPayload + */ + @PutMapping("/{agonyId}") + public void reviseAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, + @Valid @RequestBody ReviseAgonyRequest request, @UserPayload TokenPayload tokenPayload) { + agonyService.reviseAgony(bookShelfId, agonyId, tokenPayload.getUserId(), request.toTarget()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/response/SliceOfAgoniesResponse.java b/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/response/SliceOfAgoniesResponse.java index 9817e121..c39dbc0c 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/response/SliceOfAgoniesResponse.java +++ b/src/main/java/toy/bookchat/bookchat/domain/agony/api/v1/response/SliceOfAgoniesResponse.java @@ -5,20 +5,20 @@ import lombok.Getter; import org.springframework.data.domain.Slice; import toy.bookchat.bookchat.domain.agony.Agony; -import toy.bookchat.bookchat.domain.common.CursorMeta; +import toy.bookchat.bookchat.support.CursorMeta; @Getter public class SliceOfAgoniesResponse { - private List agonyResponseList; - private CursorMeta cursorMeta; + private List agonyResponseList; + private CursorMeta cursorMeta; - public SliceOfAgoniesResponse(Slice slice) { - this.cursorMeta = new CursorMeta<>(slice, Agony::getId); - this.agonyResponseList = from(slice.getContent()); - } + public SliceOfAgoniesResponse(Slice slice) { + this.cursorMeta = new CursorMeta<>(slice, Agony::getId); + this.agonyResponseList = from(slice.getContent()); + } - private List from(List content) { - return content.stream().map(AgonyResponse::from).collect(Collectors.toList()); - } + private List from(List content) { + return content.stream().map(AgonyResponse::from).collect(Collectors.toList()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/AgonyRecordController.java b/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/AgonyRecordController.java index bb0fead1..f477c3a2 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/AgonyRecordController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/AgonyRecordController.java @@ -4,6 +4,7 @@ import java.net.URI; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.http.ResponseEntity; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import toy.bookchat.bookchat.domain.agonyrecord.AgonyRecord; import toy.bookchat.bookchat.domain.agonyrecord.api.v1.request.CreateAgonyRecordRequest; @@ -23,50 +25,56 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController +@RequestMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}/records") public class AgonyRecordController { - private final AgonyRecordService agonyRecordService; - - public AgonyRecordController(AgonyRecordService agonyRecordService) { - this.agonyRecordService = agonyRecordService; - } + private final AgonyRecordService agonyRecordService; - @PostMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}/records") - public ResponseEntity addAgonyRecordOnBookAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @Valid @RequestBody CreateAgonyRecordRequest createAgonyRecordRequest, - @UserPayload TokenPayload tokenPayload) { - Long agonyRecordId = agonyRecordService.storeAgonyRecord(bookShelfId, createAgonyRecordRequest.toTarget(), tokenPayload.getUserId(), agonyId); + @PostMapping + public ResponseEntity addAgonyRecordOnBookAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, + @Valid @RequestBody CreateAgonyRecordRequest createAgonyRecordRequest, @UserPayload TokenPayload tokenPayload) { + Long agonyRecordId = agonyRecordService.storeAgonyRecord(bookShelfId, createAgonyRecordRequest.toTarget(), + tokenPayload.getUserId(), agonyId); - return ResponseEntity.status(CREATED) - .headers(hs -> hs.setLocation(URI.create("/v1/api/bookshelves/" + bookShelfId + "/agonies/" + agonyId + "/records/" + agonyRecordId))) - .build(); - } + return ResponseEntity.status(CREATED) + .headers(hs -> hs.setLocation( + URI.create("/v1/api/bookshelves/" + bookShelfId + "/agonies/" + agonyId + "/records/" + agonyRecordId))) + .build(); + } - @GetMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}/records") - public SliceOfAgonyRecordsResponse getAgonyRecordsOnBookAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, Long postCursorId, @UserPayload TokenPayload tokenPayload, - Pageable pageable) { - Slice agonyRecordSlice = agonyRecordService.searchPageOfAgonyRecords(bookShelfId, agonyId, tokenPayload.getUserId(), pageable, postCursorId); + @GetMapping + public SliceOfAgonyRecordsResponse getAgonyRecordsOnBookAgony(@PathVariable Long bookShelfId, + @PathVariable Long agonyId, Long postCursorId, @UserPayload TokenPayload tokenPayload, Pageable pageable) { + Slice agonyRecordSlice = agonyRecordService.searchPageOfAgonyRecords(bookShelfId, agonyId, + tokenPayload.getUserId(), pageable, postCursorId); - return new SliceOfAgonyRecordsResponse(agonyRecordSlice); - } + return new SliceOfAgonyRecordsResponse(agonyRecordSlice); + } - @GetMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}/records/{recordId}") - public AgonyRecordResponse getAgonyRecordsOnBookAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @PathVariable Long recordId, @UserPayload TokenPayload tokenPayload) { - AgonyRecord agonyRecord = agonyRecordService.searchAgonyRecord(bookShelfId, agonyId, recordId, tokenPayload.getUserId()); + @GetMapping("/{recordId}") + public AgonyRecordResponse getAgonyRecordsOnBookAgony(@PathVariable Long bookShelfId, @PathVariable Long agonyId, + @PathVariable Long recordId, @UserPayload TokenPayload tokenPayload) { + AgonyRecord agonyRecord = agonyRecordService.searchAgonyRecord(bookShelfId, agonyId, recordId, + tokenPayload.getUserId()); - return AgonyRecordResponse.from(agonyRecord); - } + return AgonyRecordResponse.from(agonyRecord); + } - @DeleteMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}/records/{recordId}") - public void deleteAgonyRecord(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @PathVariable Long recordId, @UserPayload TokenPayload tokenPayload) { + @DeleteMapping("/{recordId}") + public void deleteAgonyRecord(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @PathVariable Long recordId, + @UserPayload TokenPayload tokenPayload) { - agonyRecordService.deleteAgonyRecord(bookShelfId, agonyId, recordId, tokenPayload.getUserId()); - } + agonyRecordService.deleteAgonyRecord(bookShelfId, agonyId, recordId, tokenPayload.getUserId()); + } - @PutMapping("/v1/api/bookshelves/{bookShelfId}/agonies/{agonyId}/records/{recordId}") - public void reviseAgonyRecord(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @PathVariable Long recordId, @Valid @RequestBody ReviseAgonyRecordRequest reviseAgonyRecordRequest, - @UserPayload TokenPayload tokenPayload) { + @PutMapping("/{recordId}") + public void reviseAgonyRecord(@PathVariable Long bookShelfId, @PathVariable Long agonyId, @PathVariable Long recordId, + @Valid @RequestBody ReviseAgonyRecordRequest reviseAgonyRecordRequest, @UserPayload TokenPayload tokenPayload) { - agonyRecordService.reviseAgonyRecord(bookShelfId, agonyId, recordId, tokenPayload.getUserId(), reviseAgonyRecordRequest.toTarget()); - } + agonyRecordService.reviseAgonyRecord(bookShelfId, agonyId, recordId, tokenPayload.getUserId(), + reviseAgonyRecordRequest.toTarget()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/response/SliceOfAgonyRecordsResponse.java b/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/response/SliceOfAgonyRecordsResponse.java index 5034a55d..3fdc6819 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/response/SliceOfAgonyRecordsResponse.java +++ b/src/main/java/toy/bookchat/bookchat/domain/agonyrecord/api/v1/response/SliceOfAgonyRecordsResponse.java @@ -5,25 +5,25 @@ import lombok.Getter; import org.springframework.data.domain.Slice; import toy.bookchat.bookchat.domain.agonyrecord.AgonyRecord; -import toy.bookchat.bookchat.domain.common.CursorMeta; +import toy.bookchat.bookchat.support.CursorMeta; @Getter public class SliceOfAgonyRecordsResponse { - private List agonyRecordResponseList; - private CursorMeta cursorMeta; + private List agonyRecordResponseList; + private CursorMeta cursorMeta; - public SliceOfAgonyRecordsResponse(Slice slice) { - this.cursorMeta = new CursorMeta<>(slice, AgonyRecord::getId); - this.agonyRecordResponseList = from(slice.getContent()); - } - - private List from(List content) { - List agonyRecordResponseList = new ArrayList<>(); - for (AgonyRecord agonyRecord : content) { - agonyRecordResponseList.add(AgonyRecordResponse.from(agonyRecord)); - } + public SliceOfAgonyRecordsResponse(Slice slice) { + this.cursorMeta = new CursorMeta<>(slice, AgonyRecord::getId); + this.agonyRecordResponseList = from(slice.getContent()); + } - return agonyRecordResponseList; + private List from(List content) { + List agonyRecordResponseList = new ArrayList<>(); + for (AgonyRecord agonyRecord : content) { + agonyRecordResponseList.add(AgonyRecordResponse.from(agonyRecord)); } + + return agonyRecordResponseList; + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/book/api/BookController.java b/src/main/java/toy/bookchat/bookchat/domain/book/api/BookController.java index 7b5cf8df..32d82738 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/book/api/BookController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/book/api/BookController.java @@ -1,6 +1,7 @@ package toy.bookchat.bookchat.domain.book.api; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @@ -9,18 +10,16 @@ import toy.bookchat.bookchat.domain.book.service.dto.request.BookSearchRequest; import toy.bookchat.bookchat.domain.book.service.dto.response.BookSearchResponse; +@RequiredArgsConstructor + @RestController -@RequestMapping("/v1/api") +@RequestMapping("/v1/api/books") public class BookController { - private final BookSearchService bookSearchService; - - public BookController(BookSearchService bookSearchService) { - this.bookSearchService = bookSearchService; - } + private final BookSearchService bookSearchService; - @GetMapping("/books") - public BookSearchResponse getBookInformation(@Valid @ModelAttribute BookSearchRequest bookSearchRequest) { - return bookSearchService.searchByQuery(bookSearchRequest); - } + @GetMapping + public BookSearchResponse getBookInformation(@Valid @ModelAttribute BookSearchRequest bookSearchRequest) { + return bookSearchService.searchByQuery(bookSearchRequest); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookReportController.java b/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookReportController.java index 9095891f..fbcbb7d7 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookReportController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookReportController.java @@ -1,6 +1,7 @@ package toy.bookchat.bookchat.domain.bookshelf.api.v1; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -17,35 +18,36 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController @RequestMapping("/v1/api/bookshelves/{bookShelfId}/report") public class BookReportController { - private final BookReportService bookReportService; - - public BookReportController(BookReportService bookReportService) { - this.bookReportService = bookReportService; - } + private final BookReportService bookReportService; - @PostMapping - public void writeBookReport(@Valid @RequestBody WriteBookReportRequest writeBookReportRequest, @PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { - bookReportService.writeReport(tokenPayload.getUserId(), bookShelfId, writeBookReportRequest.toTarget()); - } + @PostMapping + public void writeBookReport(@Valid @RequestBody WriteBookReportRequest writeBookReportRequest, + @PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { + bookReportService.writeReport(tokenPayload.getUserId(), bookShelfId, writeBookReportRequest.toTarget()); + } - @GetMapping - public BookReportResponse findBookReportFromBook(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { - BookReport bookReport = bookReportService.getBookReport(bookShelfId, tokenPayload.getUserId()); + @GetMapping + public BookReportResponse findBookReportFromBook(@PathVariable Long bookShelfId, + @UserPayload TokenPayload tokenPayload) { + BookReport bookReport = bookReportService.getBookReport(bookShelfId, tokenPayload.getUserId()); - return BookReportResponse.from(bookReport); - } + return BookReportResponse.from(bookReport); + } - @DeleteMapping - public void deleteBookReport(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { - bookReportService.deleteBookReport(bookShelfId, tokenPayload.getUserId()); - } + @DeleteMapping + public void deleteBookReport(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { + bookReportService.deleteBookReport(bookShelfId, tokenPayload.getUserId()); + } - @PutMapping - public void reviseBookReport(@PathVariable Long bookShelfId, @Valid @RequestBody ReviseBookReportRequest reviseBookReportRequest, @UserPayload TokenPayload tokenPayload) { - bookReportService.reviseBookReport(bookShelfId, tokenPayload.getUserId(), reviseBookReportRequest.toTarget()); - } + @PutMapping + public void reviseBookReport(@PathVariable Long bookShelfId, + @Valid @RequestBody ReviseBookReportRequest reviseBookReportRequest, @UserPayload TokenPayload tokenPayload) { + bookReportService.reviseBookReport(bookShelfId, tokenPayload.getUserId(), reviseBookReportRequest.toTarget()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookShelfController.java b/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookShelfController.java index b11ea526..d1e716b8 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookShelfController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/BookShelfController.java @@ -5,6 +5,7 @@ import java.net.URI; import java.time.LocalDate; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.ResponseEntity; @@ -28,41 +29,39 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController -@RequestMapping("/v1/api") +@RequestMapping("/v1/api/bookshelves") public class BookShelfController { private final BookShelfService bookShelfService; - public BookShelfController(BookShelfService bookShelfService) { - this.bookShelfService = bookShelfService; - } - - @GetMapping("/bookshelves/{bookShelfId}") + @GetMapping("/{bookShelfId}") public BookShelfResponse getBookOnBookShelf(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { return BookShelfResponse.from(bookShelfService.getBookOnBookShelf(bookShelfId, tokenPayload.getUserId())); } - @PostMapping("/bookshelves") + @PostMapping public ResponseEntity putBookOnBookShelf(@Valid @RequestBody CreateBookShelfRequest createBookShelfRequest, @UserPayload TokenPayload tokenPayload) { Long bookShelfId = bookShelfService.putBookOnBookShelf(createBookShelfRequest.toTarget(), createBookShelfRequest.getBook(), tokenPayload.getUserId()); return ResponseEntity.status(CREATED) - .headers(hs -> hs.setLocation(URI.create("/v1/api/bookshelves/" + bookShelfId))) + .headers(hs -> hs.setLocation(URI.create("/v1/api/" + bookShelfId))) .build(); } - @GetMapping("/bookshelves") + @GetMapping public SearchBookShelfByReadingStatus takeBooksOutOfBookShelves(ReadingStatus readingStatus, Pageable pageable, @UserPayload TokenPayload tokenPayload) { return new SearchBookShelfByReadingStatus( bookShelfService.takeBooksOutOfBookShelves(readingStatus, pageable, tokenPayload.getUserId())); } - @GetMapping("/bookshelves/book") + @GetMapping("/book") public ExistenceBookOnBookShelfResponse findBookIfExistedOnBookShelves(@RequestParam String isbn, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate publishAt, @UserPayload TokenPayload tokenPayload) { @@ -71,13 +70,13 @@ public ExistenceBookOnBookShelfResponse findBookIfExistedOnBookShelves(@RequestP return ExistenceBookOnBookShelfResponse.from(bookShelf); } - @PutMapping("/bookshelves/{bookShelfId}") + @PutMapping("/{bookShelfId}") public void reviseBookOnBookShelf(@PathVariable Long bookShelfId, @Valid @RequestBody ReviseBookShelfRequest reviseBookShelfStarRequest, @UserPayload TokenPayload tokenPayload) { bookShelfService.reviseBookShelf(bookShelfId, reviseBookShelfStarRequest.toTarget(), tokenPayload.getUserId()); } - @DeleteMapping("/bookshelves/{bookShelfId}") + @DeleteMapping("/{bookShelfId}") public void deleteBookOnBookShelf(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) { bookShelfService.deleteBookShelf(bookShelfId, tokenPayload.getUserId()); } diff --git a/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/response/SearchBookShelfByReadingStatus.java b/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/response/SearchBookShelfByReadingStatus.java index 9f0a79fb..4fd86ba6 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/response/SearchBookShelfByReadingStatus.java +++ b/src/main/java/toy/bookchat/bookchat/domain/bookshelf/api/v1/response/SearchBookShelfByReadingStatus.java @@ -5,22 +5,22 @@ import lombok.Getter; import org.springframework.data.domain.Page; import toy.bookchat.bookchat.domain.bookshelf.BookShelf; -import toy.bookchat.bookchat.domain.common.PageMeta; +import toy.bookchat.bookchat.support.PageMeta; @Getter public class SearchBookShelfByReadingStatus { - private PageMeta pageMeta; - private List contents; + private PageMeta pageMeta; + private List contents; - public SearchBookShelfByReadingStatus(Page pagedBookShelf) { - this.pageMeta = PageMeta.from(pagedBookShelf); - this.contents = getBookShelfSearchResponseDtos(pagedBookShelf.getContent()); - } + public SearchBookShelfByReadingStatus(Page pagedBookShelf) { + this.pageMeta = PageMeta.from(pagedBookShelf); + this.contents = getBookShelfSearchResponseDtos(pagedBookShelf.getContent()); + } - private List getBookShelfSearchResponseDtos(List bookShelves) { - return bookShelves.stream() - .map(BookShelfResponse::from) - .collect(Collectors.toList()); - } + private List getBookShelfSearchResponseDtos(List bookShelves) { + return bookShelves.stream() + .map(BookShelfResponse::from) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/ChatController.java b/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/ChatController.java index df4600c8..d5858843 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/ChatController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/ChatController.java @@ -1,7 +1,7 @@ package toy.bookchat.bookchat.domain.chat.api.v1; import javax.validation.Valid; -import lombok.extern.slf4j.Slf4j; +import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.messaging.handler.annotation.DestinationVariable; @@ -17,30 +17,29 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; -@Slf4j +@RequiredArgsConstructor + @RestController public class ChatController { - private final ChatService chatService; - - public ChatController(ChatService chatService) { - this.chatService = chatService; - } + private final ChatService chatService; - @MessageMapping("/send/chatrooms/{roomId}") - public void sendMessage(@Valid MessageRequest messageRequest, @DestinationVariable Long roomId, @UserPayload TokenPayload tokenPayload) { - chatService.sendMessage(tokenPayload.getUserId(), roomId, messageRequest.toTarget()); - } + @MessageMapping("/send/chatrooms/{roomId}") + public void sendMessage(@Valid MessageRequest messageRequest, @DestinationVariable Long roomId, + @UserPayload TokenPayload tokenPayload) { + chatService.sendMessage(tokenPayload.getUserId(), roomId, messageRequest.toTarget()); + } - @GetMapping("/v1/api/chatrooms/{roomId}/chats") - public ChatRoomChatsResponse getChatRoomChats(@PathVariable Long roomId, Long postCursorId, Pageable pageable, @UserPayload TokenPayload tokenPayload) { - Slice slicedChat = chatService.getChatRoomChats(roomId, postCursorId, pageable, tokenPayload.getUserId()); - return new ChatRoomChatsResponse(slicedChat); - } + @GetMapping("/v1/api/chatrooms/{roomId}/chats") + public ChatRoomChatsResponse getChatRoomChats(@PathVariable Long roomId, Long postCursorId, Pageable pageable, + @UserPayload TokenPayload tokenPayload) { + Slice slicedChat = chatService.getChatRoomChats(roomId, postCursorId, pageable, tokenPayload.getUserId()); + return new ChatRoomChatsResponse(slicedChat); + } - @GetMapping("/v1/api/chats/{chatId}") - public ChatDetailResponse getChat(@PathVariable Long chatId, @UserPayload TokenPayload tokenPayload) { - Chat chat = chatService.getChatDetail(chatId, tokenPayload.getUserId()); - return ChatDetailResponse.from(chat); - } + @GetMapping("/v1/api/chats/{chatId}") + public ChatDetailResponse getChat(@PathVariable Long chatId, @UserPayload TokenPayload tokenPayload) { + Chat chat = chatService.getChatDetail(chatId, tokenPayload.getUserId()); + return ChatDetailResponse.from(chat); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/response/ChatRoomChatsResponse.java b/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/response/ChatRoomChatsResponse.java index df135dc5..ed79a746 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/response/ChatRoomChatsResponse.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chat/api/v1/response/ChatRoomChatsResponse.java @@ -6,35 +6,35 @@ import lombok.Getter; import org.springframework.data.domain.Slice; import toy.bookchat.bookchat.domain.chat.Chat; -import toy.bookchat.bookchat.domain.common.CursorMeta; +import toy.bookchat.bookchat.support.CursorMeta; @Getter @EqualsAndHashCode(exclude = "cursorMeta") public class ChatRoomChatsResponse { - private List chatResponseList; - private CursorMeta cursorMeta; + private List chatResponseList; + private CursorMeta cursorMeta; - public ChatRoomChatsResponse(Slice slicedChat) { - this.cursorMeta = new CursorMeta<>(slicedChat, Chat::getId); - this.chatResponseList = slicedChat.stream() - .map(this::createChatResponse) - .collect(Collectors.toList()); - } + public ChatRoomChatsResponse(Slice slicedChat) { + this.cursorMeta = new CursorMeta<>(slicedChat, Chat::getId); + this.chatResponseList = slicedChat.stream() + .map(this::createChatResponse) + .collect(Collectors.toList()); + } - private ChatResponse createChatResponse(Chat chat) { - if (chat.isAnnouncement()) { - return ChatResponse.builder() - .chatId(chat.getId()) - .message(chat.getMessage()) - .dispatchTime(chat.getDispatchTime().toString()) - .build(); - } - return ChatResponse.builder() - .chatId(chat.getId()) - .senderId(chat.getSenderId()) - .message(chat.getMessage()) - .dispatchTime(chat.getDispatchTime().toString()) - .build(); + private ChatResponse createChatResponse(Chat chat) { + if (chat.isAnnouncement()) { + return ChatResponse.builder() + .chatId(chat.getId()) + .message(chat.getMessage()) + .dispatchTime(chat.getDispatchTime().toString()) + .build(); } + return ChatResponse.builder() + .chatId(chat.getId()) + .senderId(chat.getSenderId()) + .message(chat.getMessage()) + .dispatchTime(chat.getDispatchTime().toString()) + .build(); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java index cf543902..3f55578e 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java @@ -2,6 +2,7 @@ import java.net.URI; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.http.HttpStatus; @@ -29,16 +30,14 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController @RequestMapping("/v1/api") public class ChatRoomController { private final ChatRoomService chatRoomService; - public ChatRoomController(ChatRoomService chatRoomService) { - this.chatRoomService = chatRoomService; - } - @PostMapping("/chatrooms") public ResponseEntity createChatRoom(@Valid @RequestPart CreateChatRoomRequest createChatRoomRequest, @RequestPart(required = false) MultipartFile chatRoomImage, @UserPayload TokenPayload tokenPayload) { @@ -66,8 +65,7 @@ public UserChatRoomDetailResponse getUserChatRoomDetails(@PathVariable Long room } @GetMapping("/chatrooms") - public ChatRoomsResponseSlice getChatRooms(@ModelAttribute ChatRoomRequest chatRoomRequest, - @UserPayload TokenPayload tokenPayload, Pageable pageable) { + public ChatRoomsResponseSlice getChatRooms(@ModelAttribute ChatRoomRequest chatRoomRequest, Pageable pageable) { chatRoomRequest.validate(); Slice slicedChatRoomResponse = chatRoomService.getChatRooms(chatRoomRequest, pageable); diff --git a/src/main/java/toy/bookchat/bookchat/domain/device/api/v1/DeviceController.java b/src/main/java/toy/bookchat/bookchat/domain/device/api/v1/DeviceController.java index 307b2185..50fac99b 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/device/api/v1/DeviceController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/device/api/v1/DeviceController.java @@ -1,6 +1,7 @@ package toy.bookchat.bookchat.domain.device.api.v1; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,16 +11,14 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController @RequestMapping("/v1/api/devices") public class DeviceController { private final DeviceService deviceService; - public DeviceController(DeviceService deviceService) { - this.deviceService = deviceService; - } - @PutMapping("/fcm-token") public void updateFcmToken(@UserPayload TokenPayload tokenPayload, @Valid @RequestBody UpdateFcmTokenRequest request) { diff --git a/src/main/java/toy/bookchat/bookchat/domain/participant/api/v1/ParticipantController.java b/src/main/java/toy/bookchat/bookchat/domain/participant/api/v1/ParticipantController.java index 4d56759d..333a0c16 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/participant/api/v1/ParticipantController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/participant/api/v1/ParticipantController.java @@ -1,5 +1,6 @@ package toy.bookchat.bookchat.domain.participant.api.v1; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -10,23 +11,21 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController -@RequestMapping("/v1/api") +@RequestMapping("/v1/api/chatrooms") public class ParticipantController { private final ParticipantService participantService; - public ParticipantController(ParticipantService participantService) { - this.participantService = participantService; - } - - @PatchMapping("/chatrooms/{roomId}/participants/{userId}") + @PatchMapping("/{roomId}/participants/{userId}") public void changeParticipantRights(@PathVariable Long roomId, @PathVariable Long userId, ParticipantStatus participantStatus, @UserPayload TokenPayload tokenPayload) { participantService.changeParticipantRights(roomId, userId, participantStatus, tokenPayload.getUserId()); } - @DeleteMapping("/chatrooms/{roomId}/participants/{userId}") + @DeleteMapping("/{roomId}/participants/{userId}") public void deleteParticipant(@PathVariable Long roomId, @PathVariable Long userId, @UserPayload TokenPayload tokenPayload) { participantService.kickParticipant(roomId, userId, tokenPayload.getUserId()); diff --git a/src/main/java/toy/bookchat/bookchat/domain/scrap/api/ScrapController.java b/src/main/java/toy/bookchat/bookchat/domain/scrap/api/ScrapController.java index e191af80..c686268f 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/scrap/api/ScrapController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/scrap/api/ScrapController.java @@ -5,6 +5,7 @@ import java.net.URI; import java.util.List; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -21,17 +22,15 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @RestController -@RequestMapping("/v1/api") +@RequestMapping("/v1/api/scraps") public class ScrapController { private final ScrapService scrapService; - public ScrapController(ScrapService scrapService) { - this.scrapService = scrapService; - } - - @PostMapping("/scraps") + @PostMapping public ResponseEntity scrapChat(@Valid @RequestBody CreateScrapRequest createScrapRequest, @UserPayload TokenPayload tokenPayload) { Long scrapId = scrapService.scrap(createScrapRequest, tokenPayload.getUserId()); @@ -41,18 +40,18 @@ public ResponseEntity scrapChat(@Valid @RequestBody CreateScrapRequest cre .build(); } - @GetMapping("/scraps") + @GetMapping public ScrapResponseSlice getScraps(Long bookShelfId, Long postCursorId, Pageable pageable, @UserPayload TokenPayload tokenPayload) { return scrapService.getScraps(bookShelfId, postCursorId, pageable, tokenPayload.getUserId()); } - @GetMapping("/scraps/{scrapId}") + @GetMapping("/{scrapId}") public ScrapResponse getScrap(@PathVariable Long scrapId, @UserPayload TokenPayload tokenPayload) { return scrapService.getScrap(scrapId, tokenPayload.getUserId()); } - @DeleteMapping("/scraps/{scrapIds}") + @DeleteMapping("/{scrapIds}") public void deleteScraps(Long bookShelfId, @PathVariable List scrapIds, @UserPayload TokenPayload tokenPayload) { scrapService.deleteScraps(bookShelfId, scrapIds, tokenPayload.getUserId()); diff --git a/src/main/java/toy/bookchat/bookchat/domain/scrap/api/v1/response/ScrapResponseSlice.java b/src/main/java/toy/bookchat/bookchat/domain/scrap/api/v1/response/ScrapResponseSlice.java index 430dfadc..3231bab2 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/scrap/api/v1/response/ScrapResponseSlice.java +++ b/src/main/java/toy/bookchat/bookchat/domain/scrap/api/v1/response/ScrapResponseSlice.java @@ -3,7 +3,7 @@ import java.util.List; import lombok.Getter; import org.springframework.data.domain.Slice; -import toy.bookchat.bookchat.domain.common.CursorMeta; +import toy.bookchat.bookchat.support.CursorMeta; @Getter public class ScrapResponseSlice { diff --git a/src/main/java/toy/bookchat/bookchat/domain/user/api/v1/UserController.java b/src/main/java/toy/bookchat/bookchat/domain/user/api/v1/UserController.java index 7128f058..8e28fe77 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/user/api/v1/UserController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/user/api/v1/UserController.java @@ -1,8 +1,11 @@ package toy.bookchat.bookchat.domain.user.api.v1; +import static toy.bookchat.bookchat.support.Constants.OIDC; + import javax.validation.Valid; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Pattern; +import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; @@ -17,7 +20,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import toy.bookchat.bookchat.db_module.user.UserEntity; -import toy.bookchat.bookchat.domain.common.RateLimit; +import toy.bookchat.bookchat.support.RateLimit; import toy.bookchat.bookchat.domain.user.UserProfile; import toy.bookchat.bookchat.domain.user.api.v1.request.ChangeUserNicknameRequest; import toy.bookchat.bookchat.domain.user.api.v1.request.UserSignInRequest; @@ -32,32 +35,36 @@ import toy.bookchat.bookchat.security.user.TokenPayload; import toy.bookchat.bookchat.security.user.UserPayload; +@RequiredArgsConstructor + @Validated @RestController @RequestMapping("/v1/api") public class UserController { - private final String OIDC = "OIDC"; - private final UserService userService; private final IdTokenManager idTokenManager; private final JwtTokenProvider jwtTokenProvider; private final JwtTokenRecorder jwtTokenRecorder; - public UserController(UserService userService, IdTokenManager idTokenManager, - JwtTokenProvider jwtTokenProvider, JwtTokenRecorder jwtTokenRecorder) { - this.userService = userService; - this.idTokenManager = idTokenManager; - this.jwtTokenProvider = jwtTokenProvider; - this.jwtTokenRecorder = jwtTokenRecorder; - } - + /** + * 유저 프로필 조회 + * + * @param tokenPayload + * @return + */ @GetMapping("/users/profile") public UserProfileResponse userProfile(@UserPayload TokenPayload tokenPayload) { UserProfile userProfile = userService.findUser(tokenPayload.getUserId()); return UserProfileResponse.of(userProfile); } + /** + * 닉네임 중복 체크 + * + * @param nickname + * @return + */ @GetMapping("/users/profile/nickname") @RateLimit(keyName = "nicknameCall", capacity = 300, tokens = 100, seconds = 1) public ResponseEntity checkDuplicatedNickname(String nickname) { @@ -67,12 +74,26 @@ public ResponseEntity checkDuplicatedNickname(String nickname) { return ResponseEntity.ok().build(); } + /** + * 유저 프로필 수정 + * + * @param changeUserNicknameRequest + * @param userProfileImage + * @param tokenPayload + */ @PostMapping("/users/profile") public void updateUserProfile(@Valid @RequestPart ChangeUserNicknameRequest changeUserNicknameRequest, @RequestPart(required = false) MultipartFile userProfileImage, @UserPayload TokenPayload tokenPayload) { userService.updateUserProfile(changeUserNicknameRequest, userProfileImage, tokenPayload.getUserId()); } + /** + * 회원가입 + * + * @param userSignUpRequest + * @param userProfileImage + * @param bearerToken + */ @PostMapping("/users/signup") public void userSignUp(@Valid @RequestPart UserSignUpRequest userSignUpRequest, @RequestPart(required = false) MultipartFile userProfileImage, @@ -85,6 +106,13 @@ public void userSignUp(@Valid @RequestPart UserSignUpRequest userSignUpRequest, userService.registerNewUser(userSignUpRequest, userProfileImage, oauth2MemberNumber, userEmail); } + /** + * 로그인 + * + * @param bearerToken + * @param userSignInRequest + * @return + */ @PostMapping("/users/signin") public Token userSignIn( @RequestHeader(OIDC) @NotBlank @Pattern(regexp = "^(Bearer)\\s.+") String bearerToken, @@ -101,16 +129,32 @@ public Token userSignIn( return token; } + /** + * 로그아웃 + * + * @param tokenPayload + */ @PostMapping("/users/logout") public void userLogout(@UserPayload TokenPayload tokenPayload) { userService.deleteDevice(tokenPayload.getUserId()); } + /** + * 회원탈퇴 + * + * @param tokenPayload + */ @DeleteMapping("/users") public void withdrawUser(@UserPayload TokenPayload tokenPayload) { userService.deleteUser(tokenPayload.getUserId()); } + /** + * 회원 프로필 조회 + * + * @param memberId + * @return + */ @GetMapping("/members") public MemberProfileResponse memberProfile(@RequestParam Long memberId) { return userService.getMemberProfile(memberId); diff --git a/src/main/java/toy/bookchat/bookchat/domain/user/service/UserReader.java b/src/main/java/toy/bookchat/bookchat/domain/user/service/UserReader.java index 9648a26e..3d815cac 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/user/service/UserReader.java +++ b/src/main/java/toy/bookchat/bookchat/domain/user/service/UserReader.java @@ -1,6 +1,6 @@ package toy.bookchat.bookchat.domain.user.service; -import static toy.bookchat.bookchat.domain.common.Status.ACTIVE; +import static toy.bookchat.bookchat.support.Status.ACTIVE; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/toy/bookchat/bookchat/localtest/LocalTestConfig.java b/src/main/java/toy/bookchat/bookchat/localtest/LocalTestConfig.java index 1c790f5e..5f726d32 100644 --- a/src/main/java/toy/bookchat/bookchat/localtest/LocalTestConfig.java +++ b/src/main/java/toy/bookchat/bookchat/localtest/LocalTestConfig.java @@ -1,6 +1,6 @@ package toy.bookchat.bookchat.localtest; -import static toy.bookchat.bookchat.domain.common.Status.ACTIVE; +import static toy.bookchat.bookchat.support.Status.ACTIVE; import java.util.List; import javax.annotation.PostConstruct; @@ -22,66 +22,66 @@ @Component public class LocalTestConfig implements JwtTokenManager { - private final UserRepository userRepository; - private final Flyway flyway; - private UserEntity userEntity; + private final UserRepository userRepository; + private final Flyway flyway; + private UserEntity userEntity; - public LocalTestConfig(UserRepository userRepository, Flyway flyway) { - this.userRepository = userRepository; - this.flyway = flyway; - } + public LocalTestConfig(UserRepository userRepository, Flyway flyway) { + this.userRepository = userRepository; + this.flyway = flyway; + } - @PostConstruct - public void init() { - flyway.clean(); - flyway.migrate(); - userEntity = UserEntity.builder() - .name("google123") - .nickname("geneaky") - .email("kaktus41@gmail.com") - .defaultProfileImageType(1) - .profileImageUrl(null) - .provider(OAuth2Provider.GOOGLE) - .readingTastes(List.of(ReadingTaste.DEVELOPMENT, ReadingTaste.HEALTH)) - .role(ROLE.USER) - .status(ACTIVE) - .build(); - userRepository.save(userEntity); - } + @PostConstruct + public void init() { + flyway.clean(); + flyway.migrate(); + userEntity = UserEntity.builder() + .name("google123") + .nickname("geneaky") + .email("kaktus41@gmail.com") + .defaultProfileImageType(1) + .profileImageUrl(null) + .provider(OAuth2Provider.GOOGLE) + .readingTastes(List.of(ReadingTaste.DEVELOPMENT, ReadingTaste.HEALTH)) + .role(ROLE.USER) + .status(ACTIVE) + .build(); + userRepository.save(userEntity); + } - @PreDestroy - public void finalize() { - userRepository.deleteAll(); - } + @PreDestroy + public void finalize() { + userRepository.deleteAll(); + } - @Override - public String extractTokenFromAuthorizationHeader(String header) { - return null; - } + @Override + public String extractTokenFromAuthorizationHeader(String header) { + return null; + } - @Override - public Long getUserIdFromToken(String token) { - return this.userEntity.getId(); - } + @Override + public Long getUserIdFromToken(String token) { + return this.userEntity.getId(); + } - @Override - public String getOAuth2MemberNumberFromToken(String token) { - return this.userEntity.getName(); - } + @Override + public String getOAuth2MemberNumberFromToken(String token) { + return this.userEntity.getName(); + } - @Override - public String getUserEmailFromToken(String token) { - return this.userEntity.getEmail(); - } + @Override + public String getUserEmailFromToken(String token) { + return this.userEntity.getEmail(); + } - @Override - public boolean shouldRefreshTokenBeRenew(String token) { - return false; - } + @Override + public boolean shouldRefreshTokenBeRenew(String token) { + return false; + } - @Override - public TokenPayload getTokenPayloadFromToken(String token) { - return TokenPayload.of(userEntity.getId(), userEntity.getName(), userEntity.getNickname(), userEntity.getEmail(), - userEntity.getProfileImageUrl(), userEntity.getDefaultProfileImageType(), userEntity.getRole()); - } + @Override + public TokenPayload getTokenPayloadFromToken(String token) { + return TokenPayload.of(userEntity.getId(), userEntity.getName(), userEntity.getNickname(), userEntity.getEmail(), + userEntity.getProfileImageUrl(), userEntity.getDefaultProfileImageType(), userEntity.getRole()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/Constants.java b/src/main/java/toy/bookchat/bookchat/support/Constants.java new file mode 100644 index 00000000..c574e0f7 --- /dev/null +++ b/src/main/java/toy/bookchat/bookchat/support/Constants.java @@ -0,0 +1,9 @@ +package toy.bookchat.bookchat.support; + +public abstract class Constants { + + public static final String OIDC = "OIDC"; + + private Constants() { + } +} diff --git a/src/main/java/toy/bookchat/bookchat/support/CursorMeta.java b/src/main/java/toy/bookchat/bookchat/support/CursorMeta.java index 6e5c8725..9322ba1c 100644 --- a/src/main/java/toy/bookchat/bookchat/support/CursorMeta.java +++ b/src/main/java/toy/bookchat/bookchat/support/CursorMeta.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import java.util.List; import java.util.function.Function; @@ -8,29 +8,29 @@ @Getter public final class CursorMeta { - private final int sliceSize; - private final int contentSize; - private final boolean hasContent; - private final boolean isFirst; - private final boolean isLast; - private final boolean hasNext; + private final int sliceSize; + private final int contentSize; + private final boolean hasContent; + private final boolean isFirst; + private final boolean isLast; + private final boolean hasNext; - private final R nextCursorId; + private final R nextCursorId; - public CursorMeta(Slice slice, Function getId) { - this.sliceSize = slice.getPageable().getPageSize(); - this.contentSize = slice.getContent().size(); - this.hasContent = slice.hasContent(); - this.isFirst = slice.isFirst(); - this.isLast = slice.isLast(); - this.hasNext = slice.hasNext(); - this.nextCursorId = getNextCursorId(slice.getContent(), getId); - } + public CursorMeta(Slice slice, Function getId) { + this.sliceSize = slice.getPageable().getPageSize(); + this.contentSize = slice.getContent().size(); + this.hasContent = slice.hasContent(); + this.isFirst = slice.isFirst(); + this.isLast = slice.isLast(); + this.hasNext = slice.hasNext(); + this.nextCursorId = getNextCursorId(slice.getContent(), getId); + } - private R getNextCursorId(List content, Function getId) { - if (content.isEmpty()) { - return null; - } - return content.stream().reduce((c1, c2) -> c2).map(getId).orElse(null); + private R getNextCursorId(List content, Function getId) { + if (content.isEmpty()) { + return null; } + return content.stream().reduce((c1, c2) -> c2).map(getId).orElse(null); + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/PageMeta.java b/src/main/java/toy/bookchat/bookchat/support/PageMeta.java index 60297a80..da14b878 100644 --- a/src/main/java/toy/bookchat/bookchat/support/PageMeta.java +++ b/src/main/java/toy/bookchat/bookchat/support/PageMeta.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import lombok.Getter; import org.springframework.data.domain.Page; @@ -6,34 +6,34 @@ @Getter public final class PageMeta { - private final Long totalElements; + private final Long totalElements; - private final int totalPages; + private final int totalPages; - private final int pageSize; + private final int pageSize; - private final int pageNumber; + private final int pageNumber; - private final Long offset; + private final Long offset; - private final boolean first; + private final boolean first; - private final boolean last; + private final boolean last; - private final boolean empty; + private final boolean empty; - private PageMeta(Page page) { - this.totalElements = page.getTotalElements(); - this.totalPages = page.getTotalPages(); - this.pageSize = page.getPageable().getPageSize(); - this.pageNumber = page.getPageable().getPageNumber(); - this.offset = page.getPageable().getOffset(); - this.first = page.isFirst(); - this.last = page.isLast(); - this.empty = page.isEmpty(); - } + private PageMeta(Page page) { + this.totalElements = page.getTotalElements(); + this.totalPages = page.getTotalPages(); + this.pageSize = page.getPageable().getPageSize(); + this.pageNumber = page.getPageable().getPageNumber(); + this.offset = page.getPageable().getOffset(); + this.first = page.isFirst(); + this.last = page.isLast(); + this.empty = page.isEmpty(); + } - public static PageMeta from(Page page) { - return new PageMeta(page); - } + public static PageMeta from(Page page) { + return new PageMeta(page); + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/RateLimit.java b/src/main/java/toy/bookchat/bookchat/support/RateLimit.java index 77b3252e..271997b6 100644 --- a/src/main/java/toy/bookchat/bookchat/support/RateLimit.java +++ b/src/main/java/toy/bookchat/bookchat/support/RateLimit.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -9,11 +9,11 @@ @Retention(RetentionPolicy.RUNTIME) public @interface RateLimit { - String keyName() default ""; + String keyName() default ""; - long capacity(); + long capacity(); - long seconds(); + long seconds(); - long tokens(); + long tokens(); } diff --git a/src/main/java/toy/bookchat/bookchat/support/RateLimiter.java b/src/main/java/toy/bookchat/bookchat/support/RateLimiter.java index 713584fa..e54fc471 100644 --- a/src/main/java/toy/bookchat/bookchat/support/RateLimiter.java +++ b/src/main/java/toy/bookchat/bookchat/support/RateLimiter.java @@ -1,6 +1,6 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; public interface RateLimiter { - boolean tryConsume(); + boolean tryConsume(); } diff --git a/src/main/java/toy/bookchat/bookchat/support/RateLimiterAop.java b/src/main/java/toy/bookchat/bookchat/support/RateLimiterAop.java index 0d55e4d1..867d64ff 100644 --- a/src/main/java/toy/bookchat/bookchat/support/RateLimiterAop.java +++ b/src/main/java/toy/bookchat/bookchat/support/RateLimiterAop.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import java.lang.reflect.Method; import java.util.concurrent.ConcurrentHashMap; @@ -14,25 +14,25 @@ @Component public class RateLimiterAop { - private final ConcurrentHashMap rateLimiterMappingTable = new ConcurrentHashMap<>(); + private final ConcurrentHashMap rateLimiterMappingTable = new ConcurrentHashMap<>(); - @Pointcut("@annotation(toy.bookchat.bookchat.domain.common.RateLimit)") - public void pointCut() { - } + @Pointcut("@annotation(toy.bookchat.bookchat.support.RateLimit)") + public void pointCut() { + } - @Before("pointCut()") - public void rateLimit(JoinPoint joinPoint) { - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - RateLimit rateLimit = method.getAnnotation(RateLimit.class); - RateLimiter rateLimiter = rateLimiterMappingTable.putIfAbsent(rateLimit.keyName(), - new RateLimiterImpl(rateLimit.capacity(), rateLimit.tokens(), rateLimit.seconds())); + @Before("pointCut()") + public void rateLimit(JoinPoint joinPoint) { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + RateLimit rateLimit = method.getAnnotation(RateLimit.class); + RateLimiter rateLimiter = rateLimiterMappingTable.putIfAbsent(rateLimit.keyName(), + new RateLimiterImpl(rateLimit.capacity(), rateLimit.tokens(), rateLimit.seconds())); - if (!rateLimiter.tryConsume()) { - throw new RateOverLimitException(); - } + if (!rateLimiter.tryConsume()) { + throw new RateOverLimitException(); + } /* TODO: 2023-02-07 ip를 별도로 기억해서 차단하는 방식으로하기엔 메모리가 부족하기 때문에 초당 보낼 수 있는 전체 호출 횟수를 제한. (추후 ip방식 혹은 어플 스토어 계정을 메모리나 db에 넣는 방식으로 바꿀 수도 있음.) */ - } + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/RateLimiterImpl.java b/src/main/java/toy/bookchat/bookchat/support/RateLimiterImpl.java index 82e01db0..0866cd5a 100644 --- a/src/main/java/toy/bookchat/bookchat/support/RateLimiterImpl.java +++ b/src/main/java/toy/bookchat/bookchat/support/RateLimiterImpl.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import io.github.bucket4j.Bandwidth; import io.github.bucket4j.Bucket; @@ -7,18 +7,18 @@ public class RateLimiterImpl implements RateLimiter { - public static final int TOKEN_PER_REQUEST = 1; - private final Bucket bucket; + public static final int TOKEN_PER_REQUEST = 1; + private final Bucket bucket; - public RateLimiterImpl(long capacity, long tokens, long seconds) { - this.bucket = Bucket.builder() - .addLimit( - Bandwidth.classic(capacity, Refill.greedy(tokens, Duration.ofSeconds(seconds)))) - .build(); - } + public RateLimiterImpl(long capacity, long tokens, long seconds) { + this.bucket = Bucket.builder() + .addLimit( + Bandwidth.classic(capacity, Refill.greedy(tokens, Duration.ofSeconds(seconds)))) + .build(); + } - @Override - public boolean tryConsume() { - return this.bucket.tryConsume(TOKEN_PER_REQUEST); - } + @Override + public boolean tryConsume() { + return this.bucket.tryConsume(TOKEN_PER_REQUEST); + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/RepositorySupport.java b/src/main/java/toy/bookchat/bookchat/support/RepositorySupport.java index 2a39f2fc..da29bfbe 100644 --- a/src/main/java/toy/bookchat/bookchat/support/RepositorySupport.java +++ b/src/main/java/toy/bookchat/bookchat/support/RepositorySupport.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import com.querydsl.core.types.Order; import com.querydsl.core.types.OrderSpecifier; @@ -17,76 +17,76 @@ public abstract class RepositorySupport { - public static OrderSpecifier[] extractOrderSpecifierFrom(final EntityPathBase qClass, - final Pageable pageable) { - return pageable.getSort() - .stream() - .map(sort -> toOrderSpecifier(qClass, sort)) - .collect(Collectors.toList()).toArray(OrderSpecifier[]::new); - } + public static OrderSpecifier[] extractOrderSpecifierFrom(final EntityPathBase qClass, + final Pageable pageable) { + return pageable.getSort() + .stream() + .map(sort -> toOrderSpecifier(qClass, sort)) + .collect(Collectors.toList()).toArray(OrderSpecifier[]::new); + } - private static OrderSpecifier toOrderSpecifier(final EntityPathBase qClass, - final Sort.Order sortOrder) { - final Order order = toOrder(sortOrder); - final PathBuilder pathBuilder = new PathBuilder<>(qClass.getType(), - qClass.getMetadata()); - return new OrderSpecifier(order, pathBuilder.get(sortOrder.getProperty())); - } + private static OrderSpecifier toOrderSpecifier(final EntityPathBase qClass, + final Sort.Order sortOrder) { + final Order order = toOrder(sortOrder); + final PathBuilder pathBuilder = new PathBuilder<>(qClass.getType(), + qClass.getMetadata()); + return new OrderSpecifier(order, pathBuilder.get(sortOrder.getProperty())); + } - private static Order toOrder(final Sort.Order sortOrder) { - if (sortOrder.isAscending()) { - return Order.ASC; - } - return Order.DESC; + private static Order toOrder(final Sort.Order sortOrder) { + if (sortOrder.isAscending()) { + return Order.ASC; } + return Order.DESC; + } - public static Slice toSlice(final List content, final Pageable pageable) { - if (hasSameSize(content, pageable)) { - return new SliceImpl<>(content, pageable, true); - } - return new SliceImpl<>(content, pageable, false); + public static Slice toSlice(final List content, final Pageable pageable) { + if (hasSameSize(content, pageable)) { + return new SliceImpl<>(content, pageable, true); } + return new SliceImpl<>(content, pageable, false); + } - private static boolean hasSameSize(List content, Pageable pageable) { - return content.size() == pageable.getPageSize(); - } + private static boolean hasSameSize(List content, Pageable pageable) { + return content.size() == pageable.getPageSize(); + } - public static > BooleanExpression numberBasedPagination( - EntityPathBase entityPathBase, NumberPath numberPath, T optionalNumberCursorId, - Pageable pageable) { + public static > BooleanExpression numberBasedPagination( + EntityPathBase entityPathBase, NumberPath numberPath, T optionalNumberCursorId, + Pageable pageable) { - return optionalNumberCursorId == null ? null - : getCursorExpression(entityPathBase, numberPath, optionalNumberCursorId, pageable); - } + return optionalNumberCursorId == null ? null + : getCursorExpression(entityPathBase, numberPath, optionalNumberCursorId, pageable); + } - private static > BooleanExpression getCursorExpression( - EntityPathBase entityPathBase, NumberPath numberPath, T numberCursorId, - Pageable pageable) { - for (OrderSpecifier orderSpecifier : extractOrderSpecifierFrom(entityPathBase, pageable)) { - if (isSameTargetPath(orderSpecifier, numberPath)) { - return getSortedCursorExpression(numberPath, numberCursorId, orderSpecifier); - } - } - throw new NotSupportedPagingConditionException(); + private static > BooleanExpression getCursorExpression( + EntityPathBase entityPathBase, NumberPath numberPath, T numberCursorId, + Pageable pageable) { + for (OrderSpecifier orderSpecifier : extractOrderSpecifierFrom(entityPathBase, pageable)) { + if (isSameTargetPath(orderSpecifier, numberPath)) { + return getSortedCursorExpression(numberPath, numberCursorId, orderSpecifier); + } } + throw new NotSupportedPagingConditionException(); + } - private static > BooleanExpression getSortedCursorExpression( - NumberPath numberPath, T numberCursorId, - OrderSpecifier orderSpecifier) { - if (orderSpecifier.isAscending()) { - return numberPath.gt(numberCursorId); - } - return numberPath.lt(numberCursorId); + private static > BooleanExpression getSortedCursorExpression( + NumberPath numberPath, T numberCursorId, + OrderSpecifier orderSpecifier) { + if (orderSpecifier.isAscending()) { + return numberPath.gt(numberCursorId); } + return numberPath.lt(numberCursorId); + } - private static boolean isSameTargetPath(OrderSpecifier orderSpecifier, Path path) { - if (isSamePath(orderSpecifier, path)) { - return true; - } - return false; + private static boolean isSameTargetPath(OrderSpecifier orderSpecifier, Path path) { + if (isSamePath(orderSpecifier, path)) { + return true; } + return false; + } - private static boolean isSamePath(OrderSpecifier orderSpecifier, Path path) { - return orderSpecifier.getTarget().equals(path); - } + private static boolean isSamePath(OrderSpecifier orderSpecifier, Path path) { + return orderSpecifier.getTarget().equals(path); + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/SliceMeta.java b/src/main/java/toy/bookchat/bookchat/support/SliceMeta.java index e4551f6a..c1f6c058 100644 --- a/src/main/java/toy/bookchat/bookchat/support/SliceMeta.java +++ b/src/main/java/toy/bookchat/bookchat/support/SliceMeta.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; import lombok.Getter; import org.springframework.data.domain.Slice; @@ -6,28 +6,28 @@ @Getter public final class SliceMeta { - private final int sliceSize; - private final int sliceNumber; - private final int contentSize; - private final boolean hasContent; - private final boolean isFirst; - private final boolean isLast; - private final boolean hasNext; - private final boolean hasPrevious; + private final int sliceSize; + private final int sliceNumber; + private final int contentSize; + private final boolean hasContent; + private final boolean isFirst; + private final boolean isLast; + private final boolean hasNext; + private final boolean hasPrevious; - private SliceMeta(Slice slice) { - this.sliceSize = slice.getPageable().getPageSize(); - this.sliceNumber = slice.getPageable().getPageNumber(); - this.contentSize = slice.getContent().size(); - this.hasContent = slice.hasContent(); - this.isFirst = slice.isFirst(); - this.isLast = slice.isLast(); - this.hasNext = slice.hasNext(); - this.hasPrevious = slice.hasPrevious(); - } + private SliceMeta(Slice slice) { + this.sliceSize = slice.getPageable().getPageSize(); + this.sliceNumber = slice.getPageable().getPageNumber(); + this.contentSize = slice.getContent().size(); + this.hasContent = slice.hasContent(); + this.isFirst = slice.isFirst(); + this.isLast = slice.isLast(); + this.hasNext = slice.hasNext(); + this.hasPrevious = slice.hasPrevious(); + } - public static SliceMeta from(Slice slice) { - return new SliceMeta(slice); - } + public static SliceMeta from(Slice slice) { + return new SliceMeta(slice); + } } diff --git a/src/main/java/toy/bookchat/bookchat/support/Status.java b/src/main/java/toy/bookchat/bookchat/support/Status.java index a23caa7a..20f99fc8 100644 --- a/src/main/java/toy/bookchat/bookchat/support/Status.java +++ b/src/main/java/toy/bookchat/bookchat/support/Status.java @@ -1,5 +1,5 @@ -package toy.bookchat.bookchat.domain.common; +package toy.bookchat.bookchat.support; public enum Status { - ACTIVE, INACTIVE + ACTIVE, INACTIVE }