src/main/java/com/forum/controller/PostController.java
package com.forum.controller;
import com.forum.dto.PostRequest;
import com.forum.dto.PostResponse;
import com.forum.service.PostService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import static org.springframework.http.ResponseEntity.status;
import java.util.List;
@RestController
@RequestMapping("/api/posts")
@AllArgsConstructor
@Slf4j
public class PostController {
private final PostService postService;
@PostMapping
public ResponseEntity<Void> createPost(@RequestBody PostRequest postRequest) {
postService.create(postRequest);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@GetMapping
public ResponseEntity<List<PostResponse>> getAllPosts() {
return status(HttpStatus.OK).body(postService.getAllPosts());
}
@GetMapping("/{id}")
public ResponseEntity<PostResponse> getPost(@PathVariable Long id) {
return status(HttpStatus.OK).body(postService.getPostById(id));
}
@GetMapping("by-subforum/{id}")
public ResponseEntity<List<PostResponse>> getPostsBySubreddit(@PathVariable Long id) {
return status(HttpStatus.OK).body(postService.getPostsBySubforum(id));
}
@GetMapping("by-user/{username}")
public ResponseEntity<List<PostResponse>> getPostsByUsername(@PathVariable String username) {
return status(HttpStatus.OK).body(postService.getPostsByUsername(username));
}
}