Compare commits

..

1 Commits

Author SHA1 Message Date
Tim
3b1843b6dd Return category and tag change logs as DTOs 2025-09-08 14:45:47 +08:00
3 changed files with 54 additions and 34 deletions

View File

@@ -4,14 +4,25 @@ import com.openisle.dto.CategoryDto;
import com.openisle.dto.PostChangeLogDto;
import com.openisle.dto.TagDto;
import com.openisle.model.*;
import com.openisle.repository.CategoryRepository;
import com.openisle.repository.TagRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Component
@RequiredArgsConstructor
public class PostChangeLogMapper {
private final CategoryRepository categoryRepository;
private final TagRepository tagRepository;
private final CategoryMapper categoryMapper;
private final TagMapper tagMapper;
public PostChangeLogDto toDto(PostChangeLog log) {
PostChangeLogDto dto = new PostChangeLogDto();
dto.setId(log.getId());
@@ -28,41 +39,11 @@ public class PostChangeLogMapper {
dto.setOldContent(c.getOldContent());
dto.setNewContent(c.getNewContent());
} else if (log instanceof PostCategoryChangeLog cat) {
if (cat.getOldCategory() != null) {
CategoryDto oldCat = new CategoryDto();
oldCat.setName(cat.getOldCategory());
dto.setOldCategory(oldCat);
}
if (cat.getNewCategory() != null) {
CategoryDto newCat = new CategoryDto();
newCat.setName(cat.getNewCategory());
dto.setNewCategory(newCat);
}
dto.setOldCategory(mapCategory(cat.getOldCategory()));
dto.setNewCategory(mapCategory(cat.getNewCategory()));
} else if (log instanceof PostTagChangeLog tag) {
if (tag.getOldTags() != null && !tag.getOldTags().isBlank()) {
List<TagDto> oldTags = Arrays.stream(tag.getOldTags().split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.map(name -> {
TagDto t = new TagDto();
t.setName(name);
return t;
})
.collect(Collectors.toList());
dto.setOldTags(oldTags);
}
if (tag.getNewTags() != null && !tag.getNewTags().isBlank()) {
List<TagDto> newTags = Arrays.stream(tag.getNewTags().split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.map(name -> {
TagDto t = new TagDto();
t.setName(name);
return t;
})
.collect(Collectors.toList());
dto.setNewTags(newTags);
}
dto.setOldTags(mapTags(tag.getOldTags()));
dto.setNewTags(mapTags(tag.getNewTags()));
} else if (log instanceof PostClosedChangeLog cl) {
dto.setOldClosed(cl.isOldClosed());
dto.setNewClosed(cl.isNewClosed());
@@ -75,4 +56,37 @@ public class PostChangeLogMapper {
}
return dto;
}
private CategoryDto mapCategory(String name) {
if (name == null) {
return null;
}
return categoryRepository.findByName(name)
.map(categoryMapper::toDto)
.orElseGet(() -> {
CategoryDto dto = new CategoryDto();
dto.setName(name);
return dto;
});
}
private List<TagDto> mapTags(String tags) {
if (tags == null || tags.isBlank()) {
return Collections.emptyList();
}
return Arrays.stream(tags.split(","))
.map(String::trim)
.map(this::mapTag)
.collect(Collectors.toList());
}
private TagDto mapTag(String name) {
return tagRepository.findByName(name)
.map(tagMapper::toDto)
.orElseGet(() -> {
TagDto dto = new TagDto();
dto.setName(name);
return dto;
});
}
}

View File

@@ -4,7 +4,10 @@ import com.openisle.model.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface CategoryRepository extends JpaRepository<Category, Long> {
List<Category> findByNameContainingIgnoreCase(String keyword);
Optional<Category> findByName(String name);
}

View File

@@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;
public interface TagRepository extends JpaRepository<Tag, Long> {
List<Tag> findByNameContainingIgnoreCase(String keyword);
@@ -15,4 +16,6 @@ public interface TagRepository extends JpaRepository<Tag, Long> {
List<Tag> findByCreatorOrderByCreatedAtDesc(User creator, Pageable pageable);
List<Tag> findByCreator(User creator);
Optional<Tag> findByName(String name);
}