Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public abstract class BaseTimeEntity {
protected void setDeletedAt(LocalDateTime deletedAt) {
this.deletedAt = deletedAt;
}
protected void setUpdatedAt(LocalDateTime updatedAt) {this.updatedAt = updatedAt;}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/chalpu/guide/domain/Guide.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;


@NamedEntityGraph(
name = "Guide.withSubCategoryAndCategory",
Expand Down Expand Up @@ -84,6 +86,10 @@ public void update(String content, String fileName, SubCategory subCategory) {
}
}

public void updateDate() {
setUpdatedAt(LocalDateTime.now());
}

public void softDelete() {
this.isActive = false;
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/example/chalpu/tag/service/GuideTagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public class GuideTagService {
@Transactional
public TagResponse addTagToGuide(Long guideId, String tagName) {
// Guide 존재 여부만 확인 (엔티티 조회 없이)
if (!guideRepository.existsById(guideId)) {
throw new NoticeException(ErrorMessage.GUIDE_NOT_FOUND);
}
Guide guide = guideRepository.findById(guideId)
.orElseThrow(() -> new NoticeException(ErrorMessage.GUIDE_NOT_FOUND));

// Tag 조회 또는 생성
Tag tag = tagRepository.findByNameAndIsActiveTrue(tagName)
Expand All @@ -49,20 +48,21 @@ public TagResponse addTagToGuide(Long guideId, String tagName) {
guideTag.activate();
}
} else {
// Guide 엔티티가 필요한 경우에만 조회
Guide guide = guideRepository.findById(guideId)
.orElseThrow(() -> new NoticeException(ErrorMessage.GUIDE_NOT_FOUND));
guideTagRepository.save(GuideTag.builder().guide(guide).tag(tag).build());
}

guide.updateDate();
return TagResponse.from(tag);
}

@Transactional
public void removeTagFromGuide(Long guideId, Long tagId) {
Guide guide = guideRepository.findById(guideId)
.orElseThrow(() -> new NoticeException(ErrorMessage.GUIDE_NOT_FOUND));

GuideTag guideTag = guideTagRepository.findByGuideIdAndTagIdAndIsActiveTrue(guideId, tagId)
.orElseThrow(() -> new GuideTagException(ErrorMessage.GUIDE_TAG_NOT_FOUND));
guideTag.softDelete();
guide.updateDate();
}

public List<TagResponse> getTagsForGuide(Long guideId) {
Expand All @@ -77,6 +77,9 @@ public List<TagResponse> getTagsForGuide(Long guideId) {

@Transactional
public TagResponse updateTagForGuide(Long guideId, Long tagId, String tagName) {
Guide guide = guideRepository.findById(guideId)
.orElseThrow(() -> new NoticeException(ErrorMessage.GUIDE_NOT_FOUND));

// 기존 GuideTag 조회
GuideTag guideTag = guideTagRepository.findByGuideIdAndTagIdAndIsActiveTrue(guideId, tagId)
.orElseThrow(() -> new GuideTagException(ErrorMessage.GUIDE_TAG_NOT_FOUND));
Expand All @@ -87,7 +90,7 @@ public TagResponse updateTagForGuide(Long guideId, Long tagId, String tagName) {

// GuideTag의 태그를 새 태그로 변경
guideTag.updateTag(newTag);

guide.updateDate();
return TagResponse.from(newTag);
}
}