-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
9조 BE 코드리뷰 5회차 #96
9조 BE 코드리뷰 5회차 #96
Changes from all commits
814bb5f
3b565d1
daa049c
a200558
f011449
1f79ee2
7b1104f
6298063
0cc6353
5a789bb
f01fcb6
1a7da80
1c14c81
43c1309
b4e882f
3136eae
eff003f
e0c5699
03833d3
d2b9d08
30e6488
880f720
093b1db
91ea66e
a3bc59d
b4cdd1e
27be18f
42ca391
877ea6f
5c9aaac
21db729
e423ca5
c2ece8d
0994938
f1e321a
59daef7
73be0c2
ccc701b
f0bd608
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.helpmeCookies.global.ApiResponse; | ||
|
||
import jakarta.annotation.Nullable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@RequiredArgsConstructor | ||
public class ApiResponse<T> { | ||
|
||
private final int code; | ||
private final String message; | ||
private T data; | ||
|
||
public static<T> ApiResponse<T> success(SuccessCode successCode, T data) { | ||
return new ApiResponse<T>(successCode.getCode(), successCode.getMessage(),data); | ||
} | ||
|
||
public static ApiResponse<Void> success(SuccessCode successCode) { | ||
return new ApiResponse<>(successCode.getCode(), successCode.getMessage(),null); | ||
} | ||
|
||
public static ApiResponse<Void> error(HttpStatus errorCode, String message) { | ||
return new ApiResponse<>(errorCode.value(), message,null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.helpmeCookies.global.ApiResponse; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum SuccessCode { | ||
OK(200, "OK"), | ||
CREATED_SUCCESS(201, "저장에 성공했습니다"), | ||
NO_CONTENT(204, "삭제에 성공했습니다."); | ||
|
||
private final int code; | ||
private final String message; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.helpmeCookies.global.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.helpmeCookies.global.jwt.JwtProperties; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(JwtProperties.class) | ||
public class ProPertyConfig { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.helpmeCookies.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableRedisRepositories | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate() { | ||
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.helpmeCookies.global.jwt; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "jwt") | ||
@Getter | ||
@Setter | ||
public class JwtProperties { | ||
private String secret; | ||
private long accessTokenExpireTime; | ||
private long refreshTokenExpireTime; | ||
|
||
public JwtProperties() { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToOne; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
|
@@ -25,4 +24,11 @@ public class Like { | |
@ManyToOne | ||
@JoinColumn(name = "product_id") | ||
private Product product; | ||
|
||
protected Like(){}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
public Like(User user, Product product) { | ||
this.user = user; | ||
this.product = product; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.helpmeCookies.product.repository; | ||
|
||
import com.helpmeCookies.product.entity.Like; | ||
import com.helpmeCookies.product.entity.Product; | ||
import com.helpmeCookies.user.entity.User; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ProductLikeRepository extends JpaRepository<Like, Long> { | ||
Optional<Like> findDistinctFirstByUserAndProduct(User user, Product product); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.helpmeCookies.product.service; | ||
|
||
import com.helpmeCookies.product.entity.Like; | ||
import com.helpmeCookies.product.entity.Product; | ||
import com.helpmeCookies.product.repository.ProductLikeRepository; | ||
import com.helpmeCookies.product.repository.ProductRepository; | ||
import com.helpmeCookies.user.entity.User; | ||
import com.helpmeCookies.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProductLikeService { | ||
private final ProductLikeRepository productLikeRepository; | ||
private final UserRepository userRepository; | ||
private final ProductRepository productRepository; | ||
|
||
@Transactional | ||
public void productLike(Long userId, Long productId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like를 동시에 두번 요청하면 어떻게 될까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아! 이미 좋아요 되었을 때 예외처리가 안되어 있네요..!! 감사합니다!! 관련 처리 반영하겠습니다 |
||
User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 유저Id입니다." + userId)); | ||
Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 상품Id입니다." + productId)); | ||
Like like = new Like(user, product); | ||
productLikeRepository.save(like); | ||
} | ||
|
||
@Transactional | ||
public void deleteProductLike(Long userId, Long productId) { | ||
User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 유저Id입니다." + userId)); | ||
Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 상품Id입니다." + productId)); | ||
|
||
Like like = productLikeRepository.findDistinctFirstByUserAndProduct(user,product).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 상품 찜 항목입니다.")); | ||
productLikeRepository.delete(like); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lettuce도 좋지만, Redission을 사용하시면 분산락 기능도 사용하실수 있어요~
분산락 기능이 필요없으시다면 Lettuce로도 충분한것 같습니다. 다만 분산락은 애플리케이션에서 많이 활용되는 측면이 있어서
나중에 한번 공부해보시면 좋을것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 한번 공부해보겠습니다!