Merge pull request #19 from nagisa77/codex/add-post-category-module

Add Post categories
This commit is contained in:
Tim
2025-06-30 23:07:31 +08:00
committed by GitHub
10 changed files with 163 additions and 7 deletions
@@ -68,6 +68,9 @@ public class SecurityConfig {
.requestMatchers(HttpMethod.POST, "/api/auth/**").permitAll() .requestMatchers(HttpMethod.POST, "/api/auth/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/posts/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/posts/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/comments/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/comments/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/categories/**").permitAll()
.requestMatchers(HttpMethod.POST, "/api/categories/**").hasAuthority("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/api/categories/**").hasAuthority("ADMIN")
.requestMatchers("/api/admin/**").hasAuthority("ADMIN") .requestMatchers("/api/admin/**").hasAuthority("ADMIN")
.anyRequest().authenticated() .anyRequest().authenticated()
) )
@@ -84,7 +87,7 @@ public class SecurityConfig {
String uri = request.getRequestURI(); String uri = request.getRequestURI();
boolean publicGet = "GET".equalsIgnoreCase(request.getMethod()) && boolean publicGet = "GET".equalsIgnoreCase(request.getMethod()) &&
(uri.startsWith("/api/posts") || uri.startsWith("/api/comments")); (uri.startsWith("/api/posts") || uri.startsWith("/api/comments") || uri.startsWith("/api/categories"));
if (authHeader != null && authHeader.startsWith("Bearer ")) { if (authHeader != null && authHeader.startsWith("Bearer ")) {
String token = authHeader.substring(7); String token = authHeader.substring(7);
@@ -0,0 +1,58 @@
package com.openisle.controller;
import com.openisle.model.Category;
import com.openisle.service.CategoryService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {
private final CategoryService categoryService;
@PostMapping
public CategoryDto create(@RequestBody CategoryRequest req) {
Category c = categoryService.createCategory(req.getName());
return toDto(c);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
categoryService.deleteCategory(id);
}
@GetMapping
public List<CategoryDto> list() {
return categoryService.listCategories().stream()
.map(this::toDto)
.collect(Collectors.toList());
}
@GetMapping("/{id}")
public CategoryDto get(@PathVariable Long id) {
return toDto(categoryService.getCategory(id));
}
private CategoryDto toDto(Category c) {
CategoryDto dto = new CategoryDto();
dto.setId(c.getId());
dto.setName(c.getName());
return dto;
}
@Data
private static class CategoryRequest {
private String name;
}
@Data
private static class CategoryDto {
private Long id;
private String name;
}
}
@@ -39,7 +39,7 @@ public class PostController {
@PostMapping @PostMapping
public ResponseEntity<PostDto> createPost(@RequestBody PostRequest req, Authentication auth) { public ResponseEntity<PostDto> createPost(@RequestBody PostRequest req, Authentication auth) {
Post post = postService.createPost(auth.getName(), req.getTitle(), req.getContent()); Post post = postService.createPost(auth.getName(), req.getCategoryId(), req.getTitle(), req.getContent());
return ResponseEntity.ok(toDto(post)); return ResponseEntity.ok(toDto(post));
} }
@@ -61,6 +61,7 @@ public class PostController {
dto.setContent(post.getContent()); dto.setContent(post.getContent());
dto.setCreatedAt(post.getCreatedAt()); dto.setCreatedAt(post.getCreatedAt());
dto.setAuthor(post.getAuthor().getUsername()); dto.setAuthor(post.getAuthor().getUsername());
dto.setCategory(post.getCategory().getName());
dto.setViews(post.getViews()); dto.setViews(post.getViews());
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId()) List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
@@ -119,6 +120,7 @@ public class PostController {
@Data @Data
private static class PostRequest { private static class PostRequest {
private Long categoryId;
private String title; private String title;
private String content; private String content;
} }
@@ -130,6 +132,7 @@ public class PostController {
private String content; private String content;
private LocalDateTime createdAt; private LocalDateTime createdAt;
private String author; private String author;
private String category;
private long views; private long views;
private List<CommentDto> comments; private List<CommentDto> comments;
private List<ReactionDto> reactions; private List<ReactionDto> reactions;
@@ -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 = "categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String name;
}
@@ -30,6 +30,10 @@ public class Post {
@JoinColumn(name = "author_id") @JoinColumn(name = "author_id")
private User author; private User author;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;
@Column(nullable = false) @Column(nullable = false)
private long views = 0; private long views = 0;
@@ -0,0 +1,7 @@
package com.openisle.repository;
import com.openisle.model.Category;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
@@ -0,0 +1,33 @@
package com.openisle.service;
import com.openisle.model.Category;
import com.openisle.repository.CategoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class CategoryService {
private final CategoryRepository categoryRepository;
public Category createCategory(String name) {
Category category = new Category();
category.setName(name);
return categoryRepository.save(category);
}
public void deleteCategory(Long id) {
categoryRepository.deleteById(id);
}
public Category getCategory(Long id) {
return categoryRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Category not found"));
}
public List<Category> listCategories() {
return categoryRepository.findAll();
}
}
@@ -2,8 +2,10 @@ package com.openisle.service;
import com.openisle.model.Post; import com.openisle.model.Post;
import com.openisle.model.User; import com.openisle.model.User;
import com.openisle.model.Category;
import com.openisle.repository.PostRepository; import com.openisle.repository.PostRepository;
import com.openisle.repository.UserRepository; import com.openisle.repository.UserRepository;
import com.openisle.repository.CategoryRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -14,14 +16,18 @@ import java.util.List;
public class PostService { public class PostService {
private final PostRepository postRepository; private final PostRepository postRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
private final CategoryRepository categoryRepository;
public Post createPost(String username, String title, String content) { public Post createPost(String username, Long categoryId, String title, String content) {
User author = userRepository.findByUsername(username) User author = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found")); .orElseThrow(() -> new IllegalArgumentException("User not found"));
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new IllegalArgumentException("Category not found"));
Post post = new Post(); Post post = new Post();
post.setTitle(title); post.setTitle(title);
post.setContent(content); post.setContent(content);
post.setAuthor(author); post.setAuthor(author);
post.setCategory(category);
return postRepository.save(post); return postRepository.save(post);
} }
@@ -2,6 +2,7 @@ package com.openisle.controller;
import com.openisle.model.Post; import com.openisle.model.Post;
import com.openisle.model.User; import com.openisle.model.User;
import com.openisle.model.Category;
import com.openisle.service.PostService; import com.openisle.service.PostService;
import com.openisle.service.CommentService; import com.openisle.service.CommentService;
import com.openisle.service.ReactionService; import com.openisle.service.ReactionService;
@@ -41,18 +42,22 @@ class PostControllerTest {
void createAndGetPost() throws Exception { void createAndGetPost() throws Exception {
User user = new User(); User user = new User();
user.setUsername("alice"); user.setUsername("alice");
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
Post post = new Post(); Post post = new Post();
post.setId(1L); post.setId(1L);
post.setTitle("t"); post.setTitle("t");
post.setContent("c"); post.setContent("c");
post.setCreatedAt(LocalDateTime.now()); post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user); post.setAuthor(user);
Mockito.when(postService.createPost(eq("alice"), eq("t"), eq("c"))).thenReturn(post); post.setCategory(cat);
Mockito.when(postService.createPost(eq("alice"), eq(1L), eq("t"), eq("c"))).thenReturn(post);
Mockito.when(postService.getPost(1L)).thenReturn(post); Mockito.when(postService.getPost(1L)).thenReturn(post);
mockMvc.perform(post("/api/posts") mockMvc.perform(post("/api/posts")
.contentType("application/json") .contentType("application/json")
.content("{\"title\":\"t\",\"content\":\"c\"}") .content("{\"title\":\"t\",\"content\":\"c\",\"categoryId\":1}")
.principal(new UsernamePasswordAuthenticationToken("alice", "p"))) .principal(new UsernamePasswordAuthenticationToken("alice", "p")))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.title").value("t")); .andExpect(jsonPath("$.title").value("t"));
@@ -66,12 +71,16 @@ class PostControllerTest {
void listPosts() throws Exception { void listPosts() throws Exception {
User user = new User(); User user = new User();
user.setUsername("bob"); user.setUsername("bob");
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
Post post = new Post(); Post post = new Post();
post.setId(2L); post.setId(2L);
post.setTitle("hello"); post.setTitle("hello");
post.setContent("world"); post.setContent("world");
post.setCreatedAt(LocalDateTime.now()); post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user); post.setAuthor(user);
post.setCategory(cat);
Mockito.when(postService.listPosts()).thenReturn(List.of(post)); Mockito.when(postService.listPosts()).thenReturn(List.of(post));
mockMvc.perform(get("/api/posts")) mockMvc.perform(get("/api/posts"))
@@ -1,7 +1,9 @@
package com.openisle.integration; package com.openisle.integration;
import com.openisle.model.User; import com.openisle.model.User;
import com.openisle.model.Category;
import com.openisle.repository.UserRepository; import com.openisle.repository.UserRepository;
import com.openisle.repository.CategoryRepository;
import com.openisle.service.EmailService; import com.openisle.service.EmailService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +26,9 @@ class ComplexFlowIntegrationTest {
@Autowired @Autowired
private UserRepository users; private UserRepository users;
@Autowired
private CategoryRepository categories;
@MockBean @MockBean
private EmailService emailService; private EmailService emailService;
@@ -52,8 +57,12 @@ class ComplexFlowIntegrationTest {
String t1 = registerAndLogin("alice", "a@example.com"); String t1 = registerAndLogin("alice", "a@example.com");
String t2 = registerAndLogin("bob", "b@example.com"); String t2 = registerAndLogin("bob", "b@example.com");
Category cat = new Category();
cat.setName("general");
cat = categories.save(cat);
ResponseEntity<Map> postResp = postJson("/api/posts", ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "Hello", "content", "World"), t1); Map.of("title", "Hello", "content", "World", "categoryId", cat.getId()), t1);
Long postId = ((Number)postResp.getBody().get("id")).longValue(); Long postId = ((Number)postResp.getBody().get("id")).longValue();
ResponseEntity<Map> c1Resp = postJson("/api/posts/" + postId + "/comments", ResponseEntity<Map> c1Resp = postJson("/api/posts/" + postId + "/comments",
@@ -87,8 +96,12 @@ class ComplexFlowIntegrationTest {
String t1 = registerAndLogin("carol", "c@example.com"); String t1 = registerAndLogin("carol", "c@example.com");
String t2 = registerAndLogin("dave", "d@example.com"); String t2 = registerAndLogin("dave", "d@example.com");
Category cat = new Category();
cat.setName("general");
cat = categories.save(cat);
ResponseEntity<Map> postResp = postJson("/api/posts", ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "React", "content", "Test"), t1); Map.of("title", "React", "content", "Test", "categoryId", cat.getId()), t1);
Long postId = ((Number)postResp.getBody().get("id")).longValue(); Long postId = ((Number)postResp.getBody().get("id")).longValue();
postJson("/api/posts/" + postId + "/reactions", postJson("/api/posts/" + postId + "/reactions",