Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 외부 api 비동기 처리 #267

Merged
merged 8 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Optional;

import org.openapitools.inference.client.model.Embedding;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.google.protobuf.Struct;
Expand Down Expand Up @@ -56,6 +57,7 @@ public List<Vector> findSimilarVector(Long memberId) {
return similarVectors.stream().toList();
}

@Async
@Override
public void save(CreateVectorDto dto) {
Vector vector =
Expand All @@ -73,6 +75,7 @@ public void save(CreateVectorDto dto) {
saveVector(vector, dto.memberId());
}

@Async
@Override
public void updateGoal(UpdateVectorGoalDto dto) {
Vector vector =
Expand All @@ -83,6 +86,7 @@ public void updateGoal(UpdateVectorGoalDto dto) {
saveVector(vector, dto.memberId());
}

@Async
@Override
public void updateQuest(UpdateVectorQuestDto dto) {
Vector vector =
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/groom/orbit/config/async/AsyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.groom.orbit.config.async;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncConfig {}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.groom.orbit.goal.app.command;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.groom.orbit.ai.app.VectorService;
import com.groom.orbit.ai.app.dto.CreateVectorDto;
import com.groom.orbit.ai.app.dto.UpdateVectorQuestDto;
import com.groom.orbit.common.dto.CommonSuccessDto;
import com.groom.orbit.common.exception.CommonException;
import com.groom.orbit.common.exception.ErrorCode;
import com.groom.orbit.goal.app.MemberGoalService;
import com.groom.orbit.goal.app.dto.request.CreateQuestRequestDto;
import com.groom.orbit.goal.app.dto.request.UpdateQuestRequestDto;
import com.groom.orbit.goal.app.dto.response.CreateQuestResponse;
import com.groom.orbit.goal.app.query.QuestQueryService;
import com.groom.orbit.goal.dao.QuestRepository;
Expand All @@ -33,49 +31,48 @@ public class QuestCommandService {
/** TODO join 최적화 */
public CreateQuestResponse createQuest(Long memberId, CreateQuestRequestDto dto) {
MemberGoal memberGoal = memberGoalService.findMemberGoal(memberId, dto.goalId());
memberGoal.validateMember(memberId);

int newQuestSequence = questQueryService.getQuestCountsByGoalId(dto.goalId()) + 1;
Quest quest = Quest.create(dto.title(), memberGoal, dto.deadline(), newQuestSequence);

questRepository.save(quest);
saveVector(memberId, dto);

return CreateQuestResponse.fromEntity(quest);
}

private void saveVector(Long memberId, CreateQuestRequestDto dto) {
CreateVectorDto vectorDto =
CreateVectorDto.builder().memberId(memberId).quest(dto.title()).build();
vectorService.save(vectorDto);
}

/** select 최적화 */
public CommonSuccessDto deleteQuest(Long memberId, Long questId, Long goalId) {
List<Quest> quests = questQueryService.findQuestsByMemberAndGoal(memberId, goalId);
Quest removeQuest =
quests.stream()
.filter(q -> q.getQuestId().equals(questId))
.findFirst()
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_QUEST));
Integer removeSequence = removeQuest.getSequence();
updateSequence(quests, removeSequence);
public CommonSuccessDto deleteOneQuest(Long memberId, Long questId) {
Quest quest = questQueryService.findQuest(questId);

questRepository.delete(removeQuest);
quest.validateMember(memberId);
questRepository.delete(quest);

return CommonSuccessDto.fromEntity(true);
}

private static void updateSequence(List<Quest> quests, Integer removeSequence) {
for (Quest quest : quests) {
if (quest.getSequence() > removeSequence) {
quest.decreaseSequence();
}
}
}
public CommonSuccessDto updateQuest(Long memberId, Long questId, UpdateQuestRequestDto dto) {
Quest quest = questQueryService.findQuest(questId);
quest.validateMember(memberId);

public CommonSuccessDto deleteOneQuest(Long questId) {
updateVector(memberId, dto, quest);
quest.update(dto.title(), dto.isComplete(), dto.deadline());

questRepository.delete(questQueryService.findQuest(questId));
return new CommonSuccessDto(true);
}

return CommonSuccessDto.fromEntity(true);
private void saveVector(Long memberId, CreateQuestRequestDto dto) {
CreateVectorDto vectorDto =
CreateVectorDto.builder().memberId(memberId).quest(dto.title()).build();
vectorService.save(vectorDto);
}

private void updateVector(Long memberId, UpdateQuestRequestDto dto, Quest quest) {
UpdateVectorQuestDto updateDto =
UpdateVectorQuestDto.builder()
.memberId(memberId)
.quest(quest.getTitle())
.newQuest(dto.title())
.build();
vectorService.updateQuest(updateDto);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.groom.orbit.goal.controller.command;

import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.groom.orbit.common.annotation.AuthMember;
import com.groom.orbit.common.dto.CommonSuccessDto;
import com.groom.orbit.common.dto.ResponseDto;
import com.groom.orbit.goal.app.command.QuestCommandService;
import com.groom.orbit.goal.app.command.QuestUpdateService;
import com.groom.orbit.goal.app.dto.request.CreateQuestRequestDto;
import com.groom.orbit.goal.app.dto.request.UpdateQuestRequestDto;
import com.groom.orbit.goal.app.dto.request.UpdateQuestSequenceRequestDto;
import com.groom.orbit.goal.app.dto.response.CreateQuestResponse;

import lombok.RequiredArgsConstructor;
Expand All @@ -22,7 +24,6 @@
public class QuestCommandController {

private final QuestCommandService questCommandService;
private final QuestUpdateService questUpdateService;

@PostMapping
public ResponseDto<CreateQuestResponse> createQuest(
Expand All @@ -35,18 +36,12 @@ public ResponseDto<CommonSuccessDto> updateQuest(
@AuthMember Long memberId,
@PathVariable("quest_id") Long questId,
@RequestBody UpdateQuestRequestDto dto) {
return ResponseDto.ok(questUpdateService.updateQuest(memberId, questId, dto));
}

@PatchMapping
public ResponseDto<CommonSuccessDto> updateQuestsSequence(
@AuthMember Long memberId, @RequestBody List<UpdateQuestSequenceRequestDto> dtos) {
return ResponseDto.ok(questUpdateService.updateQuestSequence(memberId, dtos));
return ResponseDto.ok(questCommandService.updateQuest(memberId, questId, dto));
}

@DeleteMapping("/{quest_id}")
public ResponseDto<CommonSuccessDto> deleteQuest(
@AuthMember Long memberId, @PathVariable("quest_id") Long questId) {
return ResponseDto.ok(questCommandService.deleteOneQuest(questId));
return ResponseDto.ok(questCommandService.deleteOneQuest(memberId, questId));
}
}
17 changes: 1 addition & 16 deletions src/main/java/com/groom/orbit/goal/dao/entity/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.hibernate.annotations.DynamicUpdate;

import com.groom.orbit.common.dao.entity.BaseTimeEntity;
import com.groom.orbit.common.exception.CommonException;
import com.groom.orbit.common.exception.ErrorCode;
import com.groom.orbit.member.dao.jpa.entity.Member;

import lombok.Getter;
Expand Down Expand Up @@ -48,22 +46,13 @@ public class Quest extends BaseTimeEntity {

public static Quest create(
String title, MemberGoal memberGoal, LocalDate deadline, int newSequence) {
Quest quest = new Quest();
quest.title = title;
quest.memberGoal = memberGoal;
Quest quest = copyQuest(title, memberGoal);
quest.sequence = newSequence;
quest.deadline = deadline;

return quest;
}

public void decreaseSequence() {
this.sequence -= 1;
if (this.sequence <= 0) {
throw new CommonException(ErrorCode.INVALID_STATE);
}
}

public void validateMember(Long memberId) {
Member member = this.memberGoal.getMember();
member.validateId(memberId);
Expand Down Expand Up @@ -93,10 +82,6 @@ public void update(String title, Boolean isComplete, LocalDate deadline) {
}
}

public void updateSequence(int sequence) {
this.sequence = sequence;
}

public static Quest copyQuest(String title, MemberGoal memberGoal) {
Quest quest = new Quest();
quest.title = title;
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ spring:
properties:
format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
hibernate:
default_batch_fetch_size: 10
hibernate:
ddl-auto: validate
show-sql: true
data:
redis:
host: ${REDIS_HOST}
port: ${REDIS_PORT}
# open-in-view: false
ai:
openai:
Expand Down Expand Up @@ -56,9 +62,3 @@ cloud:
---
fcm:
fcm-url: ${FCM_URL}
---
spring:
data:
redis:
host: ${REDIS_HOST}
port: ${REDIS_PORT}
Loading