Skip to content

Commit

Permalink
Merge branch 'feature' into BE/#295
Browse files Browse the repository at this point in the history
  • Loading branch information
baekhangyeol authored Nov 17, 2023
2 parents a75b49d + 9a8b1c3 commit e707f7b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
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));
}

@Operation(summary = "findRecruitmentList", description = "구인 게시글 조회")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ private void initIsRecruiting() {
@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,12 +30,15 @@ public class GetRecruitmentDetailResponse {

private Position position;

private Integer recruitmentCount;
private int recruitmentCount;

private int viewCount;

private boolean isRecruiting;

private List<String> techTags;


public static GetRecruitmentDetailResponse from(Recruitment recruitment) {
return GetRecruitmentDetailResponse.builder()
.id(recruitment.getId())
Expand All @@ -47,6 +50,7 @@ public static GetRecruitmentDetailResponse from(Recruitment recruitment) {
.position(recruitment.getPosition())
.recruitmentCount(recruitment.getRecruitmentCount())
.isRecruiting(recruitment.getIsRecruiting())
.viewCount(recruitment.getViewCount())
.techTags(recruitment.getTagNames())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public List<Recruitment> findRecruitments(List<Position> positions,

return jpaQueryFactory
.selectFrom(recruitment)
.distinct()
.where(
tagIn(tags),
positionIn(positions),
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 @@ -98,4 +103,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();
}
}

0 comments on commit e707f7b

Please sign in to comment.