-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AI 연동 성공
- Loading branch information
Showing
27 changed files
with
742 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 74 additions & 1 deletion
75
src/main/java/ktb/hackathon/ktbgratitudediary/controller/DiaryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
94
src/main/java/ktb/hackathon/ktbgratitudediary/domain/DiaryDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/ktb/hackathon/ktbgratitudediary/domain/DiaryWithEmotionDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/ktb/hackathon/ktbgratitudediary/domain/EmotionEntityDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.