Skip to content

Commit

Permalink
Merge pull request #30 from studio-recoding/feat-main-api
Browse files Browse the repository at this point in the history
[🚀feat] [⚠️!HOTFIX] 한줄 추천 기능 생성 및 JWT 에러 처리시 모든 에러 catch하는 것 수정
  • Loading branch information
JeonHaeseung authored Apr 7, 2024
2 parents 9d9a843 + 09ce6d1 commit e468e18
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
chain.doFilter(request, response);

} catch (TokenExpiredException e){
log.error("EXPIRED_TOKEN");
request.setAttribute("exception", ErrorCode.EXPIRED_TOKEN.getCode());
log.error(e + " EXPIRED_TOKEN");
//request.setAttribute("exception", ErrorCode.EXPIRED_TOKEN.getCode());
setResponse(response, ErrorCode.EXPIRED_TOKEN);
} catch (SignatureVerificationException e){
log.error("INVALID_TOKEN_SIGNATURE");
request.setAttribute("exception", ErrorCode.INVALID_TOKEN_SIGNATURE.getCode());
log.error(e + " INVALID_TOKEN_SIGNATURE");
setResponse(response, ErrorCode.INVALID_TOKEN_SIGNATURE);
} catch (Exception e){
log.error("TOKEN_EXCEPTION");
request.setAttribute("exception", ErrorCode.TOKEN_ERROR.getCode());
setResponse(response, ErrorCode.TOKEN_ERROR);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/Ness/Backend/domain/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public String parseAiChat(String text){

public String postNewAiChat(Long id, String text){

PostFastApiUserChatDto Userdto = PostFastApiUserChatDto.builder()
PostFastApiUserChatDto userDto = PostFastApiUserChatDto.builder()
.message(text)
.build();

//Fast API에 전송하기
PostFastApiAiChatDto AiDto = fastApiChatApi.creatFastApiChat(Userdto);
PostFastApiAiChatDto AiDto = fastApiChatApi.creatFastApiChat(userDto);

return AiDto.getAnswer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.domain.report.dto.response.GetReportMemoryListDto;
import Ness.Backend.domain.report.dto.response.GetReportRecommendDto;
import Ness.Backend.domain.report.dto.response.GetReportTagListDto;
import Ness.Backend.global.auth.AuthUser;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -30,8 +31,8 @@ public GetReportTagListDto getTag() {

@GetMapping("/recommend/dev")
@Operation(summary = "개발 테스트용 한줄 추천 조회 API", description = "사용자의 ID로 한줄 추천을 조회하는 API 입니다.")
public GetReportTagListDto getRecommend() {
return reportService.getTag(1L);
public GetReportRecommendDto getRecommend() {
return reportService.getRecommend(1L);
}

@GetMapping("/memory")
Expand All @@ -48,8 +49,8 @@ public GetReportTagListDto getTag(@AuthUser Member member) {

@GetMapping("/recommend")
@Operation(summary = "한줄 추천 조회 API", description = "사용자의 ID로 한줄 추천을 조회하는 API 입니다.")
public GetReportTagListDto getRecommend(@AuthUser Member member) {
return reportService.getTag(member.getId());
public GetReportRecommendDto getRecommend(@AuthUser Member member) {
return reportService.getRecommend(member.getId());
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package Ness.Backend.domain.report;

import Ness.Backend.domain.chat.entity.Chat;
import Ness.Backend.domain.report.entity.ReportRecommend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.ZonedDateTime;
import java.util.List;

public interface ReportRecommendRepository extends JpaRepository<ReportRecommend, Long> {
ReportRecommend findReportRecommendByMember_IdAndCreatedDateBetween(Long id, ZonedDateTime startOfDay, ZonedDateTime now);
// 특정 맴버의 오늘 하루 생성된 데이터만 반환
@Query( value = "SELECT * FROM report_recommend " +
"WHERE member_id = :memberId " +
"AND DATE(created_date) = CURDATE() " +
"ORDER BY created_date ASC",
nativeQuery = true)
ReportRecommend findTodayReportRecommendByMember_Id(
@Param("memberId") Long memberId);

//ReportRecommend findReportRecommendByMember_IdAndCreatedDateBetween(Long id, ZonedDateTime startOfDay, ZonedDateTime now);
}
62 changes: 55 additions & 7 deletions src/main/java/Ness/Backend/domain/report/ReportService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package Ness.Backend.domain.report;

import Ness.Backend.domain.chat.dto.request.PostFastApiUserChatDto;
import Ness.Backend.domain.chat.dto.response.PostFastApiAiChatDto;
import Ness.Backend.domain.member.MemberRepository;
import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.domain.report.dto.request.PostFastApiUserRecommendDto;
import Ness.Backend.domain.report.dto.response.*;
import Ness.Backend.domain.report.entity.ReportMemory;
import Ness.Backend.domain.report.entity.ReportRecommend;
import Ness.Backend.domain.report.entity.ReportTag;
import Ness.Backend.global.fastApi.FastApiRecommendApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -21,6 +27,8 @@ public class ReportService {
private final ReportMemoryRepository reportMemoryRepository;
private final ReportTagRepository reportTagRepository;
private final ReportRecommendRepository reportRecommendRepository;
private final FastApiRecommendApi fastApiRecommendApi;
private final MemberRepository memberRepository;

public GetReportMemoryListDto getMemory(Long id){
// 2주치의 데이터 가져오기
Expand Down Expand Up @@ -65,15 +73,55 @@ public GetReportTagListDto getTag(Long id){
public GetReportRecommendDto getRecommend(Long id){
// 이번 달의 데이터 가져오기
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
ZonedDateTime startOfToday = now.toLocalDate().atStartOfDay(now.getZone());
ZonedDateTime endOfToday = now.toLocalDate().atTime(LocalTime.MAX).atZone(now.getZone());
ZonedDateTime today = now.toLocalDate().atStartOfDay(now.getZone());

ReportRecommend reportRecommend = reportRecommendRepository.findReportRecommendByMember_IdAndCreatedDateBetween(id, startOfToday, endOfToday);
ReportRecommend reportRecommend = reportRecommendRepository.findTodayReportRecommendByMember_Id(id);

return GetReportRecommendDto.builder()
.id(reportRecommend.getId())
.createdDate(reportRecommend.getCreatedDate().toString())
.recommendText(reportRecommend.getRecommendText())
if(reportRecommend == null){
//새로운 한 줄 추천 생성하기
String answer = postNewAiRecommend(id, today);
String parsedAnswer = parseAiRecommend(answer);

Member memberEntity = memberRepository.findMemberById(id);

ReportRecommend newRecommend = ReportRecommend.builder()
.createdDate(today)
.recommendText(answer)
.member(memberEntity)
.build();

//새롭게 생성된 한 줄 추천 저장하기
reportRecommendRepository.save(newRecommend);

return GetReportRecommendDto.builder()
.id(newRecommend.getId())
.createdDate(newRecommend.getCreatedDate().toString())
.recommendText(newRecommend.getRecommendText())
.build();
} else{
return GetReportRecommendDto.builder()
.id(reportRecommend.getId())
.createdDate(reportRecommend.getCreatedDate().toString())
.recommendText(reportRecommend.getRecommendText())
.build();
}
}

public String parseAiRecommend(String text){
return text.replace("\"", "");
}

public String postNewAiRecommend(Long id, ZonedDateTime today){
PostFastApiUserRecommendDto userDto = PostFastApiUserRecommendDto.builder()
.member_id(id.intValue())
.user_persona("")
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();

//Fast API에 전송하기
PostFastApiAiRecommendDto AiDto = fastApiRecommendApi.creatFastApiRecommend(userDto);

return AiDto.getAnswer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Ness.Backend.domain.report.dto.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.ZonedDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PostFastApiUserRecommendDto {
@JsonProperty("member_id")
private int member_id;

@JsonProperty("user_persona")
private String user_persona;

@JsonProperty("schedule_datetime_start")
@JsonFormat(shape = JsonFormat.Shape.STRING)
private ZonedDateTime schedule_datetime_start;

@JsonProperty("schedule_datetime_end")
@JsonFormat(shape = JsonFormat.Shape.STRING)
private ZonedDateTime schedule_datetime_end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Ness.Backend.domain.report.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PostFastApiAiRecommendDto {
@JsonProperty("ness")
private String answer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import Ness.Backend.domain.member.entity.Member;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.ZonedDateTime;

@Entity
@Data
@NoArgsConstructor
public class ReportRecommend {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -20,4 +23,11 @@ public class ReportRecommend {
@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@Builder
public ReportRecommend(ZonedDateTime createdDate, String recommendText, Member member) {
this.createdDate = createdDate;
this.recommendText = recommendText;
this.member = member;
}
}
14 changes: 14 additions & 0 deletions src/main/java/Ness/Backend/global/fastApi/FastApiRecommendApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Ness.Backend.global.fastApi;


import Ness.Backend.domain.report.dto.request.PostFastApiUserRecommendDto;
import Ness.Backend.domain.report.dto.response.PostFastApiAiRecommendDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
@FeignClient(
name = "FastApiRecommend",
url = "${spring.cloud.openfeign.client.config.fastapi.url}")
public interface FastApiRecommendApi {
@PostMapping(value = "/recommendation/main")
PostFastApiAiRecommendDto creatFastApiRecommend(PostFastApiUserRecommendDto postFastApiUserRecommendDto);
}

0 comments on commit e468e18

Please sign in to comment.