From 63fdf94f80a4f89ab548d136bd12f0cd273d2b8b Mon Sep 17 00:00:00 2001 From: HaLin Kim Date: Sat, 14 Oct 2023 09:41:57 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=ED=94=84=EB=A1=9C=EC=A0=9D?= =?UTF-8?q?=ED=8A=B8=20=EC=83=81=EC=84=B8=20=EC=A1=B0=ED=9A=8C=EC=8B=9C,?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=EC=88=98=20=EC=A6=9D=EA=B0=80=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/controller/ProjectController.java | 21 +++++++-- .../domain/project/domain/Project.java | 9 +++- .../project/service/ProjectService.java | 46 +++++++++++++++---- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/com/graphy/backend/domain/project/controller/ProjectController.java b/backend/src/main/java/com/graphy/backend/domain/project/controller/ProjectController.java index a4b129e1..7534afc5 100644 --- a/backend/src/main/java/com/graphy/backend/domain/project/controller/ProjectController.java +++ b/backend/src/main/java/com/graphy/backend/domain/project/controller/ProjectController.java @@ -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; @@ -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; @@ -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") @@ -86,9 +91,17 @@ public ResponseEntity projectList(PageRequest pageRequest, @Curr @Operation(summary = "findProject", description = "프로젝트 상세 조회") @GetMapping("/{projectId}") - public ResponseEntity projectDetails(@PathVariable Long projectId) { + public ResponseEntity 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 = "프로젝트 고도화 계획 제안") diff --git a/backend/src/main/java/com/graphy/backend/domain/project/domain/Project.java b/backend/src/main/java/com/graphy/backend/domain/project/domain/Project.java index 6f591d43..0283ea07 100644 --- a/backend/src/main/java/com/graphy/backend/domain/project/domain/Project.java +++ b/backend/src/main/java/com/graphy/backend/domain/project/domain/Project.java @@ -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) { @@ -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); diff --git a/backend/src/main/java/com/graphy/backend/domain/project/service/ProjectService.java b/backend/src/main/java/com/graphy/backend/domain/project/service/ProjectService.java index dafc4a3f..c1954dc3 100644 --- a/backend/src/main/java/com/graphy/backend/domain/project/service/ProjectService.java +++ b/backend/src/main/java/com/graphy/backend/domain/project/service/ProjectService.java @@ -21,7 +21,6 @@ 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; @@ -29,6 +28,8 @@ 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; @@ -36,7 +37,8 @@ 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; @@ -94,14 +96,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 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 findProjectList(GetProjectsRequest dto, Pageable pageable) { Page projects = projectRepository.searchProjectsWith(pageable, dto.getProjectName(), dto.getContent()); return GetProjectResponse.listOf(projects).getContent(); @@ -144,9 +176,7 @@ public CompletableFuture getProjectPlanAsync(String prompt) { } private void GptApiCall(GptCompletionRequest request, Consumer 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); From f0fa6a32b0202d55f92f2fbde7953e1e3eac8d1f Mon Sep 17 00:00:00 2001 From: HaLin Kim Date: Sat, 14 Oct 2023 09:52:30 +0900 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20pagerequest=20=EC=9C=84=EC=B9=98?= =?UTF-8?q?=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/{bookmark => comment}/domain/Bookmark.java | 2 +- .../backend/domain/job/controller/JobController.java | 9 ++++++--- .../domain/message/controller/MessageController.java | 2 +- .../notification/controller/NotificationController.java | 2 +- .../domain/notification/service/NotificationService.java | 2 +- .../recruitment/controller/RecruitmentController.java | 2 +- .../backend/global/common/{ => dto}/PageRequest.java | 2 +- .../domain/project/service/ProjectServiceTest.java | 2 +- 8 files changed, 13 insertions(+), 10 deletions(-) rename backend/src/main/java/com/graphy/backend/domain/{bookmark => comment}/domain/Bookmark.java (92%) rename backend/src/main/java/com/graphy/backend/global/common/{ => dto}/PageRequest.java (96%) diff --git a/backend/src/main/java/com/graphy/backend/domain/bookmark/domain/Bookmark.java b/backend/src/main/java/com/graphy/backend/domain/comment/domain/Bookmark.java similarity index 92% rename from backend/src/main/java/com/graphy/backend/domain/bookmark/domain/Bookmark.java rename to backend/src/main/java/com/graphy/backend/domain/comment/domain/Bookmark.java index 42fd8fb7..62e74329 100644 --- a/backend/src/main/java/com/graphy/backend/domain/bookmark/domain/Bookmark.java +++ b/backend/src/main/java/com/graphy/backend/domain/comment/domain/Bookmark.java @@ -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; diff --git a/backend/src/main/java/com/graphy/backend/domain/job/controller/JobController.java b/backend/src/main/java/com/graphy/backend/domain/job/controller/JobController.java index 12e5314e..1bae4526 100644 --- a/backend/src/main/java/com/graphy/backend/domain/job/controller/JobController.java +++ b/backend/src/main/java/com/graphy/backend/domain/job/controller/JobController.java @@ -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 diff --git a/backend/src/main/java/com/graphy/backend/domain/message/controller/MessageController.java b/backend/src/main/java/com/graphy/backend/domain/message/controller/MessageController.java index 14f1b0d4..c8e8ae38 100644 --- a/backend/src/main/java/com/graphy/backend/domain/message/controller/MessageController.java +++ b/backend/src/main/java/com/graphy/backend/domain/message/controller/MessageController.java @@ -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; diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java b/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java index 695f0232..22ca15c2 100644 --- a/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java +++ b/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java @@ -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; diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java b/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java index 12aa204d..4d4bae85 100644 --- a/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java +++ b/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java @@ -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; diff --git a/backend/src/main/java/com/graphy/backend/domain/recruitment/controller/RecruitmentController.java b/backend/src/main/java/com/graphy/backend/domain/recruitment/controller/RecruitmentController.java index 8090b454..88c08635 100644 --- a/backend/src/main/java/com/graphy/backend/domain/recruitment/controller/RecruitmentController.java +++ b/backend/src/main/java/com/graphy/backend/domain/recruitment/controller/RecruitmentController.java @@ -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; diff --git a/backend/src/main/java/com/graphy/backend/global/common/PageRequest.java b/backend/src/main/java/com/graphy/backend/global/common/dto/PageRequest.java similarity index 96% rename from backend/src/main/java/com/graphy/backend/global/common/PageRequest.java rename to backend/src/main/java/com/graphy/backend/global/common/dto/PageRequest.java index e46a8995..13d8dd2f 100644 --- a/backend/src/main/java/com/graphy/backend/global/common/PageRequest.java +++ b/backend/src/main/java/com/graphy/backend/global/common/dto/PageRequest.java @@ -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; diff --git a/backend/src/test/java/com/graphy/backend/domain/project/service/ProjectServiceTest.java b/backend/src/test/java/com/graphy/backend/domain/project/service/ProjectServiceTest.java index d388ea59..4375500e 100644 --- a/backend/src/test/java/com/graphy/backend/domain/project/service/ProjectServiceTest.java +++ b/backend/src/test/java/com/graphy/backend/domain/project/service/ProjectServiceTest.java @@ -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; From d0c9ca8d9255ca78c55eeb1c054e4c682fc968c2 Mon Sep 17 00:00:00 2001 From: HaLin Kim Date: Thu, 26 Oct 2023 13:29:32 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20schema.sql=EC=97=90=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=82=AC=ED=95=AD=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/test/resources/schema.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/test/resources/schema.sql b/backend/src/test/resources/schema.sql index 93578f08..16f92c3c 100644 --- a/backend/src/test/resources/schema.sql +++ b/backend/src/test/resources/schema.sql @@ -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,