Skip to content

Commit

Permalink
Merge pull request #69 from HyeJiJUN11/feature/59-diary
Browse files Browse the repository at this point in the history
feat: 다이어리 기능/오류 수정 및 친한친구 기능 삭제
  • Loading branch information
peeerr authored Oct 26, 2024
2 parents aae5e7e + 47d5281 commit 04a73be
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.potatocake.everymoment.dto.request.DiaryAutoCreateRequest;
import com.potatocake.everymoment.dto.request.DiaryFilterRequest;
import com.potatocake.everymoment.dto.request.DiaryManualCreateRequest;
import com.potatocake.everymoment.dto.request.DiaryPatchRequest;
import com.potatocake.everymoment.dto.response.CommentsResponse;
import com.potatocake.everymoment.dto.response.FriendDiariesResponse;
import com.potatocake.everymoment.dto.response.FriendDiaryResponse;
Expand Down Expand Up @@ -164,10 +165,10 @@ public ResponseEntity<SuccessResponse<Void>> updateDiary(
@Parameter(description = "수정할 일기 ID", required = true)
@PathVariable Long diaryId,
@Parameter(description = "일기 수정 정보", required = true)
@RequestBody DiaryManualCreateRequest diaryManualCreateRequest) {
@RequestBody DiaryPatchRequest diaryPatchRequest) {
Long memberId = memberDetails.getId();

diaryService.updateDiary(memberId, diaryId, diaryManualCreateRequest);
diaryService.updateDiary(memberId, diaryId, diaryPatchRequest);

return ResponseEntity.ok()
.body(SuccessResponse.ok());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,4 @@ public ResponseEntity<SuccessResponse<Void>> deleteFriend(
.body(SuccessResponse.ok());
}

@Operation(summary = "친한 친구 설정", description = "특정 친구를 친한 친구로 설정하거나 해제합니다.")
@ApiResponse(responseCode = "200", description = "친한 친구 설정 성공")
@PatchMapping("/{friendId}/bookmark")
public ResponseEntity<SuccessResponse<Void>> toggleCloseFriend(
@Parameter(description = "인증된 사용자 정보", hidden = true)
@AuthenticationPrincipal MemberDetails memberDetails,
@Parameter(description = "설정할 친구 ID", required = true)
@PathVariable Long friendId
) {
Long memberId = memberDetails.getId();

friendService.toggleCloseFriend(memberId, friendId);

return ResponseEntity.ok()
.body(SuccessResponse.ok());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.potatocake.everymoment.dto.request;

import com.potatocake.everymoment.dto.LocationPoint;
import jakarta.validation.constraints.Size;
import java.util.List;
import lombok.Getter;

@Getter
public class DiaryPatchRequest {

private Boolean deleteAllCategories;
private Boolean locationNameDelete;
private Boolean addressDelete;
private Boolean emojiDelete;
private Boolean contentDelete;

private List<CategoryRequest> categories;

@Size(max = 50, message = "장소명은 50자를 초과할 수 없습니다")
private String locationName;

@Size(max = 200, message = "주소는 200자를 초과할 수 없습니다")
private String address;

@Size(max = 10, message = "이모지는 10자를 초과할 수 없습니다")
private String emoji;

@Size(max = 5000, message = "일기 내용은 5000자를 초과할 수 없습니다")
private String content;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class FriendProfileResponse {
private Long id;
private String nickname;
private String profileImageUrl;
private boolean isClose;
}
4 changes: 3 additions & 1 deletion src/main/java/com/potatocake/everymoment/entity/Diary.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.potatocake.everymoment.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -56,7 +57,8 @@ public class Diary extends BaseTimeEntity {
@Builder.Default
private boolean isPublic = false;

@OneToMany(mappedBy = "diary")
@OneToMany(mappedBy = "diary", cascade = CascadeType.REMOVE, orphanRemoval = true)
@Builder.Default
private Set<DiaryCategory> diaryCategories = new HashSet<>();

public void updateContent(String content) {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/potatocake/everymoment/entity/Friend.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,4 @@ public class Friend {
@JoinColumn(nullable = false)
private Member friend;

@Column(nullable = false)
@Builder.Default
private boolean isClose = false;

public void toggleIsClose() {
this.isClose = !this.isClose;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Member extends BaseTimeEntity {
private String profileImageUrl;

@Column(nullable = false)
@Builder.Default
private boolean deleted = false;

public void update(String nickname, String profileImageUrl) {
Expand Down
71 changes: 41 additions & 30 deletions src/main/java/com/potatocake/everymoment/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.potatocake.everymoment.dto.request.DiaryAutoCreateRequest;
import com.potatocake.everymoment.dto.request.DiaryFilterRequest;
import com.potatocake.everymoment.dto.request.DiaryManualCreateRequest;
import com.potatocake.everymoment.dto.request.DiaryPatchRequest;
import com.potatocake.everymoment.dto.response.CategoryResponse;
import com.potatocake.everymoment.dto.response.MyDiariesResponse;
import com.potatocake.everymoment.dto.response.MyDiaryResponse;
Expand Down Expand Up @@ -177,47 +178,57 @@ public LocationPoint getDiaryLocation(Long memberId, Long diaryId) {
}

// 내 일기 수정
public void updateDiary(Long memberId, Long diaryId, DiaryManualCreateRequest diaryManualCreateRequest) {
public void updateDiary(Long memberId, Long diaryId, DiaryPatchRequest diaryPatchRequest) {
Diary existingDiary = getExistDiary(memberId, diaryId);

// 카테고리 업데이트
List<CategoryRequest> categoryRequestList = diaryManualCreateRequest.getCategories();
if (categoryRequestList != null && !categoryRequestList.isEmpty()) {
if (diaryPatchRequest.getDeleteAllCategories() != null && diaryPatchRequest.getDeleteAllCategories()) {
diaryCategoryRepository.deleteByDiary(existingDiary);

for (CategoryRequest categoryRequest : categoryRequestList) {
Long categoryId = categoryRequest.getCategoryId();

DiaryCategory diaryCategory = DiaryCategory.builder()
.diary(existingDiary)
.category(categoryRepository.findById(categoryId)
.map(category -> {
category.checkOwner(memberId);
return category;
})
.orElseThrow(() -> new GlobalException(ErrorCode.CATEGORY_NOT_FOUND)))
.build();

diaryCategoryRepository.save(diaryCategory);
} else {
List<CategoryRequest> categoryRequestList = diaryPatchRequest.getCategories();
if (categoryRequestList != null && !categoryRequestList.isEmpty()) {
for (CategoryRequest categoryRequest : categoryRequestList) {
Long categoryId = categoryRequest.getCategoryId();

DiaryCategory diaryCategory = DiaryCategory.builder()
.diary(existingDiary)
.category(categoryRepository.findById(categoryId)
.map(category -> {
category.checkOwner(memberId);
return category;
})
.orElseThrow(() -> new GlobalException(ErrorCode.CATEGORY_NOT_FOUND)))
.build();

diaryCategoryRepository.save(diaryCategory);
}
}
}

if (diaryManualCreateRequest.getLocationPoint() != null) {
double longitude = diaryManualCreateRequest.getLocationPoint().getLongitude();
double latitude = diaryManualCreateRequest.getLocationPoint().getLatitude();
// 다이어리 업데이트
if (diaryPatchRequest.getContentDelete() != null && diaryPatchRequest.getContentDelete()) {
existingDiary.updateContent(null);
} else {
existingDiary.updateContent(diaryPatchRequest.getContent());
}

Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
if (diaryPatchRequest.getLocationNameDelete() != null && diaryPatchRequest.getLocationNameDelete()) {
existingDiary.updateLocationName(null);
} else {
existingDiary.updateLocationName(diaryPatchRequest.getLocationName());
}

existingDiary.updateLocationPoint(point);
if (diaryPatchRequest.getAddressDelete() != null && diaryPatchRequest.getAddressDelete()) {
existingDiary.updateAddress(null);
} else {
existingDiary.updateAddress(diaryPatchRequest.getAddress());
}

//다이어리 업데이트
existingDiary.updateContent(diaryManualCreateRequest.getContent());
existingDiary.updateLocationName(diaryManualCreateRequest.getLocationName());
existingDiary.updateAddress(diaryManualCreateRequest.getAddress());
existingDiary.updateEmoji(diaryManualCreateRequest.getEmoji());
existingDiary.updateBookmark(diaryManualCreateRequest.isBookmark());
existingDiary.updatePublic(diaryManualCreateRequest.isPublic());
if (diaryPatchRequest.getEmojiDelete() != null && diaryPatchRequest.getEmojiDelete()) {
existingDiary.updateEmoji(null);
} else {
existingDiary.updateEmoji(diaryPatchRequest.getEmoji());
}
}

// 내 일기 삭제
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static Specification<Diary> filterDiaries(

if (from != null && until != null) {
predicate = builder.and(predicate,
builder.between(root.get("createAt"), from.atStartOfDay(), until.plusDays(1).atStartOfDay()));
builder.between(root.get("createAt"), from, until));
}
if (isBookmark != null) {
predicate = builder.and(predicate, builder.equal(root.get("isBookmark"), isBookmark));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,6 @@ public void deleteFriend(Long memberId, Long firendId) {
friendRepository.delete(friendFriends);
}

// 친한 친구 설정(토글)
public void toggleCloseFriend(Long memberId, Long friendId) {
Member currentMember = memberRepository.findById(memberId)
.orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_FOUND));
Member friendMember = memberRepository.findById(friendId)
.orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_FOUND));

Friend friend = friendRepository.findByMemberAndFriend(currentMember, friendMember)
.orElseThrow(() -> new GlobalException(ErrorCode.FRIEND_NOT_FOUND));

friend.toggleIsClose();
}

//다이어리 DTO 변환
private FriendDiarySimpleResponse convertToFriendDiariesResponseDTO(Diary savedDiary) {
//파일 찾음
Expand Down Expand Up @@ -156,7 +143,6 @@ private FriendProfileResponse convertToFriendProfileResponseDTO(Friend friend) {
.id(friendMember.getId())
.nickname(friendMember.getNickname())
.profileImageUrl(friendMember.getProfileImageUrl())
.isClose(friend.isClose())
.build();
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ spring:
url: ${RDS_URL}
username: ${RDS_USERNAME}
password: ${RDS_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
jdbc:
time_zone: Asia/Seoul
hibernate:
ddl-auto: update

h2:
console:
enabled: true
enabled: false

servlet:
multipart:
Expand Down

0 comments on commit 04a73be

Please sign in to comment.