Skip to content

Commit

Permalink
[feat] : 로그인한 유저가 북마크한 survey 조회 기능을 추가한다 (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Mar 7, 2024
1 parent e30d96a commit 91f8123
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class JwtDecryptInterceptorConfigurer implements WebMvcConfigurer {
"/v1/surveys/*/bookmarks/cancels",
"/v1/gallerys/logins",
"/v1/gallerys",
"/v1/surveys/bookmarks*",
};

@Override
Expand Down
180 changes: 180 additions & 0 deletions support/e2e/v1_12_find_bookmarked_survey.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
POST http://nalab-server:8080/v1/oauth/default # Default provider를 통해서 로그인 진행
{
"nickname": "find_bookmakred_survey1",
"email": "find_bookmakred_survey1@12345"
}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.access_token" exists
jsonpath "$.token_type" exists

[Captures]
token_type_1: jsonpath "$.token_type"
auth_token_1: jsonpath "$.access_token"

##########

POST http://nalab-server:8080/v1/surveys # 발급받은 토큰으로 survey를 생성한다.
Authorization: {{ token_type_1 }} {{ auth_token_1 }}
{
"question_count": 2,
"question": [
{
"type": "choice",
"form_type": "tendency",
"title": "저는 UI, UI, GUI 중에 어떤 분야를 가장 잘하는 것 같나요?",
"choices": [
{
"content": "UI",
"order": 1
},
{
"content": "UX",
"order": 2
},
{
"content": "GUI",
"order": 3
}
],
"max_selectable_count": 1,
"order": 1
},
{
"type": "short",
"form_type": "strength",
"title": "저는 UX, UI, GUI 중에 어떤 분야에 더 강점이 있나요?",
"order": 2
}
]
}

HTTP 201
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.survey_id" exists

[Captures]
survey_id_1: jsonpath "$.survey_id"

##########

POST http://nalab-server:8080/v1/oauth/default # Default provider를 통해서 로그인 진행
{
"nickname": "find_bookmakred_survey2",
"email": "find_bookmakred_survey2@12345"
}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.access_token" exists
jsonpath "$.token_type" exists

[Captures]
token_type_2: jsonpath "$.token_type"
auth_token_2: jsonpath "$.access_token"

##########

POST http://nalab-server:8080/v1/surveys # 발급받은 토큰으로 survey를 생성한다.
Authorization: {{ token_type_2 }} {{ auth_token_2 }}
{
"question_count": 2,
"question": [
{
"type": "choice",
"form_type": "tendency",
"title": "저는 UI, UI, GUI 중에 어떤 분야를 가장 잘하는 것 같나요?",
"choices": [
{
"content": "UI",
"order": 1
},
{
"content": "UX",
"order": 2
},
{
"content": "GUI",
"order": 3
}
],
"max_selectable_count": 1,
"order": 1
},
{
"type": "short",
"form_type": "strength",
"title": "저는 UX, UI, GUI 중에 어떤 분야에 더 강점이 있나요?",
"order": 2
}
]
}

HTTP 201
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.survey_id" exists

[Captures]
survey_id_2: jsonpath "$.survey_id"

##########

POST http://nalab-server:8080/v1/surveys/{{ survey_id_1 }}/bookmarks
Authorization: {{ token_type_1 }} {{ auth_token_1 }}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.survey_id" == {{ survey_id_1 }}

##########

POST http://nalab-server:8080/v1/surveys/{{ survey_id_2 }}/bookmarks
Authorization: {{ token_type_1 }} {{ auth_token_1 }}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.survey_id" == {{ survey_id_2 }}

##########

GET http://nalab-server:8080/v1/surveys/bookmarks
Authorization: {{ token_type_1 }} {{ auth_token_1 }}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.bookmarked_surveys.[0].survey_id" == {{ survey_id_1 }}
jsonpath "$.bookmarked_surveys.[0].nickname" == "find_bookmakred_survey1"

jsonpath "$.bookmarked_surveys.[1].survey_id" == {{ survey_id_2 }}
jsonpath "$.bookmarked_surveys.[1].nickname" == "find_bookmakred_survey2"

##########

GET http://nalab-server:8080/v1/surveys/bookmarks
Authorization: {{ token_type_1 }} {{ auth_token_1 }}

[QueryStringParams]
last-survey-id: {{ survey_id_1 }}
count: 1

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.bookmarked_surveys.[0].survey_id" == {{ survey_id_2 }}
jsonpath "$.bookmarked_surveys.[0].nickname" == "find_bookmakred_survey2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.nalab.survey.application.port.in.web.bookmark;

import java.util.List;
import me.nalab.survey.application.common.survey.dto.SurveyBookmarkDto;

public interface BookmarkedSurveyFindUseCase {

/**
* targetId를 입력받아 SurveyBookmarkDto를 반환합니다.
*/
List<SurveyBookmarkDto> findBookmarkedSurveysByTargetId(Long targetId, Long lastSurveyId, Integer count);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package me.nalab.survey.application.port.out.persistence.findtarget;

import java.util.List;
import java.util.Optional;

import java.util.Set;
import me.nalab.survey.domain.target.Target;

/**
Expand All @@ -23,4 +24,8 @@ public interface TargetFindPort {
*/
Target getTargetById(Long targetId);

/**
* surveyId 들로 Target들을 조회합니다.
*/
List<Target> findAllTargetBySurveyIds(Set<Long> surveyIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.nalab.survey.application.service.bookmark;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import me.nalab.survey.application.common.survey.dto.SurveyBookmarkDto;
import me.nalab.survey.application.port.in.web.bookmark.BookmarkedSurveyFindUseCase;
import me.nalab.survey.application.port.out.persistence.findtarget.TargetFindPort;
import me.nalab.survey.application.port.out.persistence.survey.find.SurveyFindPort;
import me.nalab.survey.domain.target.SurveyBookmark;
import me.nalab.survey.domain.target.Target;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class BookmarkedSurveyFindService implements BookmarkedSurveyFindUseCase {

private final TargetFindPort targetFindPort;
private final SurveyFindPort surveyFindPort;

@Override
@Transactional(readOnly = true)
public List<SurveyBookmarkDto> findBookmarkedSurveysByTargetId(Long targetId, Long lastSurveyId, Integer count) {
var target = targetFindPort.getTargetById(targetId);
var bookmarkedTargets = targetFindPort.findAllTargetBySurveyIds(getSurveyIds(target, lastSurveyId, count));

return bookmarkedTargets.stream()
.map(bookmarkedTarget -> {
var surveyId = bookmarkedTarget.getSurveyList().get(0).getId();
return SurveyBookmarkDto.from(surveyId, bookmarkedTarget);
})
.toList();
}

private Set<Long> getSurveyIds(Target target, Long lastSurveyId, Integer count) {
return target.getBookmarkedSurveys().stream()
.map(SurveyBookmark::surveyId)
.filter(aLong -> aLong > lastSurveyId)
.limit(count)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.nalab.survey.jpa.adaptor.common.mapper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import me.nalab.core.data.survey.SurveyEntity;
import me.nalab.core.data.target.SurveyBookmarkEntity;
import me.nalab.core.data.target.TargetEntity;
import me.nalab.survey.domain.target.SurveyBookmark;
Expand Down Expand Up @@ -40,6 +42,17 @@ public static Target toTarget(TargetEntity targetEntity) {
.build();
}

public static Target toTargetWithSurvey(TargetEntity targetEntity, SurveyEntity surveyEntity) {
return Target.builder()
.id(targetEntity.getId())
.surveyList(List.of(SurveyEntityMapper.toSurvey(surveyEntity)))
.nickname(targetEntity.getNickname())
.position(targetEntity.getPosition())
.job(targetEntity.getJob())
.bookmarkedSurveys(toSurveyBookmark(targetEntity.getBookmarkedSurveys()))
.build();
}

public static Set<SurveyBookmark> toSurveyBookmark(Set<SurveyBookmarkEntity> surveyBookmarkEntities) {
return surveyBookmarkEntities.stream()
.map(surveyBookmarkEntity -> new SurveyBookmark(surveyBookmarkEntity.getSurveyId()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package me.nalab.survey.jpa.adaptor.findtarget;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import java.util.Set;
import me.nalab.core.data.survey.SurveyEntity;
import me.nalab.survey.application.exception.TargetDoesNotExistException;
import me.nalab.survey.jpa.adaptor.findtarget.repository.TargetIdFindJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
Expand All @@ -17,11 +22,14 @@
public class TargetFindAdaptor implements TargetFindPort {

private final TargetFindJpaRepository targetFindJpaRepository;
private final TargetIdFindJpaRepository targetIdFindJpaRepository;

@Autowired
TargetFindAdaptor(
@Qualifier("findtarget.TargetFindJpaRepository") TargetFindJpaRepository targetFindJpaRepository) {
@Qualifier("findtarget.TargetFindJpaRepository") TargetFindJpaRepository targetFindJpaRepository,
@Qualifier("findtarget.TargetIdFindJpaRepository") TargetIdFindJpaRepository targetIdFindJpaRepository) {
this.targetFindJpaRepository = targetFindJpaRepository;
this.targetIdFindJpaRepository = targetIdFindJpaRepository;
}

@Override
Expand All @@ -37,4 +45,29 @@ public Target getTargetById(Long targetId) {

return TargetEntityMapper.toTarget(targetEntity);
}

@Override
public List<Target> findAllTargetBySurveyIds(Set<Long> surveyIds) {
var surveys = targetIdFindJpaRepository.findAllById(surveyIds);
var targetIds = surveys
.stream()
.map(SurveyEntity::getTargetId)
.toList();

return targetFindJpaRepository.findAllById(targetIds)
.stream()
.map(targetEntity -> {
var surveyEntity = getSurveyEntityByTargetId(targetEntity.getId(), surveys);
return TargetEntityMapper.toTargetWithSurvey(targetEntity, surveyEntity);
})
.toList();
}

private SurveyEntity getSurveyEntityByTargetId(Long targetId, List<SurveyEntity> surveyEntities) {
return surveyEntities.stream()
.filter(entity -> Objects.equals(entity.getTargetId(), targetId))
.findFirst().orElseThrow(
() -> new IllegalStateException("Cannot find exist survey")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.nalab.survey.web.adaptor.bookmark;

import lombok.RequiredArgsConstructor;
import me.nalab.survey.application.port.in.web.bookmark.BookmarkedSurveyFindUseCase;
import me.nalab.survey.web.adaptor.bookmark.response.SurveyBookmarksResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1")
@RequiredArgsConstructor
public class BookmarkedSurveyFindController {

private final BookmarkedSurveyFindUseCase bookmarkedSurveyFindUseCase;

@GetMapping("/surveys/bookmarks")
@ResponseStatus(HttpStatus.OK)
public SurveyBookmarksResponse findBookmarkedSurveys(
@RequestAttribute("logined") Long loginedTargetId,
@RequestParam(value = "last-survey-id", defaultValue = "0") Long lastSurveyId,
@RequestParam(value = "count", defaultValue = "20") Integer count
) {
var bookmarkedSurveys = bookmarkedSurveyFindUseCase.findBookmarkedSurveysByTargetId(loginedTargetId,
lastSurveyId, count);
return SurveyBookmarksResponse.of(bookmarkedSurveys);
}

}
Loading

0 comments on commit 91f8123

Please sign in to comment.