2022.05.22 ใShopping Cart API [Ver.2]ใ
์ด์ ๋ถํฐ ์ฅ๋ฐ๊ตฌ๋ API ๊ตฌํ์ ๋ํด ๊ณ ๋ฏผ์ ์ ๋ง ๋ง์ด ํ๋ค.
๋น์ฐํ DB๋ฅผ ์ด์ฉํ๋ค ์ฟ ํค๋ก ๊ตฌํํด๋ณด๋ ค๋ ์๋๊ฐ ์ฒ์์ด๋ผ ๋ ๊ฐ๋ ์ด ๋ฏ์ค์๋ค.
์ถ๊ฐ ๋ฐ ์กฐํ๋ ๋๋ฌ๋๋ฐ, ์ค๋ ์์นจ ๋ค์ ๋ณด๋ ์ถ๊ฐ์์ ์ด์ํ ๋ก์ง์ด ์์๊ณ , ์ค๋ณต๋๋ ์ฝ๋๊ฐ ์์ด์ ์์ ์ ํ๊ณ
์ฅ๋ฐ๊ตฌ๋ ์์ ๋ฐ ์ญ์ API๋ฅผ ์์ฑํด๋ณด์๋ค.
API๋ฅผ ์ค๊ณํ๋ฉด์ ๋ฌธ์ ์ ๋ค๊ณผ ํ์คํ ์๊ฒ ๋ ์ ์ ์ ๋ฆฌํด๋ณด๊ฒ ๋ค~
์ฅ๋ฐ๊ตฌ๋ ์กฐํ
์ฅ๋ฐ๊ตฌ๋๋ฅผ ์กฐํํ๋ค๋ ๊ฒ์ ๊ณง ์ฅ๋ฐ๊ตฌ๋์ ๋ค์ด์๋ ์ํ ๋ชฉ๋ก์ ๋ฐ์ดํฐ๋ฅผ ์ป๊ฒ ๋ค๋ ๊ฒ.
๊ทธ ์ํ ๋ชฉ๋ก์ ๊ฐ์ฒด๋ฅผ ์ค๊ณํ๋ค.
public class CartItemDTO {
private final Integer productNo;
private Integer productStock;
private final String productPrice;
public CartItemDTO(Integer productNo, Integer productStock, String productPrice) {
this.productNo = productNo;
this.productStock = productStock;
this.productPrice = productPrice;
}
public Integer getProductNo() {
return productNo;
}
public Integer getProductStock() {
return productStock;
}
public String getProductPrice() {
return productPrice;
}
public void setProductStock(Integer productStock) {
this.productStock = productStock;
}
}
=> ์ต๋ํ ๋ถ๋ณ ๊ฐ์ฒด๋ก ๋ง๋๋ ๊ฒ์ด ์ข๋ค.
/**
* ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ ๋ฉ์๋
*
* @param request servlet Request
* @return Message ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก
*/
@Auth(role = Auth.Role.BASIC_USER)
@GetMapping
public Message getCartList(HttpServletRequest request) throws UnsupportedEncodingException {
Cookie[] requestCookies = request.getCookies();
List<CartItemDTO> cartList;
Cookie responseCookie;
/* ์ฟ ํค๊ฐ ์กด์ฌํ ๋ ์นดํธ ์ฟ ํค ๋ฐํ */
if (requestCookies != null) {
responseCookie = CookieUtil.getCartCookie(requestCookies);
/* ์ฅ๋ฐ๊ตฌ๋ ์ ๋ณด๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ */
if (responseCookie != null) {
String cookieValue = responseCookie.getValue();
Map<Integer, CartItemDTO> cartDTOMap = JsonUtil.stringToMap(URLDecoder.decode(cookieValue, ENC_TYPE), Integer.class, CartItemDTO.class);
if (cartDTOMap != null && !cartDTOMap.isEmpty()) {
cartList = new ArrayList<>(cartDTOMap.values());
return new Message
.Builder(cartList)
.httpStatus(HttpStatus.OK)
.mediaType(MediaType.APPLICATION_JSON)
.build();
}
}
}
return new Message
.Builder(CART_EMPTY)
.httpStatus(HttpStatus.NO_CONTENT)
.build();
}
๋๋ต์ ์ธ ํ๋ฆ
- ์๋ธ๋ฆฟ ๊ฐ์ฒด๋ฅผ ํตํด ์ฟ ํค๋ฅผ ๋ฐ์์ ์ฅ๋ฐ๊ตฌ๋ ์ฟ ํค๋ฅผ ๋ฐํํ๋ค.
- objectMapper๋ก json ํํ์ ๋ฌธ์์ด์ ๋์ฝ๋ ํด Map ๊ฐ์ฒด๋ก ๋ณํํ์ฌ List์ ๋ด๊ทผ๋ค.
- ํด๋น ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ค.
๊ทธ๋ฐ๋ฐ ์ฌ๊ธฐ์ ๋ฌธ์ ๊ฐ ์์๋ค..
์๋ JsonUtil ์ด๋ ํด๋์ค๋ ์๋ค. ๋ด๊ฐ ๋ฐ๋ก ๋ง๋ ๊ฒ์ธ๋ฐ..
๊ฒฐ๋ก ๋ถํฐ ๋งํ์๋ฉด ์ผ๋ฐ ์ ํธ ํด๋์ค๋ ์ธ์คํด์คํ์ ๊ตณ์ด Heap ์์ญ์ ์ ์ฅํ ํ์๊ฐ ์์ผ๋ Bean ๋ฑ๋ก์ ํ์ง ์๋ ๊ฒ์ด๋ค. ๋์ private ์์ฑ์๋ฅผ ์ถ๊ฐํ์.
๋ ๋ฒ์งธ ๋ฌธ์ ๋ ์ค๋ณต๋ ์ฝ๋๋ค.
์ค๋ณต๋ ์ฝ๋๋ฅผ ๋ถ๋ฆฌํด์ผ ์ ์ง๋ณด์์ ํจ์จ์ ์ธ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ผ๋ฏ๋ก ๋ฐ๋ก Utils๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ๋ค.
public class CookieUtil {
private static final String COOKIE_KEY = "Chicken";
private static final String ENC_TYPE = "utf-8";
/* ์นดํธ ์ฟ ํค ๋ฐํ ๋ฉ์๋ */
public static Cookie getCartCookie(Cookie[] requestCookies) {
for (Cookie cookie : requestCookies) {
if (COOKIE_KEY.equals(cookie.getName())) {
return cookie;
}
}
return null;
}
/* ์นดํฌ ์ฟ ํค ๊ฐ ๋์ฝ๋ฉ ํ map ๊ฐ์ฒด ๋ฐํ ๋ฉ์๋ */
public static Map<Integer, CartItemDTO> getCartItemDTOMap (Cookie responseCookie) throws UnsupportedEncodingException {
String cookieValue = responseCookie.getValue();
return JsonUtil.stringToMap(URLDecoder.decode(cookieValue, ENC_TYPE), Integer.class, CartItemDTO.class);
}
}โ
์ฅ๋ฐ๊ตฌ๋ ์์
์ฅ๋ฐ๊ตฌ๋ ์์ ์ ์ถ๊ฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฟ ํค์ ์๋ ๊ฐ์ ๋ฝ์ ๋์ฝ๋ ํ Map ๊ฐ์ฒด๋ก ๋ณํํ์ฌ ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ก ๋ง๋ค์ด ๋ฐ์์จ DTO๋ฅผ ์ด์ฉํ์ฌ ์์ ํ ๋ค์ ์ธ์ฝ๋ฉ ํ ์ฟ ํค์ ๋ฃ์ด ๋ฐํํ๋ ํ์์ด๋ค.
/**
* ์ฅ๋ฐ๊ตฌ๋ ์ํ ๋ณ๊ฒฝ ๋ฉ์๋
*
* @param modifyCartDTO ๋ณ๊ฒฝํ ์นดํธ ๊ฐ์ฒด
* @param request Servlet Request ๊ฐ์ฒด
* @param response Servlet Response ๊ฐ์ฒด
* @return ๋ณ๊ฒฝ ๋ฉ์์ง
* @throws UnsupportedEncodingException ์ธ์ฝ๋ฉ ๋ฌธ์ ์ ์์ธ ๋ฐ์
* @throws NoCartException ์ฟ ํค ์์ ๋ ์์ธ ๋ฐ์
*/
@Auth(role = Auth.Role.BASIC_USER)
@PutMapping
public ResponseEntity<String> modifyCart(@RequestBody(required = true) CartItemDTO modifyCartDTO, HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException, NoCartException {
Integer productNo = modifyCartDTO.getProductNo();
Cookie[] requestCookies = request.getCookies();
Cookie responseCookie = null;
/* ์ฟ ํค๊ฐ ์กด์ฌํ ๋ ์นดํธ ์ฟ ํค ๋ฐํ */
if (requestCookies != null) {
responseCookie = CookieUtil.getCartCookie(requestCookies);
}
if (responseCookie == null) {
throw new NoCartException(NULL_MODIFY_COOKIE);
}
Map<Integer, CartItemDTO> cartDTOMap = CookieUtil.getCartItemDTOMap(responseCookie);
/* ๊ธฐ์กด ์ํ ๊ฐ์ฒด ์ญ์ */
cartDTOMap.remove(productNo);
/* ๋์ด์จ ์ํ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐ */
cartDTOMap.put(productNo, modifyCartDTO);
responseCookie.setValue(URLEncoder.encode(JsonUtil.objectToString(cartDTOMap), ENC_TYPE));
response.addCookie(responseCookie);
return ResponseEntity.ok().body(ResponseMessage.MODIFY_MESSAGE.getValue());
}
=> ์ถ๊ฐ ํธ๋ค๋ฌ์ ์ค๋ณต๋๋ ์ฝ๋๊ฐ ์๋ค.. ์ ๊ฑธ ์ด๋ป๊ฒ ๋ถ๋ฆฌํ ๊น..
์ฝ๋ ์ค๊ณ๊ฐ ์๋ชป๋์๋๋ ์ถ๋ค.
์ฅ๋ฐ๊ตฌ๋ ์ญ์
์ฅ๋ฐ๊ตฌ๋ ์ญ์ ๋ ๋จ์ผ ์ํ์ ์ญ์ ์ด๋ฏ๋ก ๋ณ๊ฒฝ๊ณผ ๊ฑฐ์ ๋น์ทํ๋ค.
/**
* ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ญ์ ๋ฉ์๋
*
* @param deleteCartDTO ์ญ์ ํ ์นดํธ ๊ฐ์ฒด
* @param request Servlet Request ๊ฐ์ฒด
* @param response Servlet Response ๊ฐ์ฒด
* @return ์ญ์ ๋ฉ์์ง
* @throws UnsupportedEncodingException ์ธ์ฝ๋ฉ ๋ฌธ์ ์ ์์ธ ๋ฐ์
* @throws NoCartException ์ฟ ํค ์์ ๋ ์์ธ ๋ฐ์
*/
@Auth(role = Auth.Role.BASIC_USER)
@DeleteMapping
public ResponseEntity<String> deleteCart(@RequestBody CartItemDTO deleteCartDTO, HttpServletRequest request,
HttpServletResponse response) throws NoCartException, UnsupportedEncodingException {
Integer productNo = deleteCartDTO.getProductNo();
Cookie[] requestCookies = request.getCookies();
Cookie responseCookie = null;
if (requestCookies != null) {
responseCookie = CookieUtil.getCartCookie(requestCookies);
}
if (responseCookie == null) {
throw new NoCartException(NULL_REMOVE_COOKIE);
}
Map<Integer, CartItemDTO> cartDTOMap = CookieUtil.getCartItemDTOMap(responseCookie);
/* ํด๋น ์ํ ๋งต์์ ์ญ์ */
cartDTOMap.remove(productNo);
responseCookie.setValue(URLEncoder.encode(JsonUtil.objectToString(cartDTOMap), ENC_TYPE));
response.addCookie(responseCookie);
return ResponseEntity.ok().body(ResponseMessage.DELETE_MESSAGE.getValue());
}
์ค๋ณต๋ ์ฝ๋์ ๋ฆฌํฉํฐ๋ง์ ๊ฑฐ์ณ์ผ ํ ๊ฒ ๊ฐ์ ๋๋์ด ๋ ๋ค.
์ฐธ์กฐ : https://passionha.tistory.com/403?category=457136
AJAX๋ฅผ ํตํด json object ๋ฐฐ์ด๋ฐ์ดํฐ๋ฅผ controller์ List<Map>ํ๋ผ๋ฏธํฐ๋ก ๋ณด๋ด๊ธฐ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 //JSP ๋ด JS๋ถ๋ถ //f_arrAnlys ๋ฏธ๋ฆฌ ์์ฑํด์ ๋ฐ์ดํฐ ๋ฃ์ด๋ ์ ์ญ๋ฐฐ์ด function fn_exec(){ var arrPObj =..
passionha.tistory.com