Skip to content

Commit

Permalink
Merge pull request #88 from 9uttery/feat/get-my-type-joy-api-#87
Browse files Browse the repository at this point in the history
[Feature] 취향저격 소확행 조회 API 구현
  • Loading branch information
hojeong2747 authored Feb 11, 2024
2 parents df1c830 + 203a061 commit b8b0779
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.guttery.madii.domain.joy.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

@Schema(description = "취향저격 소확행 조회 요청")
public record JoyGetRecommendRequest(
@Schema(description = "WHEN 태그", example = "[1,2]")
List<Long> when,
@Schema(description = "WHO 태그", example = "[4]")
List<Long> who,
@Schema(description = "WHICH 태그", example = "[8]")
List<Long> which
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.guttery.madii.domain.joy.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "취향저격 소확행 조회 응답")
public record JoyGetRecommendResponse(
@Schema(description = "소확행 아이디", example = "11")
Long joyId,
@Schema(description = "소확행 썸네일 아이콘 번호", example = "3")
Integer joyIconNum,
@Schema(description = "소확행 내용", example = "낮잠자기")
String contents
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ public void deleteMyJoy(Long joyId, UserPrincipal userPrincipal) {
joyRepository.delete(joy);
}
}

@Transactional(readOnly = true)
public List<JoyGetRecommendResponse> getJoyRecommend(JoyGetRecommendRequest joyGetRecommendRequest, UserPrincipal userPrincipal) {
List<JoyGetRecommendResponse> joyGetRecommendResponseList = joyQueryDslRepository.getJoyRecommend(joyGetRecommendRequest);
return joyGetRecommendResponseList;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.guttery.madii.domain.joy.domain.repository;

import com.guttery.madii.domain.joy.application.dto.JoyGetMyAllResponse;
import com.guttery.madii.domain.joy.application.dto.JoyGetRecommendRequest;
import com.guttery.madii.domain.joy.application.dto.JoyGetRecommendResponse;
import com.guttery.madii.domain.joy.domain.model.Joy;

import java.util.List;
Expand All @@ -10,4 +12,5 @@ public interface JoyQueryDslRepository {

List<Joy> getRandomOfficialJoys(int amount);

List<JoyGetRecommendResponse> getJoyRecommend(JoyGetRecommendRequest joyGetRecommendRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import com.guttery.madii.common.util.OrderSpecifierUtils;
import com.guttery.madii.domain.joy.application.dto.JoyGetMyAllResponse;
import com.guttery.madii.domain.joy.application.dto.JoyGetMyOne;
import com.guttery.madii.domain.joy.application.dto.JoyGetRecommendRequest;
import com.guttery.madii.domain.joy.application.dto.JoyGetRecommendResponse;
import com.guttery.madii.domain.joy.domain.model.Joy;
import com.guttery.madii.domain.joy.domain.model.JoyType;
import com.guttery.madii.domain.joy.domain.model.QJoy;
import com.guttery.madii.domain.joy.domain.repository.JoyQueryDslRepository;
import com.guttery.madii.domain.joytag.domain.model.QJoyTag;
import com.guttery.madii.domain.tag.domain.model.QTag;
import com.guttery.madii.domain.tag.domain.model.TagType;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.StringTemplate;
Expand All @@ -16,8 +21,13 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.guttery.madii.domain.joy.domain.model.QJoy.joy;
import static com.querydsl.core.group.GroupBy.groupBy;
import static com.querydsl.core.group.GroupBy.list;

Expand Down Expand Up @@ -69,4 +79,46 @@ public List<Joy> getRandomOfficialJoys(int amount) {
.fetch();
}

@Override
public List<JoyGetRecommendResponse> getJoyRecommend(JoyGetRecommendRequest request) {
// 태그 유형별로 조건을 만족하는 Joy ID 목록을 조회
Set<Long> whenJoyIds = new HashSet<>(getJoyIdsByTagType(TagType.WHEN, request.when()));
Set<Long> whoJoyIds = new HashSet<>(getJoyIdsByTagType(TagType.WHO, request.who()));
Set<Long> whichJoyIds = new HashSet<>(getJoyIdsByTagType(TagType.WHICH, request.which()));

// 교집합 찾기
Set<Long> resultJoyIds = new HashSet<>(whenJoyIds);
if (!whoJoyIds.isEmpty()) resultJoyIds.retainAll(whoJoyIds);
if (!whichJoyIds.isEmpty()) resultJoyIds.retainAll(whichJoyIds);

// 결과 Joy 객체 조회
List<Joy> allRecommendations = queryFactory
.selectFrom(QJoy.joy)
.where(joy.joyId.in(resultJoyIds))
.fetch();

// 결과 목록을 무작위로 섞기
Collections.shuffle(allRecommendations);

// 최대 3개의 항목만 선택하여 DTO로 변환
return allRecommendations.stream()
.limit(3)
.map(joy -> new JoyGetRecommendResponse(joy.getJoyId(), joy.getJoyIconNum(), joy.getContents()))
.collect(Collectors.toList());
}

private List<Long> getJoyIdsByTagType(TagType tagType, List<Long> tagIds) {
if (tagIds == null || tagIds.isEmpty()) return Collections.emptyList();

QJoyTag qJoyTag = QJoyTag.joyTag;
QTag qTag = QTag.tag;

return queryFactory
.select(qJoyTag.joy.joyId)
.from(qJoyTag)
.join(qJoyTag.tag, qTag)
.where(qTag.tagType.eq(tagType).and(qTag.tagId.in(tagIds)))
.fetch();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.guttery.madii.domain.joy.presentation;

import com.guttery.madii.domain.joy.application.dto.JoyCreateRequest;
import com.guttery.madii.domain.joy.application.dto.JoyCreateResponse;
import com.guttery.madii.domain.joy.application.dto.JoyGetMyAllResponse;
import com.guttery.madii.domain.joy.application.dto.JoyGetTodayResponse;
import com.guttery.madii.domain.joy.application.dto.JoyPutRequest;
import com.guttery.madii.domain.joy.application.dto.JoyPutResponse;
import com.guttery.madii.domain.joy.application.dto.*;
import com.guttery.madii.domain.joy.application.service.JoyRecommendService;
import com.guttery.madii.domain.joy.application.service.JoyService;
import com.guttery.madii.domain.user.domain.model.UserPrincipal;
Expand Down Expand Up @@ -134,4 +129,20 @@ public JoyGetTodayResponse getTodayJoy(
return joyRecommendService.getTodayRecommendedJoy(date);
}

@GetMapping("/recommend")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "취향저격 소확행 조회 성공",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "취향저격 소확행 조회 API", description = "취향저격 소확행 조회 API입니다.")
public List<JoyGetRecommendResponse> getJoyRecommend(@Valid @RequestBody JoyGetRecommendRequest joyGetRecommendRequest,
@AuthenticationPrincipal final UserPrincipal userPrincipal) {
return joyService.getJoyRecommend(joyGetRecommendRequest, userPrincipal);
}

}

0 comments on commit b8b0779

Please sign in to comment.