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 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 getPost(@PathVariable Long id) { Post post = postService.getPost(id); return ResponseEntity.ok(toDto(post)); } @GetMapping public List 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; } }