Add tag module with post associations

This commit is contained in:
Tim
2025-07-02 13:19:55 +08:00
parent 3449310a19
commit f50201aef6
13 changed files with 249 additions and 9 deletions

View File

@@ -69,9 +69,12 @@ public class SecurityConfig {
.requestMatchers(HttpMethod.GET, "/api/posts/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/comments/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/categories/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/tags/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/search/**").permitAll()
.requestMatchers(HttpMethod.POST, "/api/categories/**").hasAuthority("ADMIN")
.requestMatchers(HttpMethod.POST, "/api/tags/**").hasAuthority("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/api/categories/**").hasAuthority("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/api/tags/**").hasAuthority("ADMIN")
.requestMatchers("/api/admin/**").hasAuthority("ADMIN")
.anyRequest().authenticated()
)
@@ -89,7 +92,8 @@ public class SecurityConfig {
boolean publicGet = "GET".equalsIgnoreCase(request.getMethod()) &&
(uri.startsWith("/api/posts") || uri.startsWith("/api/comments") ||
uri.startsWith("/api/categories") || uri.startsWith("/api/search"));
uri.startsWith("/api/categories") || uri.startsWith("/api/tags") ||
uri.startsWith("/api/search"));
if (authHeader != null && authHeader.startsWith("Bearer ")) {
String token = authHeader.substring(7);

View File

@@ -38,7 +38,8 @@ public class PostController {
if (captchaEnabled && postCaptchaEnabled && !captchaService.verify(req.getCaptcha())) {
return ResponseEntity.badRequest().build();
}
Post post = postService.createPost(auth.getName(), req.getCategoryId(), req.getTitle(), req.getContent());
Post post = postService.createPost(auth.getName(), req.getCategoryId(),
req.getTitle(), req.getContent(), req.getTagIds());
return ResponseEntity.ok(toDto(post));
}
@@ -69,6 +70,7 @@ public class PostController {
dto.setCreatedAt(post.getCreatedAt());
dto.setAuthor(post.getAuthor().getUsername());
dto.setCategory(post.getCategory().getName());
dto.setTags(post.getTags().stream().map(com.openisle.model.Tag::getName).collect(Collectors.toList()));
dto.setViews(post.getViews());
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
@@ -130,6 +132,7 @@ public class PostController {
private Long categoryId;
private String title;
private String content;
private java.util.List<Long> tagIds;
private String captcha;
}
@@ -141,6 +144,7 @@ public class PostController {
private LocalDateTime createdAt;
private String author;
private String category;
private java.util.List<String> tags;
private long views;
private List<CommentDto> comments;
private List<ReactionDto> reactions;

View File

@@ -0,0 +1,58 @@
package com.openisle.controller;
import com.openisle.model.Tag;
import com.openisle.service.TagService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/api/tags")
@RequiredArgsConstructor
public class TagController {
private final TagService tagService;
@PostMapping
public TagDto create(@RequestBody TagRequest req) {
Tag tag = tagService.createTag(req.getName());
return toDto(tag);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
tagService.deleteTag(id);
}
@GetMapping
public List<TagDto> list() {
return tagService.listTags().stream()
.map(this::toDto)
.collect(Collectors.toList());
}
@GetMapping("/{id}")
public TagDto get(@PathVariable Long id) {
return toDto(tagService.getTag(id));
}
private TagDto toDto(Tag tag) {
TagDto dto = new TagDto();
dto.setId(tag.getId());
dto.setName(tag.getName());
return dto;
}
@Data
private static class TagRequest {
private String name;
}
@Data
private static class TagDto {
private Long id;
private String name;
}
}

View File

@@ -5,6 +5,11 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.HashSet;
import java.util.Set;
import com.openisle.model.Tag;
import java.time.LocalDateTime;
@@ -38,6 +43,12 @@ public class Post {
@JoinColumn(name = "category_id")
private Category category;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "post_tags",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private java.util.Set<Tag> tags = new java.util.HashSet<>();
@Column(nullable = false)
private long views = 0;

View File

@@ -0,0 +1,20 @@
package com.openisle.model;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "tags")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String name;
}

View File

@@ -0,0 +1,7 @@
package com.openisle.repository;
import com.openisle.model.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TagRepository extends JpaRepository<Tag, Long> {
}

View File

@@ -8,6 +8,7 @@ import com.openisle.model.Category;
import com.openisle.repository.PostRepository;
import com.openisle.repository.UserRepository;
import com.openisle.repository.CategoryRepository;
import com.openisle.repository.TagRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -20,29 +21,44 @@ public class PostService {
private final PostRepository postRepository;
private final UserRepository userRepository;
private final CategoryRepository categoryRepository;
private final TagRepository tagRepository;
private final PublishMode publishMode;
@org.springframework.beans.factory.annotation.Autowired
public PostService(PostRepository postRepository,
UserRepository userRepository,
CategoryRepository categoryRepository,
TagRepository tagRepository,
@Value("${app.post.publish-mode:DIRECT}") PublishMode publishMode) {
this.postRepository = postRepository;
this.userRepository = userRepository;
this.categoryRepository = categoryRepository;
this.tagRepository = tagRepository;
this.publishMode = publishMode;
}
public Post createPost(String username, Long categoryId, String title, String content) {
public Post createPost(String username,
Long categoryId,
String title,
String content,
java.util.List<Long> tagIds) {
if (tagIds == null || tagIds.isEmpty()) {
throw new IllegalArgumentException("At least one tag required");
}
User author = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new IllegalArgumentException("Category not found"));
java.util.List<com.openisle.model.Tag> tags = tagRepository.findAllById(tagIds);
if (tags.isEmpty()) {
throw new IllegalArgumentException("Tag not found");
}
Post post = new Post();
post.setTitle(title);
post.setContent(content);
post.setAuthor(author);
post.setCategory(category);
post.setTags(new java.util.HashSet<>(tags));
post.setStatus(publishMode == PublishMode.REVIEW ? PostStatus.PENDING : PostStatus.PUBLISHED);
return postRepository.save(post);
}

View File

@@ -0,0 +1,33 @@
package com.openisle.service;
import com.openisle.model.Tag;
import com.openisle.repository.TagRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class TagService {
private final TagRepository tagRepository;
public Tag createTag(String name) {
Tag tag = new Tag();
tag.setName(name);
return tagRepository.save(tag);
}
public void deleteTag(Long id) {
tagRepository.deleteById(id);
}
public Tag getTag(Long id) {
return tagRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Tag not found"));
}
public List<Tag> listTags() {
return tagRepository.findAll();
}
}