카페 리뷰 등록, 조회, 삭제 기능 구현

 

조회 기능은 추후 정렬 기능을 추가하기 위하여 sort 전략 패턴을 사용하기 위한 구조로 구현

 

controller

/**
 * Review 등록
 * id = cafeId
 */
@PostMapping("/api/cafes/{id}/review")
public void createCafeReview(User loginUser, @PathVariable Long id, CafeReviewRequestDto requestDto) {
    cafeService.saveCafeReview(requestDto, id, loginUser);
}

/**
 * 카페별 Review 조회
 * id = cafeId
 */
@GetMapping("/api/cafes/{id}/review")
public Page<CafeReviewResponseDto> ReadCafeReviewAllByCafeId(
        @PathVariable Long id,
        @RequestParam("page") int page,
        @RequestParam("size") int size,
        @RequestParam("sortStrategyKey") String sortStrategyKey
){
    return cafeService.findCafeReviewListByCafeId(id, page, size, sortStrategyKey);
}

@DeleteMapping("/api/cafes/review/{id}")
public void deleteCafeReviewByReviewId(@PathVariable Long id){
    cafeService.removeCafeReviewByReviewId(id);
}

 

 

service

// 리뷰등록
    @Transactional
    public void saveCafeReview(CafeReviewRequestDto requestDto, Long cafeNumber, User securityUser) {
        User user = userRepoistory.getById(securityUser.getId());
        Cafe cafe = cafeRepository.findById(cafeNumber).orElseThrow(
                () -> new IllegalArgumentException(CAFE_NOT_FOUND.getMessage())
        );
        CafeReview cafeReview = new CafeReview(requestDto);
        user.addCafeReview(cafeReview);
        cafe.addCafeReview(cafeReview);

        cafeReviewRepository.save(cafeReview);
    }

    public Page<CafeReviewResponseDto> findCafeReviewListByCafeId(Long cafeNumber, int page, int size, String sortStrategyKey) {

        SortStrategy sortStrategy = sortStrategyMap.get(sortStrategyKey);
        Sort sort = sortStrategy.sort();

        Pageable pageable = PageRequest.of(page - 1, size, sort);

        Page<CafeReview> all = cafeReviewRepository.findAllByCafeId(cafeNumber ,pageable);

//        return all.map(CafeReview -> new CafeReviewResponseDto(CafeReview));
        return all.map(CafeReviewResponseDto::new);
    }

    @Transactional
    public void removeCafeReviewByReviewId(Long id) {
        cafeReviewRepository.deleteById(id);
    }

 

js


function addReview() {
    let reviewContent = $("#textarea-review").val()
    let reviewRating = $("input[type=radio][name=rating]:checked").val();
    if (reviewContent === "") {
        alert("후기를 작성해주세요");
        return;
    } else if (reviewRating === undefined) {
        alert("별점을 등록해주세요")
        return;
    }
    $.ajax({
        type: "POST",
        url: `/api/cafes/${id}/review`,
        data: {
            reviewContent: reviewContent,
            reviewRating: reviewRating
        },
        success: function (response) {
            console.log(response);
            getCafeReviewList();
        }
    })
}

function getCafeReviewList() {
    let dataSource = null;
    // 정렬 조건 추가하면 사용 // 카페 list 페이지에서 사용하는 것 참조
    // let sortStrategyKey = $("#sorting option:selected").val();
    let sortStrategyKey = "createdDateDesc";
    dataSource = `/api/cafes/${id}/review?sortStrategyKey=${sortStrategyKey}`;

    $('#review-list-container').empty();
    $('#pagination').pagination({
        dataSource,
        locator: 'content',
        alias: {
            pageNumber: 'page',
            pageSize: 'size'
        },
        totalNumberLocator: (response) => {
            return response.totalElements;
        },
        pageSize: 5,
        showPrevious: true,
        showNext: true,
        ajax: {
            beforeSend: function () {
                $('#review-list-container').html('불러오는 중...');
            }
        },
        callback: function (data, pagination) {
            $('#review-list-container').empty();
            for (let review of data) {
                // console.log(review)
                let tempHtml = makeHtmlReview(review);
                $('#review-list-container').append(tempHtml);
            }
        }
    });
}

function makeHtmlReview(review) {
    const userId = review["userId"];
    const userNickname = review["userNickname"];
    const userImage = review["userImage"];
    const cafeReviewNumber = review["cafeReviewNumber"];
    const cafeReviewContent = review["cafeReviewContent"];
    const cafeReviewRating = review["cafeReviewRating"];
    const createdDate = review["createdDate"];

    return `<div class="review-container">
                        <div class="user-img">
                            <img alt="유저 이미지" src="${userImage}">
                        </div>
                        <div class="review-info">
                            <div class="review-info-top">
                                <div class="review-username">${userNickname}</div>
                                <div class="review-rating">${cafeReviewRating}</div>
                            </div>
                            <div class="review-info-middle">${cafeReviewContent}</div>
                            <div class="review-info-bottom">
                                <div class="review-create-date">${createdDate}</div>
                                <div>
                                    <button onclick="deleteReview(${cafeReviewNumber})">삭제</button>
                                </div>
                            </div>
                        </div>
                    </div>`;
}

function deleteReview(cafeReviewNumber){
    $.ajax({
        type: "DELETE",
        url: `/api/cafes/review/${cafeReviewNumber}`,
        data: {},
        success: function (response) {
            getCafeReviewList();
        }
    })
}

 

+ Recent posts