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 150f55db..93d2d504 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 @@ -3,7 +3,9 @@ import lombok.RequiredArgsConstructor; import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO; -import org.prgrms.nabimarketbe.domain.card.dto.response.*; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardSingleReadResponseDTO; import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; import org.prgrms.nabimarketbe.domain.card.service.CardService; import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum; @@ -16,6 +18,8 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import lombok.RequiredArgsConstructor; + import java.util.List; @RestController 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 887e5267..c052178c 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,22 +1,35 @@ package org.prgrms.nabimarketbe.domain.card.entity; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +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.ManyToOne; +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.domain.user.entity.User; import org.prgrms.nabimarketbe.global.BaseEntity; -import org.prgrms.nabimarketbe.domain.item.entity.Item; 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 { @@ -104,4 +117,17 @@ private Card( public void updateThumbNailImage(String url) { this.thumbNailImage = url; } + + 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 c75f37d7..4889958a 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,9 +1,12 @@ 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.*; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardSingleReadResponseDTO; import org.prgrms.nabimarketbe.domain.card.entity.Card; import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; import org.prgrms.nabimarketbe.domain.card.repository.CardRepository; @@ -18,7 +21,6 @@ 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; @@ -28,8 +30,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; -import java.util.List; +import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor @@ -107,11 +108,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 순서 맞춰서 응답 내려주는 부분 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..d4c145cd --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/api/DibController.java @@ -0,0 +1,62 @@ +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; +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.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; + +@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 + ) { + DibResponseDTO dibResponseDTO = dibService.createDib(token, cardId); + + 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()); + } + + @GetMapping + public ResponseEntity> getUserDibsByUserId( + @RequestHeader(name = "authorization") String token, + @RequestParam(required = false) Long cursorId, + @RequestParam Integer size + ) { + DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibService.getUserDibsByUserId( + token, + cursorId, + size + ); + + return ResponseEntity.ok(ResponseFactory.getSingleResult(dibListReadPagingResponseDTO)); + } +} 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..14bd131b --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibCreateResponseDTO.java @@ -0,0 +1,26 @@ +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/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..0a8cfefe --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/dto/response/DibListReadResponseDTO.java @@ -0,0 +1,22 @@ +package org.prgrms.nabimarketbe.domain.dibs.dto.response; + +import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Getter +public class DibListReadResponseDTO { + private Long dibId; + + private Long cardId; + + private String cardTitle; + + private String itemName; + + private PriceRange priceRange; + + private String thumbNail; +} 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..4e43ec40 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepository.java @@ -0,0 +1,14 @@ +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, 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..797b457d --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryCustom.java @@ -0,0 +1,11 @@ +package org.prgrms.nabimarketbe.domain.dibs.repository; + +import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO; + +public interface DibRepositoryCustom { + DibListReadPagingResponseDTO getUserDibsByUserId( + Long userId, + Long cursorId, + Integer size + ); +} 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..a9c6b187 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/repository/DibRepositoryImpl.java @@ -0,0 +1,78 @@ +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 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; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class DibRepositoryImpl implements DibRepositoryCustom{ + private final JPAQueryFactory jpaQueryFactory; + + @Override + public DibListReadPagingResponseDTO getUserDibsByUserId( + Long userId, + Long cursorId, + Integer size + ) { + List dibList = jpaQueryFactory.select( + Projections.fields( + DibListReadResponseDTO.class, + dib.dibId, + card.cardId, + card.cardTitle, + card.item.itemName, + card.item.priceRange, + card.thumbNailImage.as("thumbNail") + ) + ) + .from(dib) + .join(card).on(card.cardId.eq(dib.card.cardId)) + .where( + dibUserIdEquals(userId), + greaterThan(cursorId) + ) + .limit(size) + .fetch(); + + Long nextCursorId = generateCursor(dibList, size); + + return DibListReadPagingResponseDTO.of(dibList, nextCursorId); + } + + private Long generateCursor( + List dibList, + Integer size + ) { + if (dibList.size() == size) { + return dibList.get(dibList.size() - 1).getDibId(); + } + + return null; + } + + 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 new file mode 100644 index 00000000..9ff7d542 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/dibs/service/DibService.java @@ -0,0 +1,99 @@ +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.dto.response.DibListReadPagingResponseDTO; +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; +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; + +@Service +@RequiredArgsConstructor +public class DibService { + private final DibRepository dibRepository; + + private final UserRepository userRepository; + + private final CardRepository cardRepository; + + private final CheckService checkService; + + @Transactional + 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("해당 카드가 없습니다.")); + + if(checkService.isEqual(token, card.getUser().getUserId())) { + throw new RuntimeException("자신의 카드는 찜할 수 없습니다."); + } + + + if (dibRepository.existsDibByCardAndUser(card, user)) { + throw new RuntimeException("이미 찜한 카드는 또 찜할 수 없습니다."); + } + + Dib dib = new Dib(user, card); + Dib savedDib = dibRepository.save(dib); + + card.increaseDibCount(); + + DibCreateResponseDTO dibCreateResponseDTO = DibCreateResponseDTO.from(savedDib); + + return new DibResponseDTO<>(dibCreateResponseDTO); + } + + @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(); + } + + @Transactional(readOnly = true) + public DibListReadPagingResponseDTO getUserDibsByUserId( + String token, + Long cursorId, + Integer size + ) { + Long userId = checkService.parseToken(token); + if (!userRepository.existsById(userId)) { + throw new RuntimeException("해당 회원이 없습니다."); + } + + DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibRepository.getUserDibsByUserId( + userId, + cursorId, + size + ); + + return dibListReadPagingResponseDTO; + } +} 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..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,31 +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 = userService.updateUserImageUrl(token, file); - return ResponseFactory.getSingleResult(userUpdateResponseDTO); + 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/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/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/domain/user/service/SignService.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java index 609aae48..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) { } }); - TokenResponseDTO 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; } 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; } } 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);