Skip to content

Commit

Permalink
Merge pull request #163 from Team-Smeme/sohyeon_#158
Browse files Browse the repository at this point in the history
[REFACT] 리팩토링
  • Loading branch information
thguss authored Sep 20, 2023
2 parents 2d702cc + 490fc66 commit b9d9d56
Show file tree
Hide file tree
Showing 26 changed files with 313 additions and 316 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/smeme/server/config/ValueConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class ValueConfig {
@Value("${fcm.google_api}")
private String GOOGLE_API_URI;

@Value("${smeem.duration.expired}")
private String DURATION_EXPIRED;

@Value("${smeem.duration.remind}")
private String DURATION_REMIND;

@PostConstruct
protected void init() {
JWT_SECRET = Base64.getEncoder().encodeToString(JWT_SECRET.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public class CorrectionController {
description = "일기 첨삭 성공")
})
@PostMapping("/diary/{diaryId}")
public ResponseEntity<ApiResponse> createCorrection(
public ResponseEntity<ApiResponse> save(
@Parameter(hidden = true) Principal principal,
@Parameter(description = "일기 id") @PathVariable Long diaryId,
@RequestBody CorrectionRequestDTO request
) {
CorrectionResponseDTO response = correctionService.createCorrection(getMemberId(principal), diaryId, request);
CorrectionResponseDTO response = correctionService.save(getMemberId(principal), diaryId, request);
return ResponseEntity
.created(getURI(diaryId))
.body(success(SUCCESS_CREATE_CORRECTION.getMessage(), response));
Expand All @@ -62,8 +62,8 @@ public ResponseEntity<ApiResponse> createCorrection(
description = "첨삭 삭제 성공")
})
@DeleteMapping("/{correctionId}")
public ResponseEntity<ApiResponse> deleteCorrection(@Parameter(description = "첨삭 id") @PathVariable Long correctionId) {
correctionService.deleteCorrection(correctionId);
public ResponseEntity<ApiResponse> delete(@Parameter(description = "첨삭 id") @PathVariable Long correctionId) {
correctionService.delete(correctionId);
return ResponseEntity.ok(success(SUCCESS_DELETE_CORRECTION.getMessage()));
}

Expand All @@ -74,8 +74,8 @@ public ResponseEntity<ApiResponse> deleteCorrection(@Parameter(description = "
description = "첨삭 수정 성공")
})
@PatchMapping("/{correctionId}")
public ResponseEntity<ApiResponse> updateCorrection(@Parameter(description = "첨삭 id") @PathVariable Long correctionId, @RequestBody CorrectionRequestDTO request) {
correctionService.updateCorrection(correctionId, request);
public ResponseEntity<ApiResponse> update(@Parameter(description = "첨삭 id") @PathVariable Long correctionId, @RequestBody CorrectionRequestDTO request) {
correctionService.update(correctionId, request);
return ResponseEntity.ok(success(SUCCESS_UPDATE_CORRECTION.getMessage()));
}
}
24 changes: 13 additions & 11 deletions src/main/java/com/smeme/server/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class DiaryController {
content = @Content(schema = @Schema(implementation = CreatedDiaryResponseDTO.class)))
})
@PostMapping
public ResponseEntity<ApiResponse> createDiary(@Parameter(hidden = true) Principal principal, @RequestBody DiaryRequestDTO request) {
CreatedDiaryResponseDTO response = diaryService.createDiary(getMemberId(principal), request);
public ResponseEntity<ApiResponse> save(@Parameter(hidden = true) Principal principal, @RequestBody DiaryRequestDTO request) {
CreatedDiaryResponseDTO response = diaryService.save(getMemberId(principal), request);
return ResponseEntity
.created(getURI(response.diaryId()))
.body(success(SUCCESS_CREATE_DIARY.getMessage(), response));
Expand All @@ -66,8 +66,8 @@ public ResponseEntity<ApiResponse> createDiary(@Parameter(hidden = true) Princip
content = @Content(schema = @Schema(implementation = DiaryResponseDTO.class)))
})
@GetMapping("/{diaryId}")
public ResponseEntity<ApiResponse> getDiaryDetail(@Parameter(name = "일기 id") @PathVariable Long diaryId) {
DiaryResponseDTO response = diaryService.getDiaryDetail(diaryId);
public ResponseEntity<ApiResponse> getDetail(@Parameter(name = "일기 id") @PathVariable Long diaryId) {
DiaryResponseDTO response = diaryService.getDetail(diaryId);
return ResponseEntity.ok(success(SUCCESS_GET_DIARY.getMessage(), response));
}

Expand All @@ -78,9 +78,11 @@ public ResponseEntity<ApiResponse> getDiaryDetail(@Parameter(name = "일기 id")
description = "일기 수정 성공")
})
@PatchMapping("/{diaryId}")
public ResponseEntity<ApiResponse> updateDiary(
@Parameter(name = "일기 id") @PathVariable Long diaryId, @RequestBody DiaryRequestDTO request) {
diaryService.updateDiary(diaryId, request);
public ResponseEntity<ApiResponse> update(
@Parameter(name = "일기 id") @PathVariable Long diaryId,
@RequestBody DiaryRequestDTO request
) {
diaryService.update(diaryId, request);
return ResponseEntity.ok(success(SUCCESS_UPDATE_DAIRY.getMessage()));
}

Expand All @@ -91,8 +93,8 @@ public ResponseEntity<ApiResponse> updateDiary(
description = "일기 삭제 성공")
})
@DeleteMapping("/{diaryId}")
public ResponseEntity<ApiResponse> deleteDiary(@Parameter(name = "일기 id") @PathVariable Long diaryId) {
diaryService.deleteDiary(diaryId);
public ResponseEntity<ApiResponse> delete(@Parameter(name = "일기 id") @PathVariable Long diaryId) {
diaryService.delete(diaryId);
return ResponseEntity.ok(success(SUCCESS_DELETE_DIARY.getMessage()));
}

Expand All @@ -106,8 +108,8 @@ public ResponseEntity<ApiResponse> deleteDiary(@Parameter(name = "일기 id") @P
@GetMapping
public ResponseEntity<ApiResponse> getDiaries(
@Parameter(hidden = true) Principal principal,
@Parameter(name = "범위 시작 날짜(yyyy-MM-dd HH:mm)") @RequestParam(name = "start") String startDate,
@Parameter(name = "범위 끝 날짜(yyyy-MM-dd HH:mm)") @RequestParam(name = "end") String endDate
@Parameter(name = "범위 시작 날짜(yyyy-MM-dd)") @RequestParam(name = "start") String startDate,
@Parameter(name = "범위 끝 날짜(yyyy-MM-dd)") @RequestParam(name = "end") String endDate
) {
DiariesResponseDTO response = diaryService.getDiaries(getMemberId(principal), startDate, endDate);
return ResponseEntity.ok(success(SUCCESS_GET_DIARIES.getMessage(), response));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/smeme/server/controller/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public ResponseEntity<ApiResponse> invalidBearerTokenException(InvalidBearerToke
public ResponseEntity<ApiResponse> ioException(IOException ex) {
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(fail(ex.getMessage()));
}

@ExceptionHandler(IllegalStateException.class)
public ResponseEntity<ApiResponse> illegalStateException(IllegalStateException ex) {
return ResponseEntity.status(BAD_REQUEST).body(fail(ex.getMessage()));
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/smeme/server/controller/GoalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class GoalController {
content = @Content(schema = @Schema(implementation = GoalsResponseDTO.class)))
})
@GetMapping
public ResponseEntity<ApiResponse> getAllGoals() {
GoalsResponseDTO response = goalService.getAllGoals();
public ResponseEntity<ApiResponse> getAll() {
GoalsResponseDTO response = goalService.getAll();
return ResponseEntity.ok(success(SUCCESS_GET_GOALS.getMessage(), response));
}

Expand All @@ -52,8 +52,8 @@ public ResponseEntity<ApiResponse> getAllGoals() {
content = @Content(schema = @Schema(implementation = GoalResponseDTO.class)))
})
@GetMapping("/{type}")
public ResponseEntity<ApiResponse> getGoalByType(@Parameter(description = "학습 목표 ENUM 값") @PathVariable GoalType type) {
GoalResponseDTO response = goalService.getGoalByType(type);
public ResponseEntity<ApiResponse> getByType(@Parameter(description = "학습 목표 ENUM 값") @PathVariable GoalType type) {
GoalResponseDTO response = goalService.getByType(type);
return ResponseEntity.ok(success(SUCCESS_GET_GOAL.getMessage(), response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public void pushMessage() throws InterruptedException {
}

@Scheduled(cron = "0 0 0 * * *")
public void deleteDiaries30Past() throws InterruptedException {
Thread.sleep(1000);
diaryService.deleteDiary30Past(LocalDateTime.now().minusDays(30));
public void deleteExpiredDiaries() {
diaryService.deleteByExpiredDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class TopicController {
content = @Content(schema = @Schema(implementation = TopicResponseDTO.class)))
})
@GetMapping("/random")
public ResponseEntity<ApiResponse> getRandomTopic() {
TopicResponseDTO response = topicService.getRandomTopic();
public ResponseEntity<ApiResponse> getRandom() {
TopicResponseDTO response = topicService.getRandom();
return ResponseEntity.ok(success(SUCCESS_GET_RANDOM_TOPIC.getMessage(), response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,24 @@

import com.smeme.server.model.badge.Badge;

public record CorrectionResponseDTO(Long diaryId, Badge badge) {
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public record CorrectionResponseDTO(
Long diaryId,
List<BadgeDTO> badges
) {
public static CorrectionResponseDTO of(Long diaryId, Badge badge) {
return new CorrectionResponseDTO(diaryId, new ArrayList<>(Collections.singleton(BadgeDTO.of(badge))));
}

record BadgeDTO(
String name,
String imageUrl
) {
public static BadgeDTO of(Badge badge) {
return new BadgeDTO(badge.getName(), badge.getImageUrl());
}
}
}
13 changes: 4 additions & 9 deletions src/main/java/com/smeme/server/dto/diary/DiariesResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@

import io.swagger.v3.oas.annotations.media.Schema;

import static com.smeme.server.util.Util.transferDateTimeToString;
import static com.smeme.server.util.Util.dateToString;

public record DiariesResponseDTO(
@Schema(description = "일기 정보 리스트")
List<DiaryDTO> diaries,
@Schema(description = "30일 전 일기 존재 여부", example = "true")
boolean has30Past
) {
public static DiariesResponseDTO of(List<Diary> diaries, boolean has30Past) {
return new DiariesResponseDTO(
diaries.stream().map(DiaryDTO::of).toList(),
has30Past
);
public static DiariesResponseDTO of(List<Diary> diaries, boolean hasRemind) {
return new DiariesResponseDTO(diaries.stream().map(DiaryDTO::of).toList(), hasRemind);
}

record DiaryDTO(
Expand All @@ -30,9 +27,7 @@ record DiaryDTO(
String createdAt
) {
public static DiaryDTO of(Diary diary) {
return new DiaryDTO(diary.getId(),
diary.getContent(),
transferDateTimeToString(diary.getCreatedAt()));
return new DiaryDTO(diary.getId(), diary.getContent(), dateToString(diary.getCreatedAt()));
}
}
}
Expand Down
24 changes: 5 additions & 19 deletions src/main/java/com/smeme/server/dto/diary/DiaryResponseDTO.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.smeme.server.dto.diary;

import static com.smeme.server.util.Util.dateToString;
import static java.util.Objects.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.smeme.server.model.Correction;
import com.smeme.server.model.Diary;
import com.smeme.server.model.topic.Topic;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
Expand All @@ -35,29 +33,17 @@ public record DiaryResponseDTO(
@Schema(description = "첨삭 리스트")
List<CorrectionDTO> corrections
) {
public static DiaryResponseDTO of(Diary diary, List<Correction> corrections) {
public static DiaryResponseDTO of(Diary diary) {
return DiaryResponseDTO.builder()
.diaryId(diary.getId())
.topic(getTopic(diary.getTopic()))
.topic(nonNull(diary.getTopic()) ? diary.getTopic().getContent() : "")
.content(diary.getContent())
.createdAt(getCreatedAt(diary.getCreatedAt()))
.createdAt(dateToString(diary.getCreatedAt()))
.username(diary.getMember().getUsername())
.corrections(getCorrections(corrections))
.corrections(diary.getCorrections().stream().map(CorrectionDTO::of).toList())
.build();
}

private static String getTopic(Topic topic) {
return nonNull(topic) ? topic.getContent() : "";
}

private static String getCreatedAt(LocalDateTime createdAt) {
return createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}

private static List<CorrectionDTO> getCorrections(List<Correction> corrections) {
return corrections.stream().map(CorrectionDTO::of).toList();
}

record CorrectionDTO(
@Schema(description = "첨삭 id", example = "1")
Long correctionId,
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/smeme/server/model/Correction.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ public Correction(String beforeSentence, String afterSentence, Diary diary) {
setDiary(diary);
}

public void deleteCorrection() {
if (nonNull(this.diary)) {
this.diary.getCorrections().remove(this);
}
}

public void updateCorrection(String content) {
public void updateContent(String content) {
this.afterSentence = content;
}

Expand Down
29 changes: 22 additions & 7 deletions src/main/java/com/smeme/server/model/Diary.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.smeme.server.model;

import static com.smeme.server.util.Util.getStartOfDay;
import static jakarta.persistence.FetchType.*;
import static jakarta.persistence.GenerationType.*;
import static java.util.Objects.nonNull;

import jakarta.persistence.*;
import lombok.Getter;
Expand All @@ -10,7 +12,6 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.smeme.server.model.topic.Topic;

Expand Down Expand Up @@ -58,23 +59,37 @@ public Diary(String content, Topic topic, Member member) {

public void updateContent(String content) {
this.content = content;
this.updatedAt = LocalDateTime.now();
}

public void deleteDiary() {
public void delete() {
this.isDeleted = true;
this.updatedAt = LocalDateTime.now();
this.member.getDiaries().remove(this);
deleteFromMember();
this.member.updateDiaryCombo();
}

public void deleteFromMember() {
if (Objects.nonNull(this.member)) {
if (nonNull(this.member)) {
this.member.getDiaries().remove(this);
}
}

public boolean isValid() {
return !this.isDeleted;
}

public boolean isCreatedAt(LocalDateTime createdAt) {
return getStartOfDay(this.createdAt).isEqual(getStartOfDay(createdAt));
}

public boolean isBetween(LocalDateTime startAt, LocalDateTime endAt) {
LocalDateTime createdAt = getStartOfDay(this.createdAt);
startAt = getStartOfDay(startAt);
endAt = getStartOfDay(endAt);
return createdAt.equals(startAt) || (createdAt.isAfter(startAt) && createdAt.isBefore(endAt)) || createdAt.equals(endAt);
}

private void setMember(Member member) {
if (Objects.nonNull(this.member)) {
if (nonNull(this.member)) {
this.member.getDiaries().remove(this);
}
this.member = member;
Expand Down
Loading

0 comments on commit b9d9d56

Please sign in to comment.