Skip to content

Commit

Permalink
Merge pull request #30 from KUSITMS-CORECORD/feat/#24
Browse files Browse the repository at this point in the history
[Feat/#24] MEMO ver. 경험 기록 임시 저장 기능 구현
  • Loading branch information
daeun084 authored Oct 29, 2024
2 parents 748ca69 + 5536e3f commit 3377c63
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
@AllArgsConstructor
public enum RecordSuccessStatus implements BaseSuccessStatus {

MEMO_RECORD_CREATE_SUCCESS(HttpStatus.CREATED, "S307", "메모 경험 기록이 성공적으로 완료되었습니다."),
MEMO_RECORD_DETAIL_GET_SUCCESS(HttpStatus.OK, "S401", "메모 경험 기록 세부 조회가 성공적으로 완료되었습니다.");
MEMO_RECORD_CREATE_SUCCESS(HttpStatus.CREATED, "S404", "메모 경험 기록이 성공적으로 완료되었습니다."),
MEMO_RECORD_DETAIL_GET_SUCCESS(HttpStatus.OK, "S401", "메모 경험 기록 세부 조회가 성공적으로 완료되었습니다."),
MEMO_RECORD_TMP_CREATE_SUCCESS(HttpStatus.OK, "S403", "메모 경험 기록 임시 저장이 성공적으로 완료되었습니다."),
MEMO_RECORD_TMP_GET_SUCCESS(HttpStatus.OK, "S402", "메모 경험 기록 임시 저장 내역 조회가 성공적으로 완료되었습니다.")
;

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,22 @@ public ResponseEntity<ApiResponse<RecordResponse.MemoRecordDto>> getMemoRecordDe
RecordResponse.MemoRecordDto recordResponse = recordService.getMemoRecordDetail(userId, recordId);
return ApiResponse.success(RecordSuccessStatus.MEMO_RECORD_DETAIL_GET_SUCCESS, recordResponse);
}

@PostMapping("/memo/tmp")
public ResponseEntity<ApiResponse<String>> saveTmpMemoRecord(
@UserId Long userId,
@RequestBody RecordRequest.TmpMemoRecordDto tmpMemoRecordDto
) {
recordService.createTmpMemoRecord(userId, tmpMemoRecordDto);
return ApiResponse.success(RecordSuccessStatus.MEMO_RECORD_TMP_CREATE_SUCCESS);
}

@GetMapping("/memo/tmp")
public ResponseEntity<ApiResponse<RecordResponse.TmpMemoRecordDto>> getTmpMemoRecord(
@UserId Long userId
) {
RecordResponse.TmpMemoRecordDto recordResponse = recordService.getTmpMemoRecord(userId);
return ApiResponse.success(RecordSuccessStatus.MEMO_RECORD_TMP_GET_SUCCESS, recordResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,20 @@ public static RecordResponse.MemoRecordDto toMemoRecordDto(Record record) {
.createdAt(record.getCreatedAtFormatted())
.build();
}

public static RecordResponse.TmpMemoRecordDto toExistingTmpMemoRecordDto(Record record) {
return RecordResponse.TmpMemoRecordDto.builder()
.isExist(true)
.title(record.getTitle())
.content(record.getContent())
.build();
}

public static RecordResponse.TmpMemoRecordDto toNotExistingTmpMemoRecordDto() {
return RecordResponse.TmpMemoRecordDto.builder()
.isExist(false)
.title(null)
.content(null)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ public static class MemoRecordDto {
@NotBlank(message = "저장할 폴더의 id를 입력해주세요.")
private Long folderId;
}

@Data
public static class TmpMemoRecordDto {
private String title;
@NotBlank(message = "임시 저장할 내용을 입력해주세요.")
private String content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ public static class MemoRecordDto {
private String createdAt;
}

@Builder
@Getter
@AllArgsConstructor
@Data
public static class TmpMemoRecordDto {
private Boolean isExist;
private String title;
private String content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum RecordErrorStatus implements BaseErrorStatus {
OVERFLOW_MEMO_RECORD_TITLE(HttpStatus.BAD_REQUEST, "E0400_OVERFLOW_TITLE", "메모 제목은 15자 이내여야 합니다."),
OVERFLOW_MEMO_RECORD_CONTENT(HttpStatus.BAD_REQUEST, "E0400_OVERFLOW_CONTENT", "메모 내용은 500자 이내여야 합니다."),
USER_RECORD_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "E401_RECORD_UNAUTHORIZED", "유저가 경험 기록에 대한 권한이 없습니다."),
RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "E0404_RECORD", "존재하지 않는 경험 기록입니다.")
RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "E0404_RECORD", "존재하지 않는 경험 기록입니다."),
ALREADY_TMP_MEMO(HttpStatus.BAD_REQUEST, "E0400_TMP_MEMO", "유저가 이미 임시 저장된 메모를 가지고 있습니다.")
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,64 @@ public RecordResponse.MemoRecordDto getMemoRecordDetail(Long userId, Long record
return RecordConverter.toMemoRecordDto(record);
}

/*
* title, content를 받아 Record 객체를 생성한 후, recordId를 User.tmpMemo에 저장
* @param userId, tmpMemoRecordDto
*/
@Transactional
public void createTmpMemoRecord(Long userId, RecordRequest.TmpMemoRecordDto tmpMemoRecordDto) {
User user = findUserById(userId);
String title = tmpMemoRecordDto.getTitle();
String content = tmpMemoRecordDto.getContent();

// User의 임시 메모 저장 유무 확인
validHasUserTmpMemo(user);

// 제목, 본문 글자 수 검사
validTextLength(title, content);

// Record entity 생성 후 user.tmpMemo 필드에 recordId 저장
Record record = RecordConverter.toMemoRecordEntity(title, content, user, null);
Record tmpRecord = recordRepository.save(record);
user.updateTmpMemo(tmpRecord.getRecordId());
}


/*
* user의 임시 저장된 메모 기록이 있다면 해당 Record row와 tmpMemo 필드 정보를 제거한 후 저장된 데이터를 반환
* @param userId
* @return
*/
@Transactional
public RecordResponse.TmpMemoRecordDto getTmpMemoRecord(Long userId) {
User user = findUserById(userId);
Long tmpMemoRecordId = user.getTmpMemo();

// 임시 저장 내역이 없는 경우 isExist=false 반환
if (tmpMemoRecordId == null) {
return RecordConverter.toNotExistingTmpMemoRecordDto();
}

// 임시 저장 내역이 있는 경우 결과 조회
Record tmpMemoRecord = findRecordById(tmpMemoRecordId);

// 기존 데이터 제거 후 결과 반환
user.deleteTmpMemo();
recordRepository.delete(tmpMemoRecord);
return RecordConverter.toExistingTmpMemoRecordDto(tmpMemoRecord);
}

private void validHasUserTmpMemo(User user) {
if (user.getTmpMemo() != null)
throw new RecordException(RecordErrorStatus.ALREADY_TMP_MEMO);
}

private void validTextLength(String title, String content) {
if (title.length() > 15) {
if (title != null && title.length() > 15)
throw new RecordException(RecordErrorStatus.OVERFLOW_MEMO_RECORD_TITLE);
}

if (content.length() > 500) {
if (content != null && content.length() > 500)
throw new RecordException(RecordErrorStatus.OVERFLOW_MEMO_RECORD_CONTENT);
}
}

// user-record 권한 검사
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/corecord/dev/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ public class User extends BaseEntity {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Folder> folders;

public void updateTmpMemo(Long tmpMemo) {
this.tmpMemo = tmpMemo;
}

public void deleteTmpMemo(){
this.tmpMemo = null;
}
}

0 comments on commit 3377c63

Please sign in to comment.