mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 22:41:05 +08:00
Add Post module
This commit is contained in:
64
src/main/java/com/openisle/controller/PostController.java
Normal file
64
src/main/java/com/openisle/controller/PostController.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.service.PostService;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/posts")
|
||||
@RequiredArgsConstructor
|
||||
public class PostController {
|
||||
private final PostService postService;
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<PostDto> createPost(@RequestBody PostRequest req, Authentication auth) {
|
||||
Post post = postService.createPost(auth.getName(), req.getTitle(), req.getContent());
|
||||
return ResponseEntity.ok(toDto(post));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<PostDto> getPost(@PathVariable Long id) {
|
||||
Post post = postService.getPost(id);
|
||||
return ResponseEntity.ok(toDto(post));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<PostDto> listPosts() {
|
||||
return postService.listPosts().stream().map(this::toDto).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private PostDto toDto(Post post) {
|
||||
PostDto dto = new PostDto();
|
||||
dto.setId(post.getId());
|
||||
dto.setTitle(post.getTitle());
|
||||
dto.setContent(post.getContent());
|
||||
dto.setCreatedAt(post.getCreatedAt());
|
||||
dto.setAuthor(post.getAuthor().getUsername());
|
||||
dto.setViews(post.getViews());
|
||||
return dto;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class PostRequest {
|
||||
private String title;
|
||||
private String content;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class PostDto {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String content;
|
||||
private LocalDateTime createdAt;
|
||||
private String author;
|
||||
private long views;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user