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

BE/#299 구인 게시글에 조회수 기능 추가 #300

Merged
merged 3 commits into from
Nov 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.springframework.http.HttpHeaders.SET_COOKIE;

@Tag(name = "RecruitmentController", description = "프로젝트 구인 게시글 관련 API")
@RestController
@RequestMapping("api/v1/recruitments")
Expand All @@ -42,9 +46,14 @@ public ResponseEntity<ResultResponse> recruitmentAdd(@Validated @RequestBody Cre

@Operation(summary = "findRecruitment", description = "구인 게시글 상세 조회")
@GetMapping("/{recruitmentId}")
public ResponseEntity<ResultResponse> recruitmentDetails(@PathVariable Long recruitmentId) {
public ResponseEntity<ResultResponse> recruitmentDetails(@PathVariable Long recruitmentId,
HttpServletRequest request) {
GetRecruitmentDetailResponse result = recruitmentService.findRecruitmentById(recruitmentId);
return ResponseEntity.ok(ResultResponse.of(ResultCode.RECRUITMENT_GET_SUCCESS, result));
ResponseCookie cookie = recruitmentService.addViewCount(request, recruitmentId);

return ResponseEntity.ok()
.header(SET_COOKIE, cookie.toString())
.body(ResultResponse.of(ResultCode.RECRUITMENT_GET_SUCCESS, result));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public class Recruitment extends BaseEntity {
@Embedded
private RecruitmentTags recruitmentTags;

@Column(nullable = false)
private int viewCount = 0;
public void addViewCount() {
this.viewCount++;
}
public void addTag(Tags tags) {
recruitmentTags.add(this, tags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ public class GetRecruitmentDetailResponse {

private Position position;

private Integer recruitmentCount;
private int recruitmentCount;

private int viewCount;

private List<String> techTags;


public static GetRecruitmentDetailResponse from(Recruitment recruitment) {
return GetRecruitmentDetailResponse.builder()
.id(recruitment.getId())
Expand All @@ -44,6 +47,7 @@ public static GetRecruitmentDetailResponse from(Recruitment recruitment) {
.period(recruitment.getPeriod())
.position(recruitment.getPosition())
.recruitmentCount(recruitment.getRecruitmentCount())
.viewCount(recruitment.getViewCount())
.techTags(recruitment.getTagNames())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RecruitmentService {
private static final String VIEW_COUNT_COOKIE_NAME = "View_Count";
private final RecruitmentRepository recruitmentRepository;
private final RecruitmentTagService recruitmentTagService;
private final ApplicationRepository applicationRepository;
Expand Down Expand Up @@ -97,4 +102,46 @@ public List<GetApplicationResponse> findApplicationList(Long recruitmentId, Page
.map(GetApplicationResponse::from)
.collect(Collectors.toList());
}

@Transactional
public ResponseCookie addViewCount(HttpServletRequest request, Long recruitmentId) {
Recruitment recruitment = getRecruitmentById(recruitmentId);
Cookie viewCountCookie = getViewCountCookie(request);

if (viewCountCookie == null) viewCountCookie = createViewCountCookie(recruitment);
else if (isRequestForNewRecruitment(recruitmentId, viewCountCookie)) {
updateViewCount(recruitment, viewCountCookie, recruitmentId);
}

return ResponseCookie.from(viewCountCookie.getName(), viewCountCookie.getValue())
.path(viewCountCookie.getPath())
.maxAge(viewCountCookie.getMaxAge())
.build();
}

private Cookie getViewCountCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null) return null;

return Arrays.stream(cookies)
.filter(cookie -> VIEW_COUNT_COOKIE_NAME.equals(cookie.getName()))
.findFirst()
.orElse(null);
}

private Cookie createViewCountCookie(Recruitment recruitment) {
recruitment.addViewCount();
Cookie newCookie = new Cookie(VIEW_COUNT_COOKIE_NAME, "[" + recruitment.getId() + "]");
newCookie.setPath("/");
return newCookie;
}

private boolean isRequestForNewRecruitment(Long recruitmentId, Cookie viewCountCookie) {
return !viewCountCookie.getValue().contains("[" + recruitmentId + "]");
}

private void updateViewCount(Recruitment recruitment, Cookie viewCountCookie, Long recruitmentId) {
viewCountCookie.setValue(viewCountCookie.getValue() + "[" + recruitmentId + "]");
recruitment.addViewCount();
}
}
Loading