Skip to content

Commit

Permalink
Merge pull request #276 from techeer-sv/BE/#270
Browse files Browse the repository at this point in the history
BE/#270 프로젝트 상세 조회 시, 조회수 증가 구현
  • Loading branch information
kimhalin authored Oct 26, 2023
2 parents bf65c62 + d0c9ca8 commit fcfd23a
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.graphy.backend.domain.bookmark.domain;
package com.graphy.backend.domain.comment.domain;

import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.recruitment.domain.Recruitment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import com.graphy.backend.domain.job.dto.response.GetJobResponse;
import com.graphy.backend.domain.job.service.JobService;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.error.ErrorCode;
import com.graphy.backend.global.error.exception.EmptyResultException;
import com.graphy.backend.global.result.ResultCode;
import com.graphy.backend.global.result.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "EmploymentController", description = "신규 채용 공고 API")
@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.graphy.backend.domain.message.dto.response.GetMessageDetailResponse;
import com.graphy.backend.domain.message.dto.response.GetMessageResponse;
import com.graphy.backend.domain.message.service.MessageService;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.result.ResultCode;
import com.graphy.backend.global.result.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.notification.dto.response.GetNotificationResponse;
import com.graphy.backend.domain.notification.service.NotificationService;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.result.ResultCode;
import com.graphy.backend.global.result.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.graphy.backend.domain.notification.dto.NotificationDto;
import com.graphy.backend.domain.notification.dto.response.GetNotificationResponse;
import com.graphy.backend.domain.notification.repository.NotificationRepository;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.error.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.graphy.backend.domain.project.controller;

import com.graphy.backend.domain.auth.util.annotation.CurrentUser;
import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.project.dto.request.CreateProjectRequest;
import com.graphy.backend.domain.project.dto.request.GetProjectPlanRequest;
Expand All @@ -10,8 +11,7 @@
import com.graphy.backend.domain.project.dto.response.GetProjectResponse;
import com.graphy.backend.domain.project.dto.response.UpdateProjectResponse;
import com.graphy.backend.domain.project.service.ProjectService;
import com.graphy.backend.domain.auth.util.annotation.CurrentUser;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.error.ErrorCode;
import com.graphy.backend.global.error.exception.EmptyResultException;
import com.graphy.backend.global.result.ResultCode;
Expand All @@ -22,14 +22,19 @@
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.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

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

