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 2 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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
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.PostMapping; | ||
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; | ||
|
||
@PostMapping | ||
public ResponseEntity initCart(@CurrentUser AccountDto accountDto) { | ||
return ResponseEntity.ok(cartService.initCart(String.valueOf(accountDto.getEmail()))); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity getCart(@CurrentUser AccountDto accountDto) { | ||
return ResponseEntity.ok(cartService.getCart(String.valueOf(accountDto.getEmail()))); | ||
} | ||
|
||
@PutMapping("/add") | ||
public ResponseEntity addItem(@CurrentUser AccountDto accountDto, @RequestBody CartItem cartItem) { | ||
return ResponseEntity.ok(cartService.addItem(String.valueOf(accountDto.getEmail()), cartItem)); | ||
} | ||
|
||
@PutMapping("/remove") | ||
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,46 @@ | ||
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.stereotype.Service; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CartService { | ||
private final CartRedisRepository cartRedisRepository; | ||
|
||
public Cart initCart(String id) { | ||
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. 이 메소드는 addItem이랑 합쳐도 될것같습니다~ |
||
if (!cartRedisRepository.existsById(id)) { | ||
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 cartRedisRepository.save(Cart.of(id)); | ||
} | ||
return getCart(id); | ||
} | ||
|
||
public Cart getCart(String id) { | ||
return cartRedisRepository.findById(id).orElseThrow(EntityNotFoundException::new); | ||
} | ||
|
||
public Cart addItem(String id, CartItem cartItem) { | ||
Cart cart = getCart(id); | ||
List<CartItem> cartItemList = cart.getCartItemList(); | ||
cartItemList.add(cartItem); | ||
return cartRedisRepository.save(cart); | ||
} | ||
|
||
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.
add
,remove
같은 의미들은 메소드에 담겨있으니 URL설계를 다시 해보시는것도 좋을 것 같습니다~