Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.eightyage.domain.auth.entity;

import com.example.eightyage.domain.auth.tokenstate.TokenState;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.example.eightyage.domain.auth.entity.TokenState.INVALIDATED;
import static com.example.eightyage.domain.auth.tokenstate.TokenState.INVALIDATED;
import static com.example.eightyage.global.exception.ErrorMessage.EXPIRED_REFRESH_TOKEN;
import static com.example.eightyage.global.exception.ErrorMessage.REFRESH_TOKEN_NOT_FOUND;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.eightyage.domain.auth.entity;
package com.example.eightyage.domain.auth.tokenstate;

public enum TokenState {
VALID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -37,6 +38,9 @@ public class Product extends TimeStamped {
@OneToMany(mappedBy = "product")
private List<Review> reviews = new ArrayList<>();

@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime deletedAt;

@Builder
public Product(String name, Category category, String content, Integer price, SaleState saleState) {
this.name = name;
Expand Down Expand Up @@ -75,4 +79,8 @@ public void updateSaleState(SaleState newSaleState) {
this.saleState = newSaleState;
}
}

public void deleteProduct() {
this.deletedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

public interface ProductImageRepository extends JpaRepository<ProductImage, Long> {

@Query("SELECT pi FROM ProductImage pi WHERE pi.id = :imageId AND pi.deletedAt IS NULL")
@Query("SELECT pi FROM ProductImage pi WHERE pi.id = :imageId")
Optional<ProductImage> findById(Long imageId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String uploadImage(Long productId, MultipartFile file) {
public void deleteImage(Long imageId) {
ProductImage findProductImage = findProductImageByIdOrElseThrow(imageId);

findProductImage.delete();
productImageRepository.delete(findProductImage);
}

public ProductImage findProductImageByIdOrElseThrow(Long imageId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,14 @@ public void deleteProduct(Long productId) {
Product findProduct = findProductByIdOrElseThrow(productId);
List<Review> findReviewList = reviewRepository.findReviewsByProductId(productId);

for (Review review : findReviewList) {
review.delete();
}
reviewRepository.deleteAll(findReviewList);

findProduct.delete();
findProduct.deleteProduct();
}

public Product findProductByIdOrElseThrow(Long productId) {
return productRepository.findById(productId).orElseThrow(
() -> new NotFoundException("해당 제품이 존재하지 않습니다.")
);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {

@Query("SELECT r FROM Review r WHERE r.id = :reviewId AND r.deletedAt IS NULL")
@Query("SELECT r FROM Review r WHERE r.id = :reviewId")
Optional<Review> findById(@Param("reviewId") Long reviewId);

Page<Review> findByProductIdAndProductDeletedAtIsNull(Long productId, Pageable pageable);

@Query("SELECT r FROM Review r JOIN FETCH r.user JOIN FETCH r.product WHERE r.product.id = :productId AND r.deletedAt IS NULL")
@Query("SELECT r FROM Review r JOIN FETCH r.user JOIN FETCH r.product WHERE r.product.id = :productId")
List<Review> findReviewsByProductId(@Param("productId") Long productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public void deleteReview(Long userId, Long reviewId) {
User findUser = userService.findUserByIdOrElseThrow(userId);
Review findReview = findReviewByIdOrElseThrow(reviewId);

if(findUser.getId().equals(findReview.getUser().getId())){
findReview.delete();
} else {
if(!findUser.getId().equals(findReview.getUser().getId())){
throw new UnauthorizedException("리뷰를 삭제할 권한이 없습니다.");
}

reviewRepository.delete(findReview);
}

public Review findReviewByIdOrElseThrow(Long reviewId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class User extends TimeStamped {
@Enumerated(EnumType.STRING)
private UserRole userRole;

@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime deletedAt;

@Builder
public User(Long id, String email, String nickname, String password, UserRole userRole) {
this.id = id;
Expand All @@ -48,6 +51,6 @@ public static User fromAuthUser(AuthUser authUser) {
}

public void deleteUser() {
setDeletedAt(LocalDateTime.now());
this.deletedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.eightyage.global.entity;
package com.example.eightyage.global.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/example/eightyage/global/entity/TimeStamped.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,4 @@ public abstract class TimeStamped {
@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime modifiedAt;

@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime deletedAt;

public void delete() {
this.deletedAt = LocalDateTime.now();
}

public void setDeletedAt(LocalDateTime deletedAt) {
this.deletedAt = deletedAt;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.eightyage.global.exception;

public class BadRequestException extends CustomException {
public class BadRequestException extends HandledException {

public BadRequestException() {
super(ErrorCode.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.eightyage.global.exception;

public class ForbiddenException extends CustomException {
public class ForbiddenException extends HandledException {

public ForbiddenException() {
super(ErrorCode.FORBIDDEN);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.eightyage.global.exception;

import com.example.eightyage.global.entity.ErrorResponse;
import com.example.eightyage.global.dto.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,8 +20,8 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse<String>> invalidRequestExceptionException(CustomException ex) {
@ExceptionHandler(HandledException.class)
public ResponseEntity<ErrorResponse<String>> invalidRequestExceptionException(HandledException ex) {
HttpStatus httpStatus = ex.getHttpStatus();
return new ResponseEntity<>(ErrorResponse.of(httpStatus, ex.getMessage()), ex.getHttpStatus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import org.springframework.http.HttpStatus;

@Getter
public class CustomException extends RuntimeException {
public class HandledException extends RuntimeException {

private final HttpStatus httpStatus;
private final String message;

// 예외 던질시 기본 메세지 출력
public CustomException(ErrorCode errorCode) {
public HandledException(ErrorCode errorCode) {
super(errorCode.getDefaultMessage());
this.httpStatus = errorCode.getStatus();
this.message = errorCode.getDefaultMessage();
}

// 예외 던질시 메세지 출력
public CustomException(ErrorCode errorCode, String message) {
public HandledException(ErrorCode errorCode, String message) {
super(message);
this.httpStatus = errorCode.getStatus();
this.message = message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.eightyage.global.exception;

public class NotFoundException extends CustomException {
public class NotFoundException extends HandledException {
public NotFoundException() {
super(ErrorCode.NOT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.eightyage.global.exception;

public class UnauthorizedException extends CustomException {
public class UnauthorizedException extends HandledException {

public UnauthorizedException() {
super(ErrorCode.AUTHORIZATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.util.Optional;

import static com.example.eightyage.domain.auth.entity.TokenState.INVALIDATED;
import static com.example.eightyage.domain.auth.tokenstate.TokenState.INVALIDATED;
import static com.example.eightyage.global.exception.ErrorMessage.EXPIRED_REFRESH_TOKEN;
import static com.example.eightyage.global.exception.ErrorMessage.REFRESH_TOKEN_NOT_FOUND;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -120,7 +120,7 @@ public void setUp() {
RefreshToken mockRefreshToken = mock(RefreshToken.class);

given(refreshTokenRepository.findByToken(any(String.class))).willReturn(Optional.of(mockRefreshToken));
given(userService.findUserByIdOrElseThrow(mockRefreshToken.getUserId())).willReturn(user);
given(userService.findUserByIdOrElseThrow(anyLong())).willReturn(user);

// when
User result = tokenService.reissueToken(refreshToken);
Expand Down