@Tag(name = "ProjectController", description = "프로젝트 관련 API")
@RestController
@RequestMapping("api/v1/projects")
Expand Down Expand Up @@ -86,9 +91,17 @@ public ResponseEntity<ResultResponse> projectList(PageRequest pageRequest, @Curr

@Operation(summary = "findProject", description = "프로젝트 상세 조회")
@GetMapping("/{projectId}")
public ResponseEntity<ResultResponse> projectDetails(@PathVariable Long projectId) {
public ResponseEntity<ResultResponse> projectDetails(@PathVariable Long projectId, HttpServletRequest request) {
GetProjectDetailResponse result = projectService.findProjectById(projectId);
return ResponseEntity.ok(ResultResponse.of(ResultCode.PROJECT_GET_SUCCESS, result));
Cookie cookie = projectService.addViewCount(request, projectId);
ResponseCookie responseCookie = ResponseCookie.from(cookie.getName(), cookie.getValue())
.path(cookie.getPath())
.maxAge(cookie.getMaxAge())
.build();

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

@Operation(summary = "getProjectPlan", description = "프로젝트 고도화 계획 제안")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ public class Project extends BaseEntity {

private String thumbNail;

@Column(nullable = true)
@Column(nullable = false)
@ColumnDefault("0")
private int likeCount = 0;

@Column(nullable = false)
@ColumnDefault("0")
private int viewCount = 0;

public void updateProject(String projectName, String content,
String description, Tags tags,
String thumbNail) {
Expand All @@ -71,6 +75,9 @@ public void updateProject(String projectName, String content,
public void updateLikeCount(int amount) {
this.likeCount += amount;
}
public void addViewCount() {
this.viewCount++;
}

public void addTag(Tags tags) {
projectTags.add(this, tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@
import com.graphy.backend.global.error.exception.LongRequestException;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static com.graphy.backend.global.config.ChatGPTConfig.MAX_REQUEST_TOKEN;

@Service @Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class ProjectService {
private final ProjectRepository projectRepository;
Expand Down Expand Up @@ -96,14 +98,44 @@ public UpdateProjectResponse modifyProject(Long projectId, UpdateProjectRequest
}

public GetProjectDetailResponse findProjectById(Long projectId) {
Project project = projectRepository.findById(projectId)
.orElseThrow(() -> new EmptyResultException(ErrorCode.PROJECT_DELETED_OR_NOT_EXIST));

Project project = this.getProjectById(projectId);
List<GetCommentWithMaskingResponse> comments = commentService.findCommentListWithMasking(projectId);

return GetProjectDetailResponse.of(project, comments);
}

@Transactional
public Cookie addViewCount(HttpServletRequest request, Long projectId) {
Project project = this.getProjectById(projectId);

Cookie[] cookies = request.getCookies();
Cookie oldCookie = this.findCookie(cookies, "View_Count");

if (oldCookie != null) {
if (!oldCookie.getValue().contains("[" + projectId + "]")) {
oldCookie.setValue(oldCookie.getValue() + "[" + projectId + "]");
project.addViewCount();
}
oldCookie.setPath("/");
return oldCookie;
} else {
Cookie newCookie = new Cookie("View_Count", "[" + projectId + "]");
newCookie.setPath("/");
project.addViewCount();
return newCookie;
}
}

private Cookie findCookie(Cookie[] cookies, String cookieName) {
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookieName.equals(cookie.getName())) {
return cookie;
}
}
}
return null;
}

public List<GetProjectResponse> findProjectList(GetProjectsRequest dto, Pageable pageable) {
Page<Project> projects = projectRepository.searchProjectsWith(pageable, dto.getProjectName(), dto.getContent());
return GetProjectResponse.listOf(projects).getContent();
Expand Down Expand Up @@ -152,9 +184,7 @@ public CompletableFuture<String> getProjectPlanAsync(String prompt) {
}

private void GptApiCall(GptCompletionRequest request, Consumer<String> callback) {
log.info("비동기 작업 시작");
GptCompletionResponse result = gptChatRestService.completion(request);
log.info("비동기 작업 완료");
String response = result.getMessages().get(0).getText()
.replace("\n", " ").replace("\n\n", " ");
callback.accept(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.graphy.backend.domain.recruitment.dto.response.GetRecruitmentDetailResponse;
import com.graphy.backend.domain.recruitment.dto.response.GetRecruitmentResponse;
import com.graphy.backend.domain.recruitment.service.RecruitmentService;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.result.ResultCode;
import com.graphy.backend.global.result.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.graphy.backend.global.common;
package com.graphy.backend.global.common.dto;

import com.graphy.backend.global.error.exception.BusinessException;
import org.springframework.data.domain.Sort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.graphy.backend.domain.project.dto.response.GetProjectResponse;
import com.graphy.backend.domain.project.dto.response.UpdateProjectResponse;
import com.graphy.backend.domain.project.repository.ProjectRepository;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.common.dto.PageRequest;
import com.graphy.backend.global.error.ErrorCode;
import com.graphy.backend.global.error.exception.EmptyResultException;
import com.graphy.backend.test.MockTest;
Expand Down
1 change: 1 addition & 0 deletions backend/src/test/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ create table project
content longtext null,
description varchar(255) null,
like_count int default 0 null,
view_count int default 0 null,
project_name varchar(255) not null,
thumb_nail varchar(255) null,
member_id bigint not null,
Expand Down

0 comments on commit fcfd23a

Please sign in to comment.