Add Post module

This commit is contained in:
Tim
2025-06-30 19:00:43 +08:00
parent fe894834b7
commit d419b45515
5 changed files with 150 additions and 0 deletions

View 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;
}
}