Skip to content

Commit

Permalink
Merge pull request #1 AI 연동 성공
Browse files Browse the repository at this point in the history
AI 연동 성공
  • Loading branch information
Shortood authored Sep 6, 2024
2 parents 4ff98cc + b132a43 commit 9b23ce2
Show file tree
Hide file tree
Showing 27 changed files with 742 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
docker stop gratitude-container || true
docker rm -f gratitude-container || true
docker pull ${{ secrets.IMAGE_NAME }}:latest
docker run -d --name gratitude-container -p 8080:8080 -e DB_URL="${{ secrets.DB_URL }}" -e DB_USERNAME="${{ secrets.DB_USERNAME }}" -e DB_PASSWORD="${{ secrets.DB_PASSWORD }}" -e JWT_SECRET="${{ secrets.JWT_SECRET }}" ${{ secrets.IMAGE_NAME }}:latest
docker run -d --name gratitude-container -p 8080:8080 -e DB_URL="${{ secrets.DB_URL }}" -e DB_USERNAME="${{ secrets.DB_USERNAME }}" -e DB_PASSWORD="${{ secrets.DB_PASSWORD }}" -e JWT_SECRET="${{ secrets.JWT_SECRET }}" -e AI_SERVER="${{ secret.AI_SERVER }}" ${{ secrets.IMAGE_NAME }}:latest
docker image prune -f
# 8. 성공 시 디스코드 알림
Expand Down
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
build/

# User-specific stuff
.idea/**
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

application.yml
application-local.yml

# AWS User-specific
.idea/**/aws.xml

Expand Down Expand Up @@ -197,8 +201,12 @@ $RECYCLE.BIN/

### Gradle ###
.gradle
**/build/
/build/
/out/
/target/
/bin/
!src/**/build/
*.class

# Ignore Gradle GUI config
gradle-app.setting
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.1'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@ConfigurationPropertiesScan
@SpringBootApplication
Expand All @@ -12,4 +14,8 @@ public static void main(String[] args) {
SpringApplication.run(KtbGratitudeDiaryApplication.class, args);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,84 @@
package ktb.hackathon.ktbgratitudediary.controller;

import ktb.hackathon.ktbgratitudediary.domain.DiaryDto;
import ktb.hackathon.ktbgratitudediary.domain.DiaryWithEmotionDto;
import ktb.hackathon.ktbgratitudediary.domain.UserDto;
import ktb.hackathon.ktbgratitudediary.domain.request.DiaryRequest;
import ktb.hackathon.ktbgratitudediary.domain.response.AiResponse;
import ktb.hackathon.ktbgratitudediary.domain.security.CustomUserDetails;
import ktb.hackathon.ktbgratitudediary.response.SuccessResponse;
import ktb.hackathon.ktbgratitudediary.service.AiService;
import ktb.hackathon.ktbgratitudediary.service.DiaryService;
import ktb.hackathon.ktbgratitudediary.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/api/v1/diaries")
@RequiredArgsConstructor
@Tag(name = "Diary Management", description = "APIs for managing diaries")
public class DiaryController {

private final UserService userService;
private final DiaryService diaryService;
private final AiService aiService;

@Operation(summary = "일기 전체 조회", description = "Retrieve a paginated list of diaries for the authenticated user.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved list of diaries."),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "500", description = "Internal Server Error")
})
@GetMapping
public ResponseEntity<Object> getDiaries(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
) {
Page<DiaryDto> diaries = diaryService.getDiaries(customUserDetails.userId(), pageable);
return SuccessResponse.ok(diaries);
}

@Operation(summary = "일기 단일 조회", description = "Retrieve details of a specific diary by its ID.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved the diary."),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "404", description = "Diary not found"),
@ApiResponse(responseCode = "500", description = "Internal Server Error")
})
@GetMapping("/{diaryId}")
public ResponseEntity<Object> getDiary(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@PathVariable("diaryId") Long diaryId
) {
DiaryWithEmotionDto diary = diaryService.getDiary(customUserDetails.userId(), diaryId);
return SuccessResponse.ok(diary);
}

@Operation(summary = "일기 작성", description = "Create a new diary entry for the authenticated user.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Successfully created diary."),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "400", description = "Invalid request data"),
@ApiResponse(responseCode = "500", description = "Internal Server Error")
})
@PostMapping
public ResponseEntity<Void> createDiary(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody DiaryRequest diaryRequest
) {
UserDto userDto = userService.getUser(customUserDetails.userId());
AiResponse aiResponse = aiService.analyzeDiary(diaryRequest.toAiRequest());
diaryService.saveDiary(aiResponse.toDto(diaryRequest, userDto), aiResponse.emotions());
return SuccessResponse.created();
}
}
94 changes: 94 additions & 0 deletions src/main/java/ktb/hackathon/ktbgratitudediary/domain/DiaryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ktb.hackathon.ktbgratitudediary.domain;

