๊ฐœ๋ฐœ์ผ์ง€-40 [๐Ÿ”๊ฒ€์ƒ‰ ๋„๋ฉ”์ธ: ๊ฒ€์ƒ‰ํ•˜๊ธฐ, ์ตœ๊ทผ/์ธ๊ธฐ ๊ฒ€์ƒ‰์–ด]

๊น€์„ฑ์ธยท2023๋…„ 9์›” 26์ผ
0

๊ฒ€์ƒ‰ ๋„๋ฉ”์ธ ํฌ์ŠคํŒ…



1. [POST] /jat/app/serach

๋‹จ์–ด๋กœ ๊ฐ€๊ฒŒ ๊ฒ€์ƒ‰ํ•˜๊ธฐ

  • ๋‹จ์–ด๊ฐ€ ์นดํ…Œ๊ณ ๋ฆฌ์— ํฌํ•จ๋˜์–ด์žˆ๊ฑฐ๋‚˜, ๊ฐ€๊ฒŒ์˜ ์ด๋ฆ„์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ชฉ๋ก์„ ์กฐํšŒ
  • ์š”์ฒญ ๋ฐ”๋””
    • ๊ฒ€์ƒ‰๋‹จ์–ด + ์œ„์น˜ ์ขŒํ‘œ(๋ชฉ๋ก์˜ ๊ฐ€๊ฒŒ๊นŒ์ง€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ธฐ์œ„ํ•ด)
  • ์‘๋‹ต ๋ฐ”๋””
    • ๊ฐ€๊ฒŒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์— ๋‚˜์™”๋˜ ์ •๋ณด๋“ค๊ณผ ๋™์ผํ•˜๋‹ค.
  • ์ปจํŠธ๋กค๋Ÿฌ
  • ์„œ๋น„์Šค
    @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;
    }
  1. ๊ฒ€์ƒ‰์–ด์— ์—ฐ๊ด€๋œ ๊ฐ€๊ฒŒ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ๋‹ค.
  2. ํ•ด๋‹น ๊ฐ€๊ฒŒ์˜ ์‚ฌ์ง„ ์ •๋ณด์— ๋Œ€ํ•œ URL์„ S3๋ฅผ ํ†ตํ•ด์„œ ์ƒ์„ฑํ•œ๋‹ค.
  3. ๊ฐ€๊ฒŒ์˜ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์— ๋„์šธ ์ •๋ณด๋ฅผ ๋งŒ๋“ค๊ธฐ
  4. ๊ฒ€์ƒ‰ ํ…Œ์ด๋ธ”์— ํ•ด๋‹น ๊ฒ€์ƒ‰๊ธฐ๋ก ๋‹จ์–ด๋ฅผ ์ €์žฅ
  • 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);
    }

ํ…Œ์ŠคํŠธ



2. [GET] /jat/app/search

๊ฒ€์ƒ‰ํ•˜๊ธฐ ํ™”๋ฉด์—์„œ ์ตœ๊ทผ/์ธ๊ธฐ ๊ฒ€์ƒ‰์–ด ์กฐํšŒ

  • ๊ตฌ๋งค์ž๊ฐ€ ์ตœ๊ทผ์— ๊ฒ€์ƒ‰ํ•œ ๋‹จ์–ด์™€, ํ˜„์žฌ ์ „์ฒด ๊ตฌ๋งค์ž๋“ค์˜ ์ธ๊ธฐ๊ฒ€์ƒ‰์–ด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  • ์‘๋‹ต ๋ฐ”๋””
    • ์ตœ๊ทผ ๊ฒ€์ƒ‰์–ด, ๊ธฐ์ค€ ์‹œ๊ฐ„, ์ธ๊ธฐ ๊ฒ€์ƒ‰์–ด
  • ์ปจํŠธ๋กค๋Ÿฌ
  • ์„œ๋น„์Šค
    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);
    }
  1. ๊ฒ€์ƒ‰ ๊ธฐ๋ก ํ…Œ์ด๋ธ”์—์„œ ๊ตฌ๋งค์ž์˜ ID๋กœ ๊ฒ€์ƒ‰๋œ ๊ธฐ๋ก์„ ์กฐํšŒ
  2. ๊ธฐ์ค€ ์‹œ๊ฐ„์„ ํ˜„์žฌ ์‹œ๊ฐ„์œผ๋กœ ๋‘๊ณ , 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);
    }

ํ…Œ์ŠคํŠธ



ํ›„๊ธฐ

๋‹จ์ˆœ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ”์— ์ ์žฌํ•œ๋‹ค๋Š” ๋Š๋‚Œ์œผ๋กœ ๊ณ„์†ํ•ด์„œ ์Œ“์•˜๊ณ , ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ๊ธ์–ด์˜ฌ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฟˆ์ธ 25์‚ด ๋Œ€ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€