Skip to content
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

Feat/home #16

Merged
merged 7 commits into from
Jul 28, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Backend CD # actions 이름

on:
push:
branches: [ feat/error-fix ]
branches: [ feat/home ]

jobs:
deploy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public List<CartInfo> findAllCarts(String accessToken) {
Store store = storeQueryService.findStoreById(menu.getStore().getId());

CartInfo cartInfo = new CartInfo(
cart.getId(),
store.getName(),
menu.getName(),
menu.getPrice(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.likelion.apimodule.cart.application;

import com.likelion.apimodule.cart.dto.CartSaveReq;
import com.likelion.apimodule.cart.dto.CartUpdateReq;
import com.likelion.apimodule.security.util.JwtUtil;
import com.likelion.coremodule.cart.domain.Cart;
import com.likelion.coremodule.cart.exception.CartErrorCode;
import com.likelion.coremodule.cart.exception.CartException;
import com.likelion.coremodule.cart.service.CartQueryService;
import com.likelion.coremodule.menu.domain.Menu;
import com.likelion.coremodule.menu.service.MenuQueryService;
Expand All @@ -12,6 +15,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -22,15 +27,35 @@ public class CartSaveUseCase {
private final UserQueryService userQueryService;
private final MenuQueryService menuQueryService;

public Integer findMyCartCount(Long userId, Long menuId) {
return cartQueryService.findCartByUserIdAndMenuId(userId, menuId);
}

public void saveMyCart(String accessToken, CartSaveReq saveReq) {

String email = jwtUtil.getEmail(accessToken);
User user = userQueryService.findByEmail(email);

Menu menu = menuQueryService.findMenuById(saveReq.menuId());

final Cart cart = Cart.builder().user(user).menu(menu).quantity(saveReq.quantity()).build();
cartQueryService.saveCart(cart);
if (findMyCartCount(user.getUserId(), menu.getId()) >= 1) {
throw new CartException(CartErrorCode.NO_CART_INFO);
} else {
final Cart cart = Cart.builder().user(user).menu(menu).quantity(saveReq.quantity()).build();
cartQueryService.saveCart(cart);
}
}

public void updateMyCart(String accessToken, List<CartUpdateReq> updateReqs) {

String email = jwtUtil.getEmail(accessToken);
User user = userQueryService.findByEmail(email);

for (CartUpdateReq req : updateReqs) {

Cart cart = cartQueryService.findCartByUserIdAndCartId(user.getUserId(), req.cartId());
cart.setCartQuantity(req.quantity());
}
}

public void deleteMyCart(String accessToken, Long cartId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.likelion.apimodule.cart.dto;

public record CartInfo(
Long cartid, // cart
String storeName, // store
String menuName, // menu
Integer price, // menu
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.likelion.apimodule.cart.dto;

public record CartUpdateReq(
Long cartId,
Integer quantity
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.likelion.apimodule.cart.application.CartSaveUseCase;
import com.likelion.apimodule.cart.dto.CartInfo;
import com.likelion.apimodule.cart.dto.CartSaveReq;
import com.likelion.apimodule.cart.dto.CartUpdateReq;
import com.likelion.commonmodule.exception.common.ApplicationResponse;
import com.likelion.commonmodule.security.util.AuthConsts;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -64,6 +65,25 @@ public ApplicationResponse<String> saveMyCart(@RequestHeader(AuthConsts.ACCESS_T
return ApplicationResponse.ok("μž₯λ°”κ΅¬λ‹ˆ μ €μž₯ μ™„λ£Œ");
}

// μž₯λ°”κ΅¬λ‹ˆ μΆ”κ°€
@PutMapping
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "λ‚˜μ˜ μž₯λ°”κ΅¬λ‹ˆ μΆ”κ°€ 성곡",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "λ‚˜μ˜ μž₯λ°”κ΅¬λ‹ˆ μΆ”κ°€ API", description = "λ‚˜μ˜ μž₯λ°”κ΅¬λ‹ˆ μΆ”κ°€ API μž…λ‹ˆλ‹€.")
public ApplicationResponse<String> updateMyCart(@RequestHeader(AuthConsts.ACCESS_TOKEN_HEADER) String accessToken,
@RequestBody List<CartUpdateReq> updateReqs) {

cartSaveUseCase.updateMyCart(accessToken, updateReqs);
return ApplicationResponse.ok("μž₯λ°”κ΅¬λ‹ˆ μˆ˜λŸ‰ μˆ˜μ • μ™„λ£Œ");
}

// μž₯λ°”κ΅¬λ‹ˆ μ‚­μ œ
@DeleteMapping("{cartId}/delete")
@ApiResponses(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.likelion.apimodule.home.application;

import com.likelion.apimodule.home.dto.HomeInfo;
import com.likelion.apimodule.home.dto.HotListHome;
import com.likelion.apimodule.home.dto.VisitListHome;
import com.likelion.apimodule.security.util.JwtUtil;
import com.likelion.coremodule.market.domain.Market;
import com.likelion.coremodule.market.domain.MarketQrVisit;
import com.likelion.coremodule.market.service.HomeQueryService;
import com.likelion.coremodule.market.service.MarketQueryService;
import com.likelion.coremodule.store.domain.Store;
import com.likelion.coremodule.store.domain.StoreVisit;
import com.likelion.coremodule.store.service.StoreQueryService;
import com.likelion.coremodule.user.application.UserQueryService;
import com.likelion.coremodule.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@RequiredArgsConstructor
public class HomeFindUseCase {

private final HomeQueryService homeQueryService;
private final MarketQueryService marketQueryService;
private final StoreQueryService storeQueryService;
private final JwtUtil jwtUtil;
private final UserQueryService userQueryService;

public HomeInfo findMarketLists(String accessToken) {

String email = jwtUtil.getEmail(accessToken);
User user = userQueryService.findByEmail(email);

List<Market> marketList = marketQueryService.findAllMarkets();

List<VisitListHome> visitListHomeList = new ArrayList<>();
List<HotListHome> hotListHomeList = new ArrayList<>();

for (Market market : marketList) {
List<Store> storeList = storeQueryService.findStoresByMarketId(market.getId());
int totalStoreVisits = 0;

for (Store store : storeList) {
List<StoreVisit> storeVisitList = storeQueryService.findStoreVisitsByUserIdAndStoreId(user.getUserId(), store.getId());
totalStoreVisits += storeVisitList.size();
}

VisitListHome visitListHome = new VisitListHome(
market.getName(),
market.getLocation(),
storeList.size(),
totalStoreVisits,
market.getCreatedAt().toLocalDate()
);
visitListHomeList.add(visitListHome);

MarketQrVisit marketQrVisit = marketQueryService.findMarketVisit(market.getId());

HotListHome hotListHome = new HotListHome(
market.getName(),
market.getLocation(),
marketQrVisit.getQrVisit()
);
hotListHomeList.add(hotListHome);
}

// VisitListHome 리슀트λ₯Ό λ‚ μ§œ κΈ°μ€€μœΌλ‘œ μ •λ ¬
visitListHomeList.sort(Comparator.comparing(VisitListHome::visitDate));
// HotListHome 리슀트λ₯Ό qrVisit 순으둜 μ •λ ¬
hotListHomeList.sort(Comparator.comparingInt(HotListHome::totalQrVisit));

return new HomeInfo(visitListHomeList, hotListHomeList);
}

public String findNearestMarket(Integer xloc, Integer yloc) {
List<Market> marketList = marketQueryService.findAllMarkets();
String nearestMarketName = null;
double shortestDistance = Double.MAX_VALUE;

for (Market market : marketList) {
double distance = Math.sqrt(Math.pow(market.getXloc() - xloc, 2) + Math.pow(market.getYloc() - yloc, 2));
if (distance < shortestDistance) {
shortestDistance = distance;
nearestMarketName = market.getName();
}
}
return nearestMarketName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.likelion.apimodule.home.application;

import com.likelion.apimodule.security.util.JwtUtil;
import com.likelion.coremodule.market.domain.Market;
import com.likelion.coremodule.market.domain.MarketQrVisit;
import com.likelion.coremodule.market.service.HomeQueryService;
import com.likelion.coremodule.market.service.MarketQueryService;
import com.likelion.coremodule.store.domain.Store;
import com.likelion.coremodule.store.domain.StoreVisit;
import com.likelion.coremodule.store.service.StoreQueryService;
import com.likelion.coremodule.user.application.UserQueryService;
import com.likelion.coremodule.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class HomeSaveUseCase {

private final HomeQueryService homeQueryService;
private final UserQueryService userQueryService;
private final StoreQueryService storeQueryService;
private final MarketQueryService marketQueryService;
private final JwtUtil jwtUtil;

public void updateQrVisit(String accessToken, Long storeId) {

String email = jwtUtil.getEmail(accessToken);
User user = userQueryService.findByEmail(email);

Store store = storeQueryService.findStoreById(storeId);
Market market = marketQueryService.findMarket(store.getMarket().getId());

// λ°©λ¬Έμ—¬λΆ€ μ—…λ°μ΄νŠΈ
storeQueryService.saveVisitYn(store, user);
// qr 쑰회수 증가
homeQueryService.updateQrVisit(user, market);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.likelion.apimodule.home.dto;

import java.util.List;

public record HomeInfo(
List<VisitListHome> visitListHomeList,
List<HotListHome> hotListHomeList
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.likelion.apimodule.home.dto;

public record HotListHome(
String marketName,
String location,
Integer totalQrVisit
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.likelion.apimodule.home.dto;

public record TotalSumCompare(
String name,
Integer sum
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.likelion.apimodule.home.dto;

import java.time.LocalDate;

public record VisitListHome(
String marketName,
String location,
Integer visitedStore,
Integer totalStore,
LocalDate visitDate
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.likelion.apimodule.home.presentation;

import com.likelion.apimodule.home.application.HomeFindUseCase;
import com.likelion.apimodule.home.application.HomeSaveUseCase;
import com.likelion.apimodule.home.dto.HomeInfo;
import com.likelion.commonmodule.exception.common.ApplicationResponse;
import com.likelion.commonmodule.security.util.AuthConsts;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/home")
@Validated
@Tag(name = "Home", description = "Home κ΄€λ ¨ API")
public class HomeController {

private final HomeFindUseCase homeFindUseCase;
private final HomeSaveUseCase homeSaveUseCase;

// qr μ‹œ μ‹œμž₯ 쑰회수 증가
@PostMapping("/{storeId}/qrvisit")
public ApplicationResponse<String> updateQrVisit(@RequestHeader(AuthConsts.ACCESS_TOKEN_HEADER) String accessToken,
@PathVariable Long storeId) {

homeSaveUseCase.updateQrVisit(accessToken, storeId);
return ApplicationResponse.ok("qr 쑰회수 증가 + λ°©λ¬Έ μƒνƒœ λ³€κ²½");
}

// μ‹œμž₯ 리슀트 λ‚˜μ—΄
@GetMapping
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "μ‹œμž₯ 정보 확인 성곡",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "μ‹œμž₯ 정보 확인 API", description = "μ‹œμž₯ 정보 확인 API μž…λ‹ˆλ‹€.")
public ApplicationResponse<HomeInfo> findHomeInfo(@RequestHeader(AuthConsts.ACCESS_TOKEN_HEADER) String accessToken) {

final HomeInfo marketLists = homeFindUseCase.findMarketLists(accessToken);
return ApplicationResponse.ok(marketLists);
}

// μ‹œμž₯ 리슀트 검색
// @GetMapping
// @ApiResponses(
// value = {
// @ApiResponse(
// responseCode = "200",
// description = "μ‹œμž₯ 리슀트 검색 성곡",
// useReturnTypeSchema = true
// )
// }
// )
// @Operation(summary = "μ‹œμž₯ 리슀트 검색 API", description = "μ‹œμž₯ 리슀트 검색 API μž…λ‹ˆλ‹€.")
// public ApplicationResponse

// κ°€κΉŒμš΄ μ‹œμž₯ 확인
@GetMapping("/nearmarket")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "μ‹œμž₯ 정보 확인 성곡",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "μ‹œμž₯ 정보 확인 API", description = "μ‹œμž₯ 정보 확인 API μž…λ‹ˆλ‹€.")
public ApplicationResponse<String> findNearestMarket(@RequestParam Integer xloc, @RequestParam Integer yloc) {

String nearestMarket = homeFindUseCase.findNearestMarket(xloc, yloc);
return ApplicationResponse.ok(nearestMarket);
}
}
Loading
Loading