import ktb.hackathon.ktbgratitudediary.entity.Diary;
import ktb.hackathon.ktbgratitudediary.entity.Template;

import java.time.LocalDateTime;

public record DiaryDto(
Long id,
String title,
String content,
UserDto userDto,
int happiness,
int weather,
String vectorImage,
Template template,
String rType,
String jType,
String mType,
String dType,
String totalDesc,
String totalTitle,
LocalDateTime createdAt
) {
public static DiaryDto of(
String title,
String content,
UserDto userDto,
int happiness,
int weather,
String vectorImage,
Template template,
String rType,
String jType,
String mType,
String dType,
String totalDesc,
String totalTitle,
LocalDateTime createdAt
){
return new DiaryDto(
null,
title,
content,
userDto,
happiness,
weather,
vectorImage,
template,
rType,
jType,
mType,
dType,
totalDesc,
totalTitle,
createdAt
);
}

public Diary toEntity() {
return Diary.of(
id,
title,
content,
userDto.toEntity(),
happiness,
weather,
vectorImage,
template,
rType,
jType,
mType,
dType,
totalDesc,
totalTitle);
}

public static DiaryDto from(Diary diary) {
return DiaryDto.of(diary.getTitle(),
diary.getContent(),
UserDto.from(diary.getUser()),
diary.getHappiness(),
diary.getWeather(),
diary.getVectorImage(),
diary.getTemplate(),
diary.getRType(),
diary.getJType(),
diary.getMType(),
diary.getDType(),
diary.getTotalDesc(),
diary.getTotalTitle(),
diary.getCreatedAt());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ktb.hackathon.ktbgratitudediary.domain;

import ktb.hackathon.ktbgratitudediary.entity.Diary;
import ktb.hackathon.ktbgratitudediary.entity.Template;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.stream.Collectors;

/**
* DTO for {@link ktb.hackathon.ktbgratitudediary.entity.Diary}
*/
public record DiaryWithEmotionDto(
Long id,
String title,
String content,
UserDto userDto,
LinkedHashSet<EmotionEntityDto> emotionEntityDtos,
int happiness,
int weather,
String vectorImage,
Template template,
String rType,
String jType,
String mType,
String dType,
String totalDesc,
String totalTitle,
LocalDateTime createdAt
) {
public static DiaryWithEmotionDto from(Diary diary){
return new DiaryWithEmotionDto(diary.getId(),
diary.getTitle(),
diary.getContent(),
UserDto.from(diary.getUser()),
diary.getEmotionEntities().stream()
.map(EmotionEntityDto::from)
.collect(Collectors.toCollection(LinkedHashSet::new)),
diary.getHappiness(),
diary.getWeather(),
diary.getVectorImage(),
diary.getTemplate(),
diary.getRType(),
diary.getJType(),
diary.getMType(),
diary.getDType(),
diary.getTotalDesc(),
diary.getTotalTitle(),
diary.getCreatedAt());
}

public static DiaryWithEmotionDto of(
String title,
String content,
UserDto userDto,
LinkedHashSet<EmotionEntityDto> emotionEntityDtos,
int happiness,
int weather,
String vectorImage,
Template template,
String rType,
String jType,
String mType,
String dType,
String totalDesc,
String totalTitle
){
return new DiaryWithEmotionDto(null,
title,
content,
userDto,
emotionEntityDtos,
happiness,
weather,
vectorImage,
template,
rType,
jType,
mType,
dType,
totalDesc,
totalTitle,
null);
}

public DiaryDto toDto() {
return DiaryDto.of(
title,
content,
userDto,
happiness,
weather,
vectorImage,
template,
rType,
jType,
mType,
dType,
totalDesc,
totalTitle,
createdAt);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ktb.hackathon.ktbgratitudediary.domain;

import ktb.hackathon.ktbgratitudediary.entity.EmotionEntity;

public record EmotionEntityDto(
Long id,
String name,
int per,
String color,
DiaryDto diaryDto
) {
public static EmotionEntityDto from(EmotionEntity emotionEntity) {
return new EmotionEntityDto(emotionEntity.getId(),
emotionEntity.getName(),
emotionEntity.getPer(),
emotionEntity.getColor(),
DiaryDto.from(emotionEntity.getDiary())
);
}

public EmotionEntity toEntity() {
return EmotionEntity.of(name, per, color, diaryDto.toEntity());
}

public static EmotionEntityDto of(
String name,
int per,
String color,
DiaryDto diaryDto
) {
return new EmotionEntityDto(
null,
name,
per,
color,
diaryDto);
}
}
Loading

0 comments on commit 9b23ce2

Please sign in to comment.