mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-07 18:47:44 +08:00
Return category and tag change logs as DTOs
This commit is contained in:
@@ -5,6 +5,7 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -18,10 +19,10 @@ public class PostChangeLogDto {
|
|||||||
private String newTitle;
|
private String newTitle;
|
||||||
private String oldContent;
|
private String oldContent;
|
||||||
private String newContent;
|
private String newContent;
|
||||||
private String oldCategory;
|
private CategoryDto oldCategory;
|
||||||
private String newCategory;
|
private CategoryDto newCategory;
|
||||||
private String oldTags;
|
private List<TagDto> oldTags;
|
||||||
private String newTags;
|
private List<TagDto> newTags;
|
||||||
private Boolean oldClosed;
|
private Boolean oldClosed;
|
||||||
private Boolean newClosed;
|
private Boolean newClosed;
|
||||||
private LocalDateTime oldPinnedAt;
|
private LocalDateTime oldPinnedAt;
|
||||||
|
|||||||
@@ -1,11 +1,28 @@
|
|||||||
package com.openisle.mapper;
|
package com.openisle.mapper;
|
||||||
|
|
||||||
|
import com.openisle.dto.CategoryDto;
|
||||||
import com.openisle.dto.PostChangeLogDto;
|
import com.openisle.dto.PostChangeLogDto;
|
||||||
|
import com.openisle.dto.TagDto;
|
||||||
import com.openisle.model.*;
|
import com.openisle.model.*;
|
||||||
|
import com.openisle.repository.CategoryRepository;
|
||||||
|
import com.openisle.repository.TagRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class PostChangeLogMapper {
|
public class PostChangeLogMapper {
|
||||||
|
|
||||||
|
private final CategoryRepository categoryRepository;
|
||||||
|
private final TagRepository tagRepository;
|
||||||
|
private final CategoryMapper categoryMapper;
|
||||||
|
private final TagMapper tagMapper;
|
||||||
|
|
||||||
public PostChangeLogDto toDto(PostChangeLog log) {
|
public PostChangeLogDto toDto(PostChangeLog log) {
|
||||||
PostChangeLogDto dto = new PostChangeLogDto();
|
PostChangeLogDto dto = new PostChangeLogDto();
|
||||||
dto.setId(log.getId());
|
dto.setId(log.getId());
|
||||||
@@ -22,11 +39,11 @@ public class PostChangeLogMapper {
|
|||||||
dto.setOldContent(c.getOldContent());
|
dto.setOldContent(c.getOldContent());
|
||||||
dto.setNewContent(c.getNewContent());
|
dto.setNewContent(c.getNewContent());
|
||||||
} else if (log instanceof PostCategoryChangeLog cat) {
|
} else if (log instanceof PostCategoryChangeLog cat) {
|
||||||
dto.setOldCategory(cat.getOldCategory());
|
dto.setOldCategory(mapCategory(cat.getOldCategory()));
|
||||||
dto.setNewCategory(cat.getNewCategory());
|
dto.setNewCategory(mapCategory(cat.getNewCategory()));
|
||||||
} else if (log instanceof PostTagChangeLog tag) {
|
} else if (log instanceof PostTagChangeLog tag) {
|
||||||
dto.setOldTags(tag.getOldTags());
|
dto.setOldTags(mapTags(tag.getOldTags()));
|
||||||
dto.setNewTags(tag.getNewTags());
|
dto.setNewTags(mapTags(tag.getNewTags()));
|
||||||
} else if (log instanceof PostClosedChangeLog cl) {
|
} else if (log instanceof PostClosedChangeLog cl) {
|
||||||
dto.setOldClosed(cl.isOldClosed());
|
dto.setOldClosed(cl.isOldClosed());
|
||||||
dto.setNewClosed(cl.isNewClosed());
|
dto.setNewClosed(cl.isNewClosed());
|
||||||
@@ -39,4 +56,37 @@ public class PostChangeLogMapper {
|
|||||||
}
|
}
|
||||||
return dto;
|
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;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import com.openisle.model.Category;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface CategoryRepository extends JpaRepository<Category, Long> {
|
public interface CategoryRepository extends JpaRepository<Category, Long> {
|
||||||
List<Category> findByNameContainingIgnoreCase(String keyword);
|
List<Category> findByNameContainingIgnoreCase(String keyword);
|
||||||
|
|
||||||
|
Optional<Category> findByName(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface TagRepository extends JpaRepository<Tag, Long> {
|
public interface TagRepository extends JpaRepository<Tag, Long> {
|
||||||
List<Tag> findByNameContainingIgnoreCase(String keyword);
|
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> findByCreatorOrderByCreatedAtDesc(User creator, Pageable pageable);
|
||||||
List<Tag> findByCreator(User creator);
|
List<Tag> findByCreator(User creator);
|
||||||
|
|
||||||
|
Optional<Tag> findByName(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user