Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#61] 최근 방문한 매장 조회 기능 구현 #62

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion src/main/java/com/delfood/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 레디스 설정파일이 2개일 때 오류가 나서 임시로 지웠는데 해결하면 다시 추가하겠습니다.

public class RedisConfig {
@Value("${spring.redis.host}")
private String redisHost;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/delfood/config/ShopRedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.delfood.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class ShopRedisConfig {

@Bean
public LettuceConnectionFactory shopRedisConnectionFactory() { return new LettuceConnectionFactory(); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LettuceConnectionFactory는 따로 옵션을 설정해 주자 않는건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application.propertise에 spring.redis.port=6379 와 같이 설정을 해놓으면 자동으로 적용되어 따로 옵션을 설정해주진 않았습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bean이 제대로 주입되지 않아서 자동으로 적용되는것처럼 보이는 것 같습니다. 설정도 따로 분리하시는게 좋을 것 같네요. 명시적으로 config를 설정해주지않으면 자동으로 적용되지 않습니다


@Bean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오류가 나는 이유는 동일 타입 Bean에 이름을 정해주지 않아서입니다. 해당 Bean에 이름을 정해준다면 오류를 해결할 수 있을것으로 보이네요. 다만 그렇다면 @Autowired 를 사용하는 RedisTemplate에 @Qualifire 어노테이션을 추가하여 어떤 Bean을 사용할지 정해주어야합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요. 참고해서 다시 적용해보겠습니다

public RedisTemplate<String, Long> shopRedisTemplate() {
RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(shopRedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class));
return redisTemplate;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 옵션들을 설정한 의도를 주석으로 설명해주시면 좋을 것 같습니다~

}
}
25 changes: 17 additions & 8 deletions src/main/java/com/delfood/controller/MenuGroupController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.delfood.aop.OwnerLoginCheck;
import com.delfood.aop.OwnerShopCheck;
import com.delfood.dao.RecentShopViewDao;
import com.delfood.dto.MenuGroupDTO;
import com.delfood.dto.ShopDTO;
import com.delfood.service.MenuGroupService;
import com.delfood.service.RecentShopViewService;
import com.delfood.service.ShopService;
import com.delfood.utils.SessionUtil;

Expand Down Expand Up @@ -33,7 +35,10 @@ public class MenuGroupController {

@Autowired
MenuGroupService menuGroupService;


@Autowired
RecentShopViewService recentShopViewService;

/**
* 메뉴 관리
* - 매장 목록을 보여준다.
Expand All @@ -56,18 +61,22 @@ public ResponseEntity<List<ShopDTO>> shops(HttpSession session,

/**
* 매장 이름, 주소 및 모든 메뉴 정보를 조회한다.
* 메뉴 그룹 > 메뉴 > 상위 옵션 2개
*
*
* @author jinyoung
*
* @param shopId 매장 아이디
*/
@GetMapping("/shops/{shopId}/menuGroups/all")
@OwnerShopCheck
public ResponseEntity<ShopMenuInfoResponse> shopMenuInfo(@PathVariable("shopId") long shopId) {
public ResponseEntity<ShopMenuInfoResponse> shopMenuInfo(@PathVariable("shopId") long shopId,
HttpSession session) {

ShopDTO shopInfo = shopService.getMyShopInfo(shopId);


String memberId = SessionUtil.getLoginMemberId(session);
if (memberId != null) {
recentShopViewService.add(memberId, shopId);
}

if (shopInfo == null) {
return new ResponseEntity<MenuGroupController.ShopMenuInfoResponse>(HttpStatus.NOT_FOUND);
}
Expand Down Expand Up @@ -160,9 +169,9 @@ public ResponseEntity<String> updateMenuGroup(
@PutMapping("/shops/{shopId}/menuGroups/priority")
@OwnerShopCheck
public void updateMenuGroupPriority(
@PathVariable Long shopId, @RequestBody List<Long> iddList) {
@PathVariable Long shopId, @RequestBody List<Long> idList) {

menuGroupService.updateMenuGroupPriority(shopId, iddList);
menuGroupService.updateMenuGroupPriority(shopId, idList);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/delfood/controller/ShopSearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.delfood.dto.ShopCategoryDTO;
import com.delfood.dto.ShopDTO;
import com.delfood.service.MemberService;
import com.delfood.service.RecentShopViewService;
import com.delfood.service.ShopSearchService;
import com.delfood.service.ShopService;
import com.delfood.utils.SessionUtil;
Expand Down Expand Up @@ -31,6 +32,9 @@ public class ShopSearchController {
@Autowired
private MemberService memberService;

@Autowired
private RecentShopViewService recentShopViewService;

/**
* 메인화면에서 큰 카테고리들을 조회한다. Ex) 치킨, 피자, 중국집 등
*
Expand Down Expand Up @@ -62,6 +66,17 @@ public GetShopByCategoryIdAndTownCodeResponse getShopsByCategoryIdAndTownCode(
shopSearchService.shopSearchByCategory(categoryId, townCode));
}

/**
* 최근 방문한 shop 조회
* @param session
* @return
*/
@GetMapping("shops/recent")
@MemberLoginCheck
public List<ShopDTO> recentShopList(HttpSession session) {
String memberId = SessionUtil.getLoginMemberId(session);
return recentShopViewService.getRecentShopView(memberId);
}


// ----------------------- Response 객체 -----------------------
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/delfood/dao/RecentShopViewDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.delfood.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public class RecentShopViewDao {

private static final String RECENT_SHOP_VIEW_KEY = "recent:view:shop:";

// 최대 조회 가능한 매장수
private static final int MAX_LIST_SIZE = 10;

@Autowired
private RedisTemplate<String, Long> shopRedisTemplate;

/**
* 방문한 매장 정보를 추가한다.
*
* @param memberId 회원 아이디
* @param shopId 매장 아이디
*/
public void add(String memberId, Long shopId) {
ZSetOperations<String, Long> zSetOperations = shopRedisTemplate.opsForZSet();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZSet을 사용한 이유가 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최근 조회한 매장 목록을 불러와야 되는데 list를 사용하면 데이터의 중복이 발생하고 중복을 막기위해 그냥 set을 사용하면 최근 조회한 매장 순서로 불러올 수 가 없었기 때문에 정렬된 셋인 ZSet을 사용하였습니다.

zSetOperations.add(RECENT_SHOP_VIEW_KEY + memberId, shopId, System.currentTimeMillis());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 키를 만드는 Factory가 있으면 좋을 것 같습니다. 여러 곳에서 같은 key를 참조해야할텐데 동일한 key 생성 로직을 하나의 메서드로 만들 수 있으니까요.

zSetOperations.removeRange(RECENT_SHOP_VIEW_KEY + memberId, MAX_LIST_SIZE + 1, -1);
}

/**
* 최근 조회한 매장목록을 조회한다.
* 키에 해당하는 매장목록이 없는 경우 빈 리스트 반환
*
* @param memberId 회원 아이디
* @return 최근 조회한 매장 목록
*/
public List<Long> getRecentShopView(String memberId) {
return new ArrayList<>(Objects.requireNonNull(
shopRedisTemplate.opsForZSet().reverseRange(RECENT_SHOP_VIEW_KEY + memberId, 0, -1)));
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/delfood/mapper/ShopMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.delfood.dto.ShopDTO;
import com.delfood.dto.ShopUpdateDTO;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand Down Expand Up @@ -117,9 +119,14 @@ public interface ShopMapper {
*/
public List<ShopDTO> findByCategoryIdAndTownCode(Long categoryId, String townCode);


public List<ShopDTO> findByBeOpen(String ownerId);

public List<ShopDTO> findByBeClose(String ownerId);

/**
* 매장 조회
* @param shopIdList 매장 아이디 리스트
* @return 매장목록
*/
public List<ShopDTO> findByIdList(@Param("shopIdList") List<Long> shopIdList);
}
28 changes: 28 additions & 0 deletions src/main/java/com/delfood/service/RecentShopViewService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.delfood.service;

import com.delfood.dao.RecentShopViewDao;
import com.delfood.dto.ShopDTO;
import com.delfood.mapper.ShopMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecentShopViewService {

@Autowired
RecentShopViewDao recentShopViewDao;

@Autowired
ShopMapper shopMapper;

public void add(String memberId, Long shopId) {
recentShopViewDao.add(memberId, shopId);
}

public List<ShopDTO> getRecentShopView(String memberId) {
List<Long> shopIdList = recentShopViewDao.getRecentShopView(memberId);
return shopMapper.findByIdList(shopIdList);
}
}
16 changes: 16 additions & 0 deletions src/main/resources/mybatis/mapper/shop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,20 @@
AND owner_id = #{ownerId}
AND work_condition = 'OPEN'
</select>

<select id="findByIdList" resultType="com.delfood.dto.ShopDTO">
<include refid="selectShop"></include>
WHERE id IN (
<foreach collection="shopIdList" separator="," item="shopId">
#{shopId}
</foreach>
)
ORDER BY FIELD(id,
<foreach collection="shopIdList" separator="," item="shopId">
#{shopId}
</foreach>
)
</select>


</mapper>