This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
[#26] 장바구니 도메인 추가 #44
Open
jjeda
wants to merge
6
commits into
feature/39
Choose a base branch
from
feature/26
base: feature/39
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package me.jjeda.mall.cart.configs; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
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.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableRedisRepositories | ||
public class RedisConfig { | ||
@Value("${spring.redis.host}") | ||
private String redisHost; | ||
|
||
@Value("${spring.redis.port}") | ||
private int redisPort; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisHost, redisPort); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory, ObjectMapper objectMapper) { | ||
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(objectMapper); | ||
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>(); | ||
|
||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(serializer); | ||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(serializer); | ||
|
||
return redisTemplate; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/me/jjeda/mall/cart/controller/CartController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package me.jjeda.mall.cart.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.jjeda.mall.accounts.dto.AccountDto; | ||
import me.jjeda.mall.cart.domain.CartItem; | ||
import me.jjeda.mall.cart.service.CartService; | ||
import me.jjeda.mall.common.CurrentUser; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/carts") | ||
public class CartController { | ||
private final CartService cartService; | ||
|
||
@GetMapping | ||
public ResponseEntity getCart(@CurrentUser AccountDto accountDto) { | ||
return ResponseEntity.ok(cartService.getCart(String.valueOf(accountDto.getEmail()))); | ||
} | ||
|
||
@PutMapping("/items/new") | ||
public ResponseEntity addItem(@CurrentUser AccountDto accountDto, @RequestBody CartItem cartItem) { | ||
return ResponseEntity.ok(cartService.addItem(String.valueOf(accountDto.getEmail()), cartItem)); | ||
} | ||
|
||
@PutMapping("/items") | ||
public ResponseEntity removeItem(@CurrentUser AccountDto accountDto, @RequestBody CartItem cartItem) { | ||
return ResponseEntity.ok(cartService.removeItem(String.valueOf(accountDto.getEmail()), cartItem)); | ||
} | ||
|
||
@DeleteMapping | ||
public ResponseEntity deleteCart(@CurrentUser AccountDto accountDto) { | ||
cartService.deleteCart(String.valueOf(accountDto.getId())); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package me.jjeda.mall.cart.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Builder | ||
@RedisHash("cart") | ||
@NoArgsConstructor(access = AccessLevel.PACKAGE) | ||
@AllArgsConstructor(access = AccessLevel.PACKAGE) | ||
public class Cart { | ||
|
||
@Id | ||
private String id; | ||
|
||
private List<CartItem> cartItemList; | ||
|
||
private Cart(String id) { | ||
this.id = id; | ||
this.cartItemList = new ArrayList<>(); | ||
} | ||
|
||
public static Cart of(String id) { | ||
return new Cart(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package me.jjeda.mall.cart.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import me.jjeda.mall.items.domain.Item; | ||
|
||
@Getter | ||
@EqualsAndHashCode | ||
@Builder | ||
@AllArgsConstructor(access = AccessLevel.PACKAGE) | ||
public class CartItem { | ||
private Item item; | ||
private int price; | ||
private int quantity; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/jjeda/mall/cart/repository/CartRedisRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.jjeda.mall.cart.repository; | ||
|
||
import me.jjeda.mall.cart.domain.Cart; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface CartRedisRepository extends CrudRepository<Cart, String> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package me.jjeda.mall.cart.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.jjeda.mall.cart.domain.Cart; | ||
import me.jjeda.mall.cart.domain.CartItem; | ||
import me.jjeda.mall.cart.repository.CartRedisRepository; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.data.redis.core.RedisOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.SessionCallback; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CartService { | ||
private final CartRedisRepository cartRedisRepository; | ||
private final RedisTemplate redisTemplate; | ||
|
||
public Cart getCart(String id) { | ||
return cartRedisRepository.findById(id).orElseThrow(EntityNotFoundException::new); | ||
} | ||
|
||
public Cart addItem(String id, CartItem cartItem) { | ||
|
||
final String key = String.format("cart:%s", id); | ||
// "_class", "id" 필드를 제외하고 "cartItemList[n]" 의 필드개수 6개로 나누어주면 상품개수 | ||
int cartItemSize = (int)((redisTemplate.opsForHash().size(key) - 2) / 6); | ||
boolean hasKey = redisTemplate.hasKey(key); | ||
final Map<String, Object> map; | ||
|
||
if (hasKey) { | ||
map = convertCartItemToMap(cartItemSize,cartItem); | ||
} else { | ||
map = convertCartItemToMap(0,cartItem); | ||
} | ||
|
||
redisTemplate.execute(new SessionCallback() { | ||
@Override | ||
public Object execute(RedisOperations redisOperations) throws DataAccessException { | ||
redisOperations.watch(key); | ||
redisOperations.multi(); | ||
redisOperations.opsForHash().putAll(key,map); | ||
return redisOperations.exec(); | ||
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. 만약 중간에 예외가 발생하면 어떻게 될까요? 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. 오호 |
||
} | ||
}); | ||
|
||
return getCart(id); | ||
} | ||
|
||
private Map<String, Object> convertCartItemToMap(int size, CartItem cartItem) { | ||
Map<String, Object> map = new HashMap<>(); | ||
map.put(String.format("cartItemList.[%d].item.id", size), cartItem.getItem().getId()); | ||
map.put(String.format("cartItemList.[%d].item.name", size), cartItem.getItem().getName()); | ||
map.put(String.format("cartItemList.[%d].item.price", size), cartItem.getItem().getPrice()); | ||
map.put(String.format("cartItemList.[%d].item.stockQuantity", size), cartItem.getItem().getStockQuantity()); | ||
map.put(String.format("cartItemList.[%d].price", size), cartItem.getPrice()); | ||
map.put(String.format("cartItemList.[%d].quantity", size), cartItem.getQuantity()); | ||
return map; | ||
} | ||
|
||
public Cart removeItem(String id, CartItem cartItem) { | ||
Cart cart = getCart(id); | ||
List<CartItem> cartItemList = cart.getCartItemList(); | ||
cartItemList.remove(cartItem); | ||
|
||
return cartRedisRepository.save(cart); | ||
} | ||
|
||
public void deleteCart(String id) { | ||
cartRedisRepository.delete(getCart(id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
spring.redis.port=6379 | ||
spring.redis.host=localhost | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
2, 6 같은 수는 상수로 만들면 더 소스를 이해하기 좋을 것 같습니다