From aa4349b83b125286f3a4a9859aa2cf17848dce8f Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:49:11 +0900 Subject: [PATCH 01/17] =?UTF-8?q?NO-JIRA--refactor=20:=20card=20table?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nabimarketbe/domain/card/entity/Card.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java index dbf4bd69..4985b5ff 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java @@ -1,21 +1,33 @@ package org.prgrms.nabimarketbe.domain.card.entity; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.prgrms.nabimarketbe.global.BaseEntity; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.Lob; +import javax.persistence.OneToOne; +import javax.persistence.Table; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + import org.prgrms.nabimarketbe.domain.item.entity.Item; +import org.prgrms.nabimarketbe.global.BaseEntity; import org.prgrms.nabimarketbe.global.error.BaseException; import org.prgrms.nabimarketbe.global.error.ErrorCode; -import javax.persistence.*; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; @Entity -@Table +@Table(name = "cards") @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Card extends BaseEntity { From ed1c6a60bdb306a2998bec48427f732918987a17 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:49:27 +0900 Subject: [PATCH 02/17] =?UTF-8?q?NO-JIRA--refactor=20:=20=EA=B8=B0?= =?UTF-8?q?=ED=83=80=20=EB=AA=85=EC=84=B8=EC=97=90=20=EB=A7=9E=EC=A7=80?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prgrms/nabimarketbe/domain/user/api/UserController.java | 5 +++-- .../prgrms/nabimarketbe/domain/user/service/SignService.java | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java index e85d4705..637e9ea1 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java @@ -33,13 +33,14 @@ public SingleResult getUserByToken(@RequestHeader(name = "autho } @PutMapping("/profile-image") - public SingleResult updateUserImageUrl( + public SingleResult updateUserImageUrl( @RequestHeader(name = "authorization") String token, @RequestPart("file") MultipartFile file ) { UserUpdateResponseDTO userUpdateResponseDTO = userService.updateUserImageUrl(token, file); + UserResponseDTO userResponseDTO = new UserResponseDTO(userUpdateResponseDTO); - return ResponseFactory.getSingleResult(userUpdateResponseDTO); + return ResponseFactory.getSingleResult(userResponseDTO); } @PutMapping("/nickname") diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java index 105d6e52..cc1afa1c 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java @@ -55,9 +55,9 @@ public UserLoginResponseDTO signIn(UserSignInRequestDTO userSignInRequestDTO) { } }); - TokenDTO tokenDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole()); + TokenResponseDTO tokenResponseDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole()); - UserLoginResponseDTO response = UserLoginResponseDTO.of(user, tokenDTO); + UserLoginResponseDTO response = UserLoginResponseDTO.of(user, tokenResponseDTO); return response; } From 94f2b4c3ed7599f3663f26dc0dd173844d4513e9 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:49:38 +0900 Subject: [PATCH 03/17] =?UTF-8?q?NABI-124--feat=20:=20=EC=B0=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../domain/dibs/api/DibController.java | 33 +++++++++++++ .../dto/response/DibCreateResponseDTO.java | 27 +++++++++++ .../dibs/dto/response/DibResponseDTO.java | 6 +++ .../nabimarketbe/domain/dibs/entity/Dib.java | 46 +++++++++++++++++++ .../domain/dibs/repository/DibRepository.java | 7 +++ .../domain/dibs/service/DibService.java | 42 +++++++++++++++++ 7 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibResponseDTO.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/entity/Dib.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java diff --git a/build.gradle b/build.gradle index b821c461..5795379c 100644 --- a/build.gradle +++ b/build.gradle @@ -30,10 +30,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'com.auth0:java-jwt:4.4.0' + implementation 'com.mysql:mysql-connector-j' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' - runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.security:spring-security-test' diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java new file mode 100644 index 00000000..af5d8b1c --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java @@ -0,0 +1,33 @@ +package org.prgrms.nabimarketbe.domain.dibs.api; + +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibCreateResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.service.DibService; +import org.prgrms.nabimarketbe.global.util.ResponseFactory; +import org.prgrms.nabimarketbe.global.util.model.SingleResult; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@RequestMapping("/api/v1/dibs") +@RestController +public class DibController { + private final DibService dibService; + + @PostMapping("/{cardId}") + public ResponseEntity>> createDib( + @RequestHeader(name = "authorization") String token, + @PathVariable Long cardId + ) { + DibCreateResponseDTO dibCreateResponseDTO = dibService.createDib(token, cardId); + DibResponseDTO dibResponseDTO = new DibResponseDTO(dibCreateResponseDTO); + + return ResponseEntity.ok(ResponseFactory.getSingleResult(dibResponseDTO)); + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java new file mode 100644 index 00000000..2c7ef159 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java @@ -0,0 +1,27 @@ +package org.prgrms.nabimarketbe.domain.dibs.dto.response; + +import java.time.LocalDateTime; + +import org.prgrms.nabimarketbe.domain.dibs.entity.Dib; + +import lombok.Builder; + +@Builder +public record DibCreateResponseDTO( + Long dibId, + Long cardId, + Long userId, + LocalDateTime createdAt, + LocalDateTime modifiedAt +) { + + public static DibCreateResponseDTO from(Dib dib) { + return DibCreateResponseDTO.builder() + .dibId(dib.getDibId()) + .userId(dib.getUser().getUserId()) + .cardId(dib.getCard().getCardId()) + .createdAt(dib.getCreatedDate()) + .modifiedAt(dib.getModifiedDate()) + .build(); + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibResponseDTO.java new file mode 100644 index 00000000..58c22e7f --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibResponseDTO.java @@ -0,0 +1,6 @@ +package org.prgrms.nabimarketbe.domain.dibs.dto.response; + +public record DibResponseDTO( + T dibInfo +) { +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/entity/Dib.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/entity/Dib.java new file mode 100644 index 00000000..1645c71f --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/entity/Dib.java @@ -0,0 +1,46 @@ +package org.prgrms.nabimarketbe.domain.dibs.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.prgrms.nabimarketbe.domain.card.entity.Card; +import org.prgrms.nabimarketbe.domain.user.entity.User; +import org.prgrms.nabimarketbe.global.BaseEntity; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Entity +@Table(name = "dibs") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Dib extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "dib_id", nullable = false) + private Long dibId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "card_id", nullable = false) + private Card card; + + public Dib( + User user, + Card card + ) { + this.user = user; + this.card = card; + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java new file mode 100644 index 00000000..ccdb20f0 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java @@ -0,0 +1,7 @@ +package org.prgrms.nabimarketbe.domain.dibs.repository; + +import org.prgrms.nabimarketbe.domain.dibs.entity.Dib; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface DibRepository extends JpaRepository { +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java new file mode 100644 index 00000000..a2cec4e0 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -0,0 +1,42 @@ +package org.prgrms.nabimarketbe.domain.dibs.service; + +import org.prgrms.nabimarketbe.domain.card.entity.Card; +import org.prgrms.nabimarketbe.domain.card.repository.CardRepository; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibCreateResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.entity.Dib; +import org.prgrms.nabimarketbe.domain.dibs.repository.DibRepository; +import org.prgrms.nabimarketbe.domain.user.entity.User; +import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; +import org.prgrms.nabimarketbe.domain.user.service.CheckService; +import org.springframework.stereotype.Service; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class DibService { + private final DibRepository dibRepository; + + private final UserRepository userRepository; + + private final CardRepository cardRepository; + + private final CheckService checkService; + + public DibCreateResponseDTO createDib( + String token, + Long cardId + ) { + Long userId = checkService.parseToken(token); + User user = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); + + Card card = cardRepository.findById(cardId) + .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); + + Dib dib = new Dib(user, card); + Dib savedDib = dibRepository.save(dib); + + return DibCreateResponseDTO.from(dib); + } +} From bf86825760f3a08e0db20d23d112524579ca34f5 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 01:14:59 +0900 Subject: [PATCH 04/17] =?UTF-8?q?NABI-124--refactor=20:=20=EC=B0=9C?= =?UTF-8?q?=EA=B0=AF=EC=88=98=20=EC=A6=9D=EA=B0=80=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgrms/nabimarketbe/domain/card/entity/Card.java | 4 ++++ .../prgrms/nabimarketbe/domain/dibs/service/DibService.java | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java index 4985b5ff..cbf98e05 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java @@ -112,4 +112,8 @@ public void updateThumbNailImage(String url) { public void updateViewCount() { this.viewCount += 1; } + + public void increaseDibCount() { + this.dibCount += 1; + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java index a2cec4e0..46d1376e 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -9,6 +9,7 @@ import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; import org.prgrms.nabimarketbe.domain.user.service.CheckService; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; @@ -23,6 +24,7 @@ public class DibService { private final CheckService checkService; + @Transactional public DibCreateResponseDTO createDib( String token, Long cardId @@ -34,9 +36,11 @@ public DibCreateResponseDTO createDib( Card card = cardRepository.findById(cardId) .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); + card.increaseDibCount(); + Dib dib = new Dib(user, card); Dib savedDib = dibRepository.save(dib); - return DibCreateResponseDTO.from(dib); + return DibCreateResponseDTO.from(savedDib); } } From 23fb14c94963517338721127db10701d6225acf0 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 01:17:40 +0900 Subject: [PATCH 05/17] =?UTF-8?q?NO-JIRA--refactor=20:=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EC=A1=B0=ED=9A=8C=EC=88=98=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/card/api/CardController.java | 16 ++++++++------- .../domain/card/service/CardService.java | 20 ++++++------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java index 1b12c352..03fb9168 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java @@ -1,6 +1,6 @@ package org.prgrms.nabimarketbe.domain.card.api; -import lombok.RequiredArgsConstructor; +import java.util.List; import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO; @@ -12,15 +12,18 @@ import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.SingleResult; - import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import java.util.List; - -import java.util.List; +import lombok.RequiredArgsConstructor; @RestController @RequestMapping("/api/v1/cards") @@ -45,7 +48,6 @@ public ResponseEntity> createCard( @GetMapping("/{cardId}") public ResponseEntity> getCardById(@PathVariable Long cardId) { - cardService.updateViews(cardId); CardSingleReadResponseDTO cardSingleReadResponseDTO = cardService.getCardById(cardId); return ResponseEntity.ok(ResponseFactory.getSingleResult(cardSingleReadResponseDTO)); diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java index 78fa1185..8a1b0cfb 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java @@ -1,6 +1,7 @@ package org.prgrms.nabimarketbe.domain.card.service; -import lombok.RequiredArgsConstructor; +import java.util.ArrayList; +import java.util.List; import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO; @@ -20,14 +21,11 @@ import org.prgrms.nabimarketbe.domain.item.entity.Item; import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; import org.prgrms.nabimarketbe.domain.item.repository.ItemRepository; - import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor @@ -97,11 +95,13 @@ public CardCreateResponseDTO createCard( ); } - @Transactional(readOnly = true) + @Transactional public CardSingleReadResponseDTO getCardById(Long cardId) { // TODO: User 완성되면 User에 대한 정보도 추가해서 내려주기 Card card = cardRepository.findById(cardId) .orElseThrow(); + card.updateViewCount(); + Item item = card.getItem(); // image 순서 맞춰서 응답 내려주는 부분 @@ -149,12 +149,4 @@ public CardListReadPagingResponseDTO getCardsByCondition( size ); } - - @Transactional - public void updateViews(Long cardId) { - Card card = cardRepository.findById(cardId) - .orElseThrow(() -> new RuntimeException("해당 카드가 존재하지 않습니다.")); - - card.updateViewCount(); - } } From 4caae90c8bb603615af47bcd0698706dd2158fec Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:02:45 +0900 Subject: [PATCH 06/17] =?UTF-8?q?NABI-124--feat=20:=20=EC=B0=9C=20?= =?UTF-8?q?=EC=B7=A8=EC=86=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nabimarketbe/domain/card/entity/Card.java | 7 ++++++ .../domain/card/service/CardService.java | 7 +++++- .../domain/dibs/api/DibController.java | 12 ++++++++++ .../domain/dibs/repository/DibRepository.java | 5 ++++ .../domain/dibs/service/DibService.java | 24 +++++++++++++++++-- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java index a35619a4..98f06efb 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java @@ -10,6 +10,7 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Lob; +import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.validation.constraints.Min; @@ -17,6 +18,7 @@ import javax.validation.constraints.NotNull; import org.prgrms.nabimarketbe.domain.item.entity.Item; +import org.prgrms.nabimarketbe.domain.user.entity.User; import org.prgrms.nabimarketbe.global.BaseEntity; import org.prgrms.nabimarketbe.global.error.BaseException; import org.prgrms.nabimarketbe.global.error.ErrorCode; @@ -119,7 +121,12 @@ public void updateViewCount() { this.viewCount += 1; } + // TODO : dibCount 동시성 public void increaseDibCount() { this.dibCount += 1; } + + public void decreaseDibCount() { + this.dibCount -= 1; + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java index b7353d80..50af4332 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java @@ -21,6 +21,11 @@ import org.prgrms.nabimarketbe.domain.item.entity.Item; import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; import org.prgrms.nabimarketbe.domain.item.repository.ItemRepository; +import org.prgrms.nabimarketbe.domain.user.entity.User; +import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; +import org.prgrms.nabimarketbe.domain.user.service.CheckService; +import org.prgrms.nabimarketbe.global.error.BaseException; +import org.prgrms.nabimarketbe.global.error.ErrorCode; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -58,7 +63,7 @@ public CardCreateResponseDTO createCard( .orElseThrow(); Item item = cardCreateRequestDTO.toItemEntity(category); - Card card = cardCreateRequestDTO.toCardEntity(item); + Card card = cardCreateRequestDTO.toCardEntity(item, user); Item savedItem = itemRepository.save(item); Card savedCard = cardRepository.save(card); diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java index af5d8b1c..4405b681 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java @@ -4,8 +4,10 @@ import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.service.DibService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; +import org.prgrms.nabimarketbe.global.util.model.CommonResult; import org.prgrms.nabimarketbe.global.util.model.SingleResult; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestHeader; @@ -30,4 +32,14 @@ public ResponseEntity>> create return ResponseEntity.ok(ResponseFactory.getSingleResult(dibResponseDTO)); } + + @DeleteMapping("/{cardId}") + public ResponseEntity deleteDib( + @RequestHeader(name = "authorization") String token, + @PathVariable Long cardId + ) { + dibService.deleteDib(token, cardId); + + return ResponseEntity.ok(ResponseFactory.getSuccessResult()); + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java index ccdb20f0..7ad6e89f 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java @@ -1,7 +1,12 @@ package org.prgrms.nabimarketbe.domain.dibs.repository; +import java.util.Optional; + +import org.prgrms.nabimarketbe.domain.card.entity.Card; import org.prgrms.nabimarketbe.domain.dibs.entity.Dib; +import org.prgrms.nabimarketbe.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface DibRepository extends JpaRepository { + Optional findDibByUserAndCard(User user, Card card); } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java index 46d1376e..02dff2ac 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -36,11 +36,31 @@ public DibCreateResponseDTO createDib( Card card = cardRepository.findById(cardId) .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); - card.increaseDibCount(); - Dib dib = new Dib(user, card); Dib savedDib = dibRepository.save(dib); + card.increaseDibCount(); + return DibCreateResponseDTO.from(savedDib); } + + @Transactional + public void deleteDib( + String token, + Long cardId + ) { + Long userId = checkService.parseToken(token); + User user = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); + + Card card = cardRepository.findById(cardId) + .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); + + Dib dib = dibRepository.findDibByUserAndCard(user, card) + .orElseThrow(() -> new RuntimeException("해당 찜이 존재하지 않습니다.")); + + dibRepository.delete(dib); + + card.decreaseDibCount(); + } } From 9759de6ce7c567c49f864b217dd5be71ba4af1f7 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:02:44 +0900 Subject: [PATCH 07/17] =?UTF-8?q?NABI-124--refactor=20:=20=EC=B0=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=8B=9C=20=EC=9E=90=EC=8B=A0=EC=9D=98=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EB=8A=94=20=EC=B0=9C=20=EB=AA=BB=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nabimarketbe/domain/dibs/service/DibService.java | 10 +++++++--- .../nabimarketbe/domain/user/service/CheckService.java | 9 +++++---- .../security/jwt/filter/JwtAuthenticationFilter.java | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java index 02dff2ac..fb982eef 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -29,13 +29,17 @@ public DibCreateResponseDTO createDib( String token, Long cardId ) { + Card card = cardRepository.findById(cardId) + .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); + + if(checkService.isEqual(token, card.getUser().getUserId())) { + throw new RuntimeException("자신의 카드는 찜할 수 없습니다."); + } + Long userId = checkService.parseToken(token); User user = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); - Card card = cardRepository.findById(cardId) - .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); - Dib dib = new Dib(user, card); Dib savedDib = dibRepository.save(dib); diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/CheckService.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/CheckService.java index a6862c3e..c477486a 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/CheckService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/CheckService.java @@ -20,11 +20,12 @@ public Long parseToken(String token) { return userId; } - public void checkToken(String token, Long userId) { + public boolean isEqual( + String token, + Long userId + ) { Long tokenUserId = parseToken(token); - if(!Objects.equals(tokenUserId, userId)) { - throw new RuntimeException("token과 userId가 일치하지 않습니다."); - } + return Objects.equals(tokenUserId, userId); } } diff --git a/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/filter/JwtAuthenticationFilter.java b/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/filter/JwtAuthenticationFilter.java index 71feb7cd..68f434d4 100644 --- a/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/filter/JwtAuthenticationFilter.java +++ b/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/filter/JwtAuthenticationFilter.java @@ -35,6 +35,7 @@ public void doFilter( log.info("[Verifying token]"); log.info(((HttpServletRequest) request).getRequestURL().toString()); + // 매번 필터탈때마다 select쿼리 if (token != null && jwtProvider.validationToken(token)) { Authentication authentication = jwtProvider.getAuthentication(token); SecurityContextHolder.getContext().setAuthentication(authentication); From 7759b9a0bb9d3f8941769f8f4ee75f24e20084d0 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 8 Nov 2023 20:23:35 +0900 Subject: [PATCH 08/17] =?UTF-8?q?NO-JIRA--refactor=20:=20user=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=ED=8F=AC=EB=A7=B7=20=ED=9A=8D=EC=9D=BC=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/api/UserController.java | 22 +++++++++---------- .../domain/user/service/UserService.java | 20 ++++++++++++----- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java index 637e9ea1..04fb702c 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/api/UserController.java @@ -7,6 +7,7 @@ import org.prgrms.nabimarketbe.domain.user.service.UserService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.SingleResult; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -25,32 +26,29 @@ public class UserController { private final UserService userService; @GetMapping - public SingleResult getUserByToken(@RequestHeader(name = "authorization") String token) { - UserGetResponseDTO userGetResponseDTO = userService.getUserByToken(token); - UserResponseDTO userResponseDTO = new UserResponseDTO(userGetResponseDTO); + public ResponseEntity>> getUserByToken(@RequestHeader(name = "authorization") String token) { + UserResponseDTO userResponseDTO = userService.getUserByToken(token); - return ResponseFactory.getSingleResult(userResponseDTO); + return ResponseEntity.ok(ResponseFactory.getSingleResult(userResponseDTO)); } @PutMapping("/profile-image") - public SingleResult updateUserImageUrl( + public ResponseEntity>> updateUserImageUrl( @RequestHeader(name = "authorization") String token, @RequestPart("file") MultipartFile file ) { - UserUpdateResponseDTO userUpdateResponseDTO = userService.updateUserImageUrl(token, file); - UserResponseDTO userResponseDTO = new UserResponseDTO(userUpdateResponseDTO); + UserResponseDTO userResponseDTO = userService.updateUserImageUrl(token, file); - return ResponseFactory.getSingleResult(userResponseDTO); + return ResponseEntity.ok(ResponseFactory.getSingleResult(userResponseDTO)); } @PutMapping("/nickname") - public SingleResult updateUserNickname( + public ResponseEntity>> updateUserNickname( @RequestHeader(name = "authorization") String token, @RequestBody UserUpdateRequestDTO userUpdateRequestDTO ) { - UserUpdateResponseDTO userUpdateResponseDTO = userService.updateUserNickname(token, userUpdateRequestDTO); - UserResponseDTO userResponseDTO = new UserResponseDTO(userUpdateResponseDTO); + UserResponseDTO userResponseDTO = userService.updateUserNickname(token, userUpdateRequestDTO); - return ResponseFactory.getSingleResult(userResponseDTO); + return ResponseEntity.ok(ResponseFactory.getSingleResult(userResponseDTO)); } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/UserService.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/UserService.java index f2dab469..e1b8a102 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/UserService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/UserService.java @@ -4,6 +4,7 @@ import org.prgrms.nabimarketbe.domain.user.dto.request.UserUpdateRequestDTO; import org.prgrms.nabimarketbe.domain.user.dto.response.UserGetResponseDTO; +import org.prgrms.nabimarketbe.domain.user.dto.response.UserResponseDTO; import org.prgrms.nabimarketbe.domain.user.dto.response.UserUpdateResponseDTO; import org.prgrms.nabimarketbe.domain.user.entity.User; import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; @@ -35,16 +36,19 @@ public UserGetResponseDTO findById(Long id) { } @Transactional(readOnly = true) - public UserGetResponseDTO getUserByToken(String token) { + public UserResponseDTO getUserByToken(String token) { Long userId = checkService.parseToken(token); User user = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); - return UserGetResponseDTO.from(user); + UserGetResponseDTO userGetResponseDTO = UserGetResponseDTO.from(user); + UserResponseDTO userResponseDTO = new UserResponseDTO<>(userGetResponseDTO); + + return userResponseDTO; } @Transactional - public UserUpdateResponseDTO updateUserImageUrl(String token, MultipartFile file) { + public UserResponseDTO updateUserImageUrl(String token, MultipartFile file) { Long userId = checkService.parseToken(token); User user = userRepository .findById(userId).orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); @@ -57,12 +61,14 @@ public UserUpdateResponseDTO updateUserImageUrl(String token, MultipartFile file String url = s3FileUploadService.uploadFile(Domain.USER.name(), userId, file); user.updateImageUrl(url); + UserUpdateResponseDTO userUpdateResponseDTO = UserUpdateResponseDTO.from(user); + UserResponseDTO userResponseDTO = new UserResponseDTO<>(userUpdateResponseDTO); - return UserUpdateResponseDTO.from(user); + return userResponseDTO; } @Transactional - public UserUpdateResponseDTO updateUserNickname( + public UserResponseDTO updateUserNickname( String token, @Valid UserUpdateRequestDTO userUpdateRequestDTO ) { @@ -72,7 +78,9 @@ public UserUpdateResponseDTO updateUserNickname( String updateNickname = userUpdateRequestDTO.nickname(); user.updateNickname(updateNickname); + UserUpdateResponseDTO userUpdateResponseDTO = UserUpdateResponseDTO.from(user); + UserResponseDTO userResponseDTO = new UserResponseDTO<>(userUpdateResponseDTO); - return UserUpdateResponseDTO.from(user); + return userResponseDTO; } } From 761c8b7c93af4489902ee0fead66f3db0bb411a7 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 00:30:42 +0900 Subject: [PATCH 09/17] =?UTF-8?q?NABI-124--feat=20:=20=EC=9E=90=EC=8B=A0?= =?UTF-8?q?=EC=9D=98=20=EC=B0=9C=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dibs/api/DibController.java | 16 ++++- .../DibListReadPagingResponseDTO.java | 15 +++++ .../dto/response/DibListReadResponseDTO.java | 26 +++++++ .../domain/dibs/repository/DibRepository.java | 4 +- .../dibs/repository/DibRepositoryCustom.java | 12 ++++ .../dibs/repository/DibRepositoryImpl.java | 67 +++++++++++++++++++ .../domain/dibs/service/DibService.java | 44 +++++++++++- .../user/repository/UserRepository.java | 4 +- .../setup/domain/user/UserSetUp.java | 23 +++++++ 9 files changed, 204 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadPagingResponseDTO.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java create mode 100644 src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java index 4405b681..52032e98 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java @@ -1,6 +1,7 @@ package org.prgrms.nabimarketbe.domain.dibs.api; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibCreateResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.service.DibService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; @@ -8,10 +9,12 @@ import org.prgrms.nabimarketbe.global.util.model.SingleResult; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import lombok.RequiredArgsConstructor; @@ -27,8 +30,7 @@ public ResponseEntity>> create @RequestHeader(name = "authorization") String token, @PathVariable Long cardId ) { - DibCreateResponseDTO dibCreateResponseDTO = dibService.createDib(token, cardId); - DibResponseDTO dibResponseDTO = new DibResponseDTO(dibCreateResponseDTO); + DibResponseDTO dibResponseDTO = dibService.createDib(token, cardId); return ResponseEntity.ok(ResponseFactory.getSingleResult(dibResponseDTO)); } @@ -42,4 +44,14 @@ public ResponseEntity deleteDib( return ResponseEntity.ok(ResponseFactory.getSuccessResult()); } + + @GetMapping + public ResponseEntity> getUserDibsByUserId( + @RequestHeader(name = "authorization") String token, + @RequestParam(required = false) Long cursorId + ) { + DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibService.getUserDibsByUserId(token, cursorId); + + return ResponseEntity.ok(ResponseFactory.getSingleResult(dibListReadPagingResponseDTO)); + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadPagingResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadPagingResponseDTO.java new file mode 100644 index 00000000..4c2b7131 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadPagingResponseDTO.java @@ -0,0 +1,15 @@ +package org.prgrms.nabimarketbe.domain.dibs.dto.response; + +import java.util.List; + +public record DibListReadPagingResponseDTO ( + List dibList, + Long nextCursorId +) { + public static DibListReadPagingResponseDTO of( + List dibList, + Long nextCursorId + ) { + return new DibListReadPagingResponseDTO(dibList, nextCursorId); + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java new file mode 100644 index 00000000..a47d84cd --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java @@ -0,0 +1,26 @@ +package org.prgrms.nabimarketbe.domain.dibs.dto.response; + +import java.time.LocalDateTime; + +import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Getter +public class DibListReadResponseDTO { + private Long cardId; + + private String cardTitle; + + private String itemName; + + private PriceRange priceRange; + + private String thumbNail; + + private LocalDateTime createdAt; + + private LocalDateTime modifiedAt; +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java index 7ad6e89f..4e43ec40 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java @@ -7,6 +7,8 @@ import org.prgrms.nabimarketbe.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; -public interface DibRepository extends JpaRepository { +public interface DibRepository extends JpaRepository, DibRepositoryCustom{ Optional findDibByUserAndCard(User user, Card card); + + boolean existsDibByCardAndUser(Card card, User user); } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java new file mode 100644 index 00000000..0de3d0b1 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java @@ -0,0 +1,12 @@ +package org.prgrms.nabimarketbe.domain.dibs.repository; + +import java.util.List; + +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadResponseDTO; + +public interface DibRepositoryCustom { + List getUserDibsByUserId( + Long userId, + Long cursorId + ); +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java new file mode 100644 index 00000000..4e38ee92 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java @@ -0,0 +1,67 @@ +package org.prgrms.nabimarketbe.domain.dibs.repository; + +import static org.prgrms.nabimarketbe.domain.card.entity.QCard.*; +import static org.prgrms.nabimarketbe.domain.dibs.entity.QDib.*; +import static org.prgrms.nabimarketbe.domain.item.entity.QItem.*; + +import java.util.List; + +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadResponseDTO; + +import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class DibRepositoryImpl implements DibRepositoryCustom{ + public static final int PAGE_SIZE = 10; + private final JPAQueryFactory jpaQueryFactory; + + @Override + public List getUserDibsByUserId( + Long userId, + Long cursorId + ) { + List dibList = jpaQueryFactory.select( + Projections.fields( + DibListReadResponseDTO.class, + card.cardId, + card.cardTitle, + item.itemName, + item.priceRange, + card.thumbNailImage.as("thumbNail"), + card.createdDate.as("createdAt"), + card.modifiedDate.as("modifiedAt") + ) + ) + .from(dib) + .innerJoin(card).on(card.cardId.eq(dib.card.cardId)) + .leftJoin(item).on(card.item.itemId.eq(item.itemId)) + .where( + dibUserIdEquals(userId), + greaterThan(cursorId) + ) + .limit(PAGE_SIZE) + .fetch(); + + return dibList; + } + + private BooleanExpression dibUserIdEquals(Long userId) { + if(userId == null) { + return null; + } + + return dib.user.userId.eq(userId); + } + + private BooleanExpression greaterThan(Long cursorId) { + if(cursorId == null) { + return null; + } + + return dib.dibId.gt(cursorId); + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java index fb982eef..0dfd3373 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -1,8 +1,15 @@ package org.prgrms.nabimarketbe.domain.dibs.service; +import static org.prgrms.nabimarketbe.domain.dibs.repository.DibRepositoryImpl.*; + +import java.util.List; + import org.prgrms.nabimarketbe.domain.card.entity.Card; import org.prgrms.nabimarketbe.domain.card.repository.CardRepository; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibCreateResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.entity.Dib; import org.prgrms.nabimarketbe.domain.dibs.repository.DibRepository; import org.prgrms.nabimarketbe.domain.user.entity.User; @@ -25,7 +32,7 @@ public class DibService { private final CheckService checkService; @Transactional - public DibCreateResponseDTO createDib( + public DibResponseDTO createDib( String token, Long cardId ) { @@ -40,12 +47,18 @@ public DibCreateResponseDTO createDib( User user = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); + if (dibRepository.existsDibByCardAndUser(card, user)) { + throw new RuntimeException("이미 찜한 카드는 또 찜할 수 없습니다."); + } + Dib dib = new Dib(user, card); Dib savedDib = dibRepository.save(dib); card.increaseDibCount(); - return DibCreateResponseDTO.from(savedDib); + DibCreateResponseDTO dibCreateResponseDTO = DibCreateResponseDTO.from(savedDib); + + return new DibResponseDTO<>(dibCreateResponseDTO); } @Transactional @@ -67,4 +80,31 @@ public void deleteDib( card.decreaseDibCount(); } + + @Transactional(readOnly = true) + public DibListReadPagingResponseDTO getUserDibsByUserId( + String token, + Long cursorId + ) { + Long userId = checkService.parseToken(token); + if (!userRepository.existsById(userId)) { + throw new RuntimeException("해당 회원이 없습니다."); + } + + List dibList = dibRepository.getUserDibsByUserId(userId, cursorId); + Long nextCursorId = getNextCursorId(dibList, cursorId); + + return DibListReadPagingResponseDTO.of(dibList, nextCursorId); + } + + // TODO : nextCursorId repo단에서 내려주도록 수정 ..? + private Long getNextCursorId( + List dibList, + Long cursorId + ) { + if(dibList.isEmpty() || dibList.size() < PAGE_SIZE) { + return null; + } + return cursorId + PAGE_SIZE; + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/repository/UserRepository.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/repository/UserRepository.java index 0f98fe2f..555afefd 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/repository/UserRepository.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/repository/UserRepository.java @@ -1,11 +1,11 @@ package org.prgrms.nabimarketbe.domain.user.repository; +import java.util.Optional; + import org.prgrms.nabimarketbe.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.Optional; - @Repository public interface UserRepository extends JpaRepository { Optional findByNickname(String name); diff --git a/src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java b/src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java new file mode 100644 index 00000000..679d27e4 --- /dev/null +++ b/src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java @@ -0,0 +1,23 @@ +package org.prgrms.nabimarketbe.setup.domain.user; + +import org.prgrms.nabimarketbe.domain.user.entity.User; +import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; +import org.springframework.stereotype.Component; + +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +public class UserSetUp { + + final UserRepository userRepository; + + public User saveOne() { + User user = User.builder() + .nickname("테스트닉네임") + .build(); + + User savedUser = userRepository.save(user); + return savedUser; + } +} From 6576699133969dc3591f109116983cab8beac052 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 00:42:44 +0900 Subject: [PATCH 10/17] =?UTF-8?q?NABI-124--refactor=20:=20dib=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=95=20respose=20dibId=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/DibListReadResponseDTO.java | 2 ++ .../dibs/repository/DibRepositoryCustom.java | 6 ++---- .../dibs/repository/DibRepositoryImpl.java | 15 +++++++++++-- .../domain/dibs/service/DibService.java | 21 ++----------------- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java index a47d84cd..93f4e00f 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java @@ -10,6 +10,8 @@ @NoArgsConstructor @Getter public class DibListReadResponseDTO { + private Long dibId; + private Long cardId; private String cardTitle; diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java index 0de3d0b1..17877541 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java @@ -1,11 +1,9 @@ package org.prgrms.nabimarketbe.domain.dibs.repository; -import java.util.List; - -import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadResponseDTO; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO; public interface DibRepositoryCustom { - List getUserDibsByUserId( + DibListReadPagingResponseDTO getUserDibsByUserId( Long userId, Long cursorId ); diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java index 4e38ee92..7545de39 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java @@ -6,6 +6,7 @@ import java.util.List; +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadResponseDTO; import com.querydsl.core.types.Projections; @@ -20,13 +21,14 @@ public class DibRepositoryImpl implements DibRepositoryCustom{ private final JPAQueryFactory jpaQueryFactory; @Override - public List getUserDibsByUserId( + public DibListReadPagingResponseDTO getUserDibsByUserId( Long userId, Long cursorId ) { List dibList = jpaQueryFactory.select( Projections.fields( DibListReadResponseDTO.class, + dib.dibId, card.cardId, card.cardTitle, item.itemName, @@ -46,7 +48,16 @@ public List getUserDibsByUserId( .limit(PAGE_SIZE) .fetch(); - return dibList; + Long nextCursorId = generateCursor(dibList); + + return DibListReadPagingResponseDTO.of(dibList, nextCursorId); + } + + private Long generateCursor(List dibList) { + if (dibList.size() == PAGE_SIZE) { + return dibList.get(dibList.size() - 1).getDibId() + 1; + } + return null; } private BooleanExpression dibUserIdEquals(Long userId) { diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java index 0dfd3373..40c4a6b9 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -1,14 +1,9 @@ package org.prgrms.nabimarketbe.domain.dibs.service; -import static org.prgrms.nabimarketbe.domain.dibs.repository.DibRepositoryImpl.*; - -import java.util.List; - import org.prgrms.nabimarketbe.domain.card.entity.Card; import org.prgrms.nabimarketbe.domain.card.repository.CardRepository; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibCreateResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO; -import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibResponseDTO; import org.prgrms.nabimarketbe.domain.dibs.entity.Dib; import org.prgrms.nabimarketbe.domain.dibs.repository.DibRepository; @@ -91,20 +86,8 @@ public DibListReadPagingResponseDTO getUserDibsByUserId( throw new RuntimeException("해당 회원이 없습니다."); } - List dibList = dibRepository.getUserDibsByUserId(userId, cursorId); - Long nextCursorId = getNextCursorId(dibList, cursorId); - - return DibListReadPagingResponseDTO.of(dibList, nextCursorId); - } + DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibRepository.getUserDibsByUserId(userId, cursorId); - // TODO : nextCursorId repo단에서 내려주도록 수정 ..? - private Long getNextCursorId( - List dibList, - Long cursorId - ) { - if(dibList.isEmpty() || dibList.size() < PAGE_SIZE) { - return null; - } - return cursorId + PAGE_SIZE; + return dibListReadPagingResponseDTO; } } From 49ec817283a13725fc04f357904710c7ae1c4f99 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 00:57:13 +0900 Subject: [PATCH 11/17] =?UTF-8?q?NABI-124--style=20:=20return=EB=AC=B8=20?= =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java index 7545de39..ec3e3334 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java @@ -57,6 +57,7 @@ private Long generateCursor(List dibList) { if (dibList.size() == PAGE_SIZE) { return dibList.get(dibList.size() - 1).getDibId() + 1; } + return null; } From a4a1ed3bb439ae460505db0b0b354e460be9cdd1 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 01:28:39 +0900 Subject: [PATCH 12/17] =?UTF-8?q?NABI-124--refactor=20:=20=EC=BB=A4?= =?UTF-8?q?=EC=84=9C=20=EC=83=9D=EC=84=B1=EB=A1=9C=EC=A7=81=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dibs/repository/DibRepositoryImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java index ec3e3334..216cc485 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java @@ -39,8 +39,8 @@ public DibListReadPagingResponseDTO getUserDibsByUserId( ) ) .from(dib) - .innerJoin(card).on(card.cardId.eq(dib.card.cardId)) - .leftJoin(item).on(card.item.itemId.eq(item.itemId)) + .join(card).on(card.cardId.eq(dib.card.cardId)) + .join(item).on(card.item.itemId.eq(item.itemId)) .where( dibUserIdEquals(userId), greaterThan(cursorId) @@ -55,9 +55,9 @@ public DibListReadPagingResponseDTO getUserDibsByUserId( private Long generateCursor(List dibList) { if (dibList.size() == PAGE_SIZE) { - return dibList.get(dibList.size() - 1).getDibId() + 1; + return dibList.get(dibList.size() - 1).getDibId(); } - + return null; } From 1bcdfa00e73b3fd61ae0667277976f7c9cee2a38 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:05:08 +0900 Subject: [PATCH 13/17] =?UTF-8?q?NABI-124--refactor=20:=20build.gradle=20?= =?UTF-8?q?=EB=A1=A4=EB=B0=B1,=20test=20fixture=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../setup/domain/user/UserSetUp.java | 23 ------------------- 2 files changed, 1 insertion(+), 24 deletions(-) delete mode 100644 src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java diff --git a/build.gradle b/build.gradle index 5795379c..b821c461 100644 --- a/build.gradle +++ b/build.gradle @@ -30,10 +30,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'com.auth0:java-jwt:4.4.0' - implementation 'com.mysql:mysql-connector-j' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' + runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.security:spring-security-test' diff --git a/src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java b/src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java deleted file mode 100644 index 679d27e4..00000000 --- a/src/test/java/org/prgrms/nabimarketbe/setup/domain/user/UserSetUp.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.prgrms.nabimarketbe.setup.domain.user; - -import org.prgrms.nabimarketbe.domain.user.entity.User; -import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; -import org.springframework.stereotype.Component; - -import lombok.RequiredArgsConstructor; - -@Component -@RequiredArgsConstructor -public class UserSetUp { - - final UserRepository userRepository; - - public User saveOne() { - User user = User.builder() - .nickname("테스트닉네임") - .build(); - - User savedUser = userRepository.save(user); - return savedUser; - } -} From 75ef22552cda0177f20e93a1b27ed81efaa17082 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:18:06 +0900 Subject: [PATCH 14/17] =?UTF-8?q?NABI-124--refactor=20:=20queryparam?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20size=EB=B0=9B=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/repository/CardRepositoryImpl.java | 22 ++++++++-------- .../domain/dibs/api/DibController.java | 9 +++++-- .../dibs/repository/DibRepositoryImpl.java | 25 +++++++++---------- .../domain/dibs/service/DibService.java | 16 ++++++++---- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java index c808a630..9bb4acc0 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java @@ -1,5 +1,16 @@ package org.prgrms.nabimarketbe.domain.card.repository; +import static org.prgrms.nabimarketbe.domain.card.entity.QCard.*; +import static org.prgrms.nabimarketbe.domain.item.entity.QItem.*; + +import java.util.List; + +import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadResponseDTO; +import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; +import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum; +import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; + import com.querydsl.core.types.ConstantImpl; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; @@ -7,17 +18,8 @@ import com.querydsl.core.types.dsl.StringExpressions; import com.querydsl.core.types.dsl.StringTemplate; import com.querydsl.jpa.impl.JPAQueryFactory; -import lombok.RequiredArgsConstructor; -import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; -import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadResponseDTO; -import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; -import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum; -import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; - -import java.util.List; -import static org.prgrms.nabimarketbe.domain.card.entity.QCard.card; -import static org.prgrms.nabimarketbe.domain.item.entity.QItem.item; +import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public class CardRepositoryImpl implements CardRepositoryCustom { diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java index 52032e98..d4c145cd 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java @@ -48,9 +48,14 @@ public ResponseEntity deleteDib( @GetMapping public ResponseEntity> getUserDibsByUserId( @RequestHeader(name = "authorization") String token, - @RequestParam(required = false) Long cursorId + @RequestParam(required = false) Long cursorId, + @RequestParam Integer size ) { - DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibService.getUserDibsByUserId(token, cursorId); + DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibService.getUserDibsByUserId( + token, + cursorId, + size + ); return ResponseEntity.ok(ResponseFactory.getSingleResult(dibListReadPagingResponseDTO)); } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java index 216cc485..a9c6b187 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java @@ -2,7 +2,6 @@ import static org.prgrms.nabimarketbe.domain.card.entity.QCard.*; import static org.prgrms.nabimarketbe.domain.dibs.entity.QDib.*; -import static org.prgrms.nabimarketbe.domain.item.entity.QItem.*; import java.util.List; @@ -17,13 +16,13 @@ @RequiredArgsConstructor public class DibRepositoryImpl implements DibRepositoryCustom{ - public static final int PAGE_SIZE = 10; private final JPAQueryFactory jpaQueryFactory; @Override public DibListReadPagingResponseDTO getUserDibsByUserId( Long userId, - Long cursorId + Long cursorId, + Integer size ) { List dibList = jpaQueryFactory.select( Projections.fields( @@ -31,30 +30,30 @@ public DibListReadPagingResponseDTO getUserDibsByUserId( dib.dibId, card.cardId, card.cardTitle, - item.itemName, - item.priceRange, - card.thumbNailImage.as("thumbNail"), - card.createdDate.as("createdAt"), - card.modifiedDate.as("modifiedAt") + card.item.itemName, + card.item.priceRange, + card.thumbNailImage.as("thumbNail") ) ) .from(dib) .join(card).on(card.cardId.eq(dib.card.cardId)) - .join(item).on(card.item.itemId.eq(item.itemId)) .where( dibUserIdEquals(userId), greaterThan(cursorId) ) - .limit(PAGE_SIZE) + .limit(size) .fetch(); - Long nextCursorId = generateCursor(dibList); + Long nextCursorId = generateCursor(dibList, size); return DibListReadPagingResponseDTO.of(dibList, nextCursorId); } - private Long generateCursor(List dibList) { - if (dibList.size() == PAGE_SIZE) { + private Long generateCursor( + List dibList, + Integer size + ) { + if (dibList.size() == size) { return dibList.get(dibList.size() - 1).getDibId(); } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java index 40c4a6b9..9ff7d542 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -31,6 +31,10 @@ public DibResponseDTO createDib( String token, Long cardId ) { + Long userId = checkService.parseToken(token); + User user = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); + Card card = cardRepository.findById(cardId) .orElseThrow(() -> new RuntimeException("해당 카드가 없습니다.")); @@ -38,9 +42,6 @@ public DibResponseDTO createDib( throw new RuntimeException("자신의 카드는 찜할 수 없습니다."); } - Long userId = checkService.parseToken(token); - User user = userRepository.findById(userId) - .orElseThrow(() -> new RuntimeException("해당 회원이 없습니다.")); if (dibRepository.existsDibByCardAndUser(card, user)) { throw new RuntimeException("이미 찜한 카드는 또 찜할 수 없습니다."); @@ -79,14 +80,19 @@ public void deleteDib( @Transactional(readOnly = true) public DibListReadPagingResponseDTO getUserDibsByUserId( String token, - Long cursorId + Long cursorId, + Integer size ) { Long userId = checkService.parseToken(token); if (!userRepository.existsById(userId)) { throw new RuntimeException("해당 회원이 없습니다."); } - DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibRepository.getUserDibsByUserId(userId, cursorId); + DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibRepository.getUserDibsByUserId( + userId, + cursorId, + size + ); return dibListReadPagingResponseDTO; } From e16312e03ba5814ed6e97101507dec9d301bee96 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:18:29 +0900 Subject: [PATCH 15/17] =?UTF-8?q?NABI-124--refactor=20:=20dto=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dibs/dto/response/DibListReadResponseDTO.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java index 93f4e00f..0a8cfefe 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java @@ -1,7 +1,5 @@ package org.prgrms.nabimarketbe.domain.dibs.dto.response; -import java.time.LocalDateTime; - import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; import lombok.Getter; @@ -21,8 +19,4 @@ public class DibListReadResponseDTO { private PriceRange priceRange; private String thumbNail; - - private LocalDateTime createdAt; - - private LocalDateTime modifiedAt; } From 1888d815962d04fdd295dd0cba297718c4d5d6d0 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:18:39 +0900 Subject: [PATCH 16/17] =?UTF-8?q?NABI-124--style=20:=20=EA=B0=9C=ED=96=89?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dibs/dto/response/DibCreateResponseDTO.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java index 2c7ef159..14bd131b 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java @@ -14,7 +14,6 @@ public record DibCreateResponseDTO( LocalDateTime createdAt, LocalDateTime modifiedAt ) { - public static DibCreateResponseDTO from(Dib dib) { return DibCreateResponseDTO.builder() .dibId(dib.getDibId()) From b9d5e77c8b70325ea2f774d02bd523614473410b Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:21:35 +0900 Subject: [PATCH 17/17] =?UTF-8?q?NABI-124--refactor=20:=20queryparam?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20size=EB=B0=9B=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/repository/CardRepositoryImpl.java | 22 +++++++++---------- .../dibs/repository/DibRepositoryCustom.java | 3 ++- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java index 9bb4acc0..c808a630 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java @@ -1,16 +1,5 @@ package org.prgrms.nabimarketbe.domain.card.repository; -import static org.prgrms.nabimarketbe.domain.card.entity.QCard.*; -import static org.prgrms.nabimarketbe.domain.item.entity.QItem.*; - -import java.util.List; - -import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; -import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadResponseDTO; -import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; -import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum; -import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; - import com.querydsl.core.types.ConstantImpl; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; @@ -18,8 +7,17 @@ import com.querydsl.core.types.dsl.StringExpressions; import com.querydsl.core.types.dsl.StringTemplate; import com.querydsl.jpa.impl.JPAQueryFactory; - import lombok.RequiredArgsConstructor; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadResponseDTO; +import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; +import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum; +import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; + +import java.util.List; + +import static org.prgrms.nabimarketbe.domain.card.entity.QCard.card; +import static org.prgrms.nabimarketbe.domain.item.entity.QItem.item; @RequiredArgsConstructor public class CardRepositoryImpl implements CardRepositoryCustom { diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java index 17877541..797b457d 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java @@ -5,6 +5,7 @@ public interface DibRepositoryCustom { DibListReadPagingResponseDTO getUserDibsByUserId( Long userId, - Long cursorId + Long cursorId, + Integer size ); }