mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 14:40:49 +08:00
Add category module and require post category
This commit is contained in:
@@ -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
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ public class PostController {
|
||||
dto.setContent(post.getContent());
|
||||
dto.setCreatedAt(post.getCreatedAt());
|
||||
dto.setAuthor(post.getAuthor().getUsername());
|
||||
dto.setCategory(post.getCategory().getName());
|
||||
dto.setViews(post.getViews());
|
||||
|
||||
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
|
||||
@@ -119,6 +120,7 @@ public class PostController {
|
||||
|
||||
@Data
|
||||
private static class PostRequest {
|
||||
private Long categoryId;
|
||||
private String title;
|
||||
private String content;
|
||||
}
|
||||
@@ -130,6 +132,7 @@ public class PostController {
|
||||
private String content;
|
||||
private LocalDateTime createdAt;
|
||||
private String author;
|
||||
private String category;
|
||||
private long views;
|
||||
private List<CommentDto> comments;
|
||||
private List<ReactionDto> reactions;
|
||||
|
||||
Reference in New Issue
Block a user