- ๋จ์ด๊ฐ ์นดํ ๊ณ ๋ฆฌ์ ํฌํจ๋์ด์๊ฑฐ๋, ๊ฐ๊ฒ์ ์ด๋ฆ์ ํฌํจ๋์ด ์๋ ๋ชฉ๋ก์ ์กฐํ
- ์์ฒญ ๋ฐ๋
- ๊ฒ์๋จ์ด + ์์น ์ขํ(๋ชฉ๋ก์ ๊ฐ๊ฒ๊น์ง ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ธฐ์ํด)
- ์๋ต ๋ฐ๋
- ๊ฐ๊ฒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ ๋์๋ ์ ๋ณด๋ค๊ณผ ๋์ผํ๋ค.
- ์ปจํธ๋กค๋ฌ
- ์๋น์ค
@Transactional(rollbackFor = BaseException.class) public List<StorePreviewRes> postSearch(int userIdx, PostSearchReq searchReq) throws BaseException { // ๊ฐ๊ฒ ๋ฆฌ์คํธ ๊ฐ์ ธ์ค๊ธฐ List<GetStoreList> storeList; try { storeList = searchDao.postSearch(userIdx, searchReq); } catch (Exception e) { throw new BaseException(RESPONSE_ERROR); } List<StorePreviewRes> storePreviewRes = new ArrayList<>(); for(GetStoreList store : storeList){ // ๊ฐ๊ฒ ์ฌ์ง URL if(store.getStoreLogoUrl() != null && store.getStoreLogoUrl().length() != 0){ store.setStoreLogoUrl(""+s3Client.getUrl(bucketName, store.getStoreLogoUrl())); } if(store.getStoreSignUrl() != null && store.getStoreSignUrl().length() != 0){ store.setStoreSignUrl(""+s3Client.getUrl(bucketName, store.getStoreSignUrl())); } // ** ๋ด ์์น์์ ํด๋น ๊ฐ๊ฒ๊น์ง์ ๊ฑฐ๋ฆฌ ** int distance = (int) locationValue.getDistance( searchReq.getLatitude(), searchReq.getLongitude(), store.getY(), store.getX()); int duration = locationValue.getDuration(distance); // ๋ถ storePreviewRes.add( new StorePreviewRes( store.getStoreIdx(), store.getStoreName(), store.getStoreLogoUrl(), store.getStoreSignUrl(), store.getStar(), distance, duration, store.getCustomerIdx() != 0 ? 1:0 ) ); } // ๊ฒ์๊ธฐ๋ก ๋ง๋ค๊ธฐ try{ searchDao.serachRecord(userIdx, searchReq); }catch (Exception e) { throw new BaseException(RESPONSE_ERROR); } return storePreviewRes; }
- ๊ฒ์์ด์ ์ฐ๊ด๋ ๊ฐ๊ฒ ๋ชฉ๋ก์ ๊ฐ์ ธ์จ๋ค.
- ํด๋น ๊ฐ๊ฒ์ ์ฌ์ง ์ ๋ณด์ ๋ํ URL์ S3๋ฅผ ํตํด์ ์์ฑํ๋ค.
- ๊ฐ๊ฒ์ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ ๋์ธ ์ ๋ณด๋ฅผ ๋ง๋ค๊ธฐ
- ๊ฒ์ ํ ์ด๋ธ์ ํด๋น ๊ฒ์๊ธฐ๋ก ๋จ์ด๋ฅผ ์ ์ฅ
- Dao
// LIKE ๋ฌธ์ ์จ์ ํด๋น ๊ฒ์์ด์ ๊ด๋ จ๋ ๊ฐ๊ฒ์ด๋ฆ, ์นดํ ๊ณ ๋ฆฌ๋ฅผ ์กฐํ public List<GetStoreList> postSearch(int userIdx, PostSearchReq searchReq) { String query = "SELECT\n" + " S.storeIdx,\n" + " store_name,\n" + " store_logo_url,\n" + " sign_url,\n" + " store_address,\n" + " x, y,\n" + " ROUND(AVG(star), 1) as star,\n" + " SUB.customerIdx\n" + "FROM Stores S\n" + "LEFT JOIN Review R on S.storeIdx = R.storeIdx AND R.status != 'D'\n" + "LEFT JOIN Subscribe SUB on S.storeIdx = SUB.storeIdx AND SUB.status != 'D' AND SUB.customerIdx = ?\n" + "LEFT JOIN StroeCategories SC on S.categoryIdx = SC.categoryIdx\n" + "WHERE (store_name LIKE ? OR category_name LIKE ?)\n" + "GROUP BY (S.storeIdx)"; Object[] params = new Object[]{ userIdx, "%"+searchReq.getSearchWord()+"%", "%"+searchReq.getSearchWord()+"%" };
// ๊ฒ์๊ธฐ๋ก ํ ์ด๋ธ์ ์ฝ์ public void serachRecord(int userIdx, PostSearchReq searchReq) { String query = "INSERT INTO Search(customerIdx, search_word)\n" + "VALUES(?, ?)"; Object[] params = new Object[]{ userIdx, searchReq.getSearchWord() }; this.jdbcTemplate.update(query, params); }
- ๊ตฌ๋งค์๊ฐ ์ต๊ทผ์ ๊ฒ์ํ ๋จ์ด์, ํ์ฌ ์ ์ฒด ๊ตฌ๋งค์๋ค์ ์ธ๊ธฐ๊ฒ์์ด๋ฅผ ํ์ธํ๋ค.
- ์๋ต ๋ฐ๋
- ์ต๊ทผ ๊ฒ์์ด, ๊ธฐ์ค ์๊ฐ, ์ธ๊ธฐ ๊ฒ์์ด
- ์ปจํธ๋กค๋ฌ
- ์๋น์ค
public GetSearchRes getSearch(int userIdx) throws BaseException{ // ์ต๊ทผ ๊ฒ์ ๊ธฐ๋ก List<String> recentSearch; try{ recentSearch = searchDao.recentSearch(userIdx); }catch (Exception e) { System.out.println(e); throw new BaseException(RESPONSE_ERROR); } // ๊ธฐ์ค ์๊ฐ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH"); String date = sdf.format(new Date()); List<String> popularSearch; try{ popularSearch = searchDao.popularSearch(date); }catch (Exception e) { System.out.println(e); throw new BaseException(RESPONSE_ERROR); } return new GetSearchRes(recentSearch, date, popularSearch); }
- ๊ฒ์ ๊ธฐ๋ก ํ ์ด๋ธ์์ ๊ตฌ๋งค์์ ID๋ก ๊ฒ์๋ ๊ธฐ๋ก์ ์กฐํ
- ๊ธฐ์ค ์๊ฐ์ ํ์ฌ ์๊ฐ์ผ๋ก ๋๊ณ , DB์์ ํด๋น ์๊ฐ์ ๋ฐ์ํ ๊ฒ์ ๊ธฐ๋ก ์ ๋ณด๋ฅผ ์กฐํํ์ฌ ๋ฟ๋ฆผ.
- Dao
// ๊ตฌ๋งค์์ ์ต๊ทผ ๊ฒ์์ด ์กฐํ public List<String> recentSearch(int userIdx) { String query = "SELECT\n" + " DISTINCT(search_word)\n" + "FROM Search\n" + "WHERE customerIdx = ? AND status != 'D'\n" + "ORDER BY searchIdx DESC"; return this.jdbcTemplate.query(query, (rs, rowNum) -> rs.getString("search_word"), userIdx); }
// ์ธ๊ธฐ ๊ฒ์์ด ํ 10๊ฐ ์กฐํ public List<String> popularSearch(String date) { String query = "SELECT\n" + " search_word,\n" + " searchCount\n" + "FROM (SELECT\n" + " search_word,\n" + " COUNT(search_word) as searchCount\n" + " FROM Search\n" + " WHERE DATEDIFF(?, created) <= 2\n" + " GROUP BY search_word\n" + " LIMIT 10) R\n" + "ORDER BY R.searchCount DESC"; return this.jdbcTemplate.query(query, (rs, rowNum) -> rs.getString("search_word"), date); }
๋จ์ ๋ฐ์ดํฐ๋ฅผ ํ ์ด๋ธ์ ์ ์ฌํ๋ค๋ ๋๋์ผ๋ก ๊ณ์ํด์ ์์๊ณ , ์ง๊ณํจ์๋ฅผ ํตํด์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ธ์ด์ฌ ์ ์์๋ค.