Skip to content

Commit

Permalink
Merge pull request #10 from LearnMate-Dev/chore/#9
Browse files Browse the repository at this point in the history
[Chore/#9] 일기 작성 기능 수정
  • Loading branch information
daeun084 authored Nov 6, 2024
2 parents 347a121 + 46782f5 commit dbb782e
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/main/java/LearnMate/dev/common/ErrorStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ public enum ErrorStatus implements BaseErrorCode {
_JWT_BLACKLIST(HttpStatus.UNAUTHORIZED, "JWT400", "접근 불가능한 토큰입니다"),
_JWT_UNKNOWN_ERROR(HttpStatus.UNAUTHORIZED, "JWT400", "JWT 인증 중 알 수 없는 오류가 발생했습니다."),

// Diary
_DUPLICATE_DIARY_DATE(HttpStatus.BAD_REQUEST, "DIARY400", "일기는 하루에 한 개만 작성할 수 있습니다."),
_INVALID_PATCH_DIARY(HttpStatus.BAD_REQUEST, "DIARY400", "당일 작성한 일기만 수정할 수 있습니다."),
_INVALID_DIARY_CONTENT_LENGTH(HttpStatus.BAD_REQUEST, "DIARY400", "일기의 내용은 500자 이하여야 합니다."),
_USER_FORBIDDEN_DIARY(HttpStatus.FORBIDDEN, "DIARY403", "다이어리에 접근 권한이 없습니다."),
_DIARY_NOT_FOUND(HttpStatus.NOT_FOUND, "DIARY404", "해당 다이어리를 찾을 수 없습니다."),

// Emotion
_INVALID_EMOTION_SCORE(HttpStatus.BAD_REQUEST, "EMOTION400", "감정 점수는 -1과 1 사이여야 합니다."),
_INVALID_EMOTION_SPECTRUN(HttpStatus.BAD_REQUEST, "EMOTION400", "유효하지 않은 감정 지표입니다.")
;


Expand Down
16 changes: 14 additions & 2 deletions src/main/java/LearnMate/dev/controller/DiaryController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package LearnMate.dev.controller;

import LearnMate.dev.common.ApiResponse;
import LearnMate.dev.model.dto.request.DiaryAnalysisRequest;
import LearnMate.dev.model.dto.request.DiaryPatchRequest;
import LearnMate.dev.model.dto.request.DiaryPostRequest;
import LearnMate.dev.model.dto.response.DiaryAnalysisResponse;
import LearnMate.dev.model.dto.response.DiaryDetailResponse;
import LearnMate.dev.service.DiaryService;
import jakarta.validation.Valid;
Expand All @@ -16,8 +18,18 @@ public class DiaryController {

private final DiaryService diaryService;

@GetMapping("/analysis")
public ApiResponse<DiaryAnalysisResponse> getDiaryAnalysis(
// TODO: get user info from session
// @SessionAttribute(name = "user_id") Long userId,
@RequestBody @Valid DiaryAnalysisRequest request) {

return ApiResponse.onSuccessData("일기 분석 성공", diaryService.analyzeDiary(1L, request));

}

@PostMapping("")
public ApiResponse<Long> postDiary(
public ApiResponse<DiaryDetailResponse> postDiary(
// TODO: get user info from session
// @SessionAttribute(name = "user_id") Long userId,
@RequestBody @Valid DiaryPostRequest request) {
Expand All @@ -27,7 +39,7 @@ public ApiResponse<Long> postDiary(
}

@PatchMapping("")
public ApiResponse<Long> patchDiary(
public ApiResponse<DiaryDetailResponse> patchDiary(
// TODO: get user info from session
// @SessionAttribute(name = "user_id") Long userId,
@RequestBody @Valid DiaryPatchRequest request ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package LearnMate.dev.model.converter;

import LearnMate.dev.model.entity.ActionTip;

public class ActionTipConverter {
public static ActionTip toActionTip(String content) {
return ActionTip.builder()
.content(content)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package LearnMate.dev.model.converter;

import LearnMate.dev.model.dto.response.DiaryAnalysisResponse;
import LearnMate.dev.model.dto.response.DiaryDetailResponse;
import LearnMate.dev.model.entity.ActionTip;
import LearnMate.dev.model.entity.Diary;
Expand All @@ -19,12 +20,20 @@ public static Diary toDiary(String content, User user, Emotion emotion, ActionTi

public static DiaryDetailResponse toDiaryDetailResponse(Diary diary) {
return DiaryDetailResponse.builder()
.diaryId(diary.getId())
.date(diary.getCreatedAtFormatted())
.content(diary.getContent())
.emotion(diary.getEmotion().getEmotion().getValue())
.actionTip(diary.getActionTip().getContent())
.build();
}

public static DiaryAnalysisResponse toDiaryAnalysisResponse(Double score, String actionTip) {
return DiaryAnalysisResponse.builder()
.emotionScore(score)
.actionTip(actionTip)
.build();
}


}
13 changes: 13 additions & 0 deletions src/main/java/LearnMate/dev/model/converter/EmotionConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package LearnMate.dev.model.converter;

import LearnMate.dev.model.entity.Emotion;
import LearnMate.dev.model.enums.EmotionSpectrum;

public class EmotionConverter {
public static Emotion toEmotion(Double score, EmotionSpectrum emotion) {
return Emotion.builder()
.score(score)
.emotion(emotion)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package LearnMate.dev.model.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class DiaryAnalysisRequest {

@NotBlank(message = "일기 내용을 입력해주세요.")
private String content;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package LearnMate.dev.model.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -11,4 +12,13 @@ public class DiaryPostRequest {
@NotBlank(message = "일기 내용을 입력해주세요.")
private String content;

@NotNull(message = "감정 점수를 입력해주세요.")
private Double score;

@NotBlank(message = "감정 지표를 입력해주세요.")
private String emotion;

@NotBlank(message = "행동 요령 제안을 입력해주세요.")
private String actionTip;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package LearnMate.dev.model.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DiaryAnalysisResponse {

private Double emotionScore;
private String actionTip;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package LearnMate.dev.model.dto.response;

import LearnMate.dev.model.enums.EmotionSpectrum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -11,6 +10,7 @@
@NoArgsConstructor
public class DiaryDetailResponse {

private Long diaryId;
private String date;
private String content;
private String emotion; // 감정 지표
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/LearnMate/dev/model/entity/ActionTip.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import LearnMate.dev.common.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Getter
@Getter @Builder
@Table(name = "actiontips")
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ActionTip extends BaseTimeEntity {
@Id
Expand All @@ -22,4 +21,8 @@ public class ActionTip extends BaseTimeEntity {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "diary_id")
private Diary diary;

public void updateDiary(Diary diary) {
this.diary = diary;
}
}
4 changes: 2 additions & 2 deletions src/main/java/LearnMate/dev/model/entity/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class Diary extends BaseTimeEntity {
@JoinColumn(name = "user_id")
private User user;

@OneToOne(mappedBy = "diary", cascade = CascadeType.ALL)
@OneToOne(mappedBy = "diary", cascade = CascadeType.ALL, orphanRemoval = true)
private Emotion emotion;

@OneToOne(mappedBy = "diary", cascade = CascadeType.ALL)
@OneToOne(mappedBy = "diary", cascade = CascadeType.ALL, orphanRemoval = true)
private ActionTip actionTip;

public void updateContent(String content) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/LearnMate/dev/model/entity/Emotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import LearnMate.dev.model.enums.EmotionSpectrum;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Entity @Builder
@Getter
@Table(name = "emotions")
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Emotion {
@Id
Expand All @@ -30,4 +29,8 @@ public class Emotion {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "report_id")
private Report report;

public void updateDiary(Diary diary) {
this.diary = diary;
}
}
9 changes: 9 additions & 0 deletions src/main/java/LearnMate/dev/model/enums/EmotionSpectrum.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ public String getValue() {
return value;
}

public static EmotionSpectrum getName(String value){
for (EmotionSpectrum emotion : EmotionSpectrum.values()) {
if (emotion.getValue().equals(value)) {
return emotion;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package LearnMate.dev.repository;

import LearnMate.dev.model.entity.ActionTip;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ActionTipRepository extends JpaRepository<ActionTip, Long> {
}
7 changes: 7 additions & 0 deletions src/main/java/LearnMate/dev/repository/EmotionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package LearnMate.dev.repository;

import LearnMate.dev.model.entity.Emotion;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmotionRepository extends JpaRepository<Emotion, Long> {
}
Loading

0 comments on commit dbb782e

Please sign in to comment.