diff --git a/src/main/java/com/kert/controller/PostController.java b/src/main/java/com/kert/controller/PostController.java index f3787cc..6b81ad8 100644 --- a/src/main/java/com/kert/controller/PostController.java +++ b/src/main/java/com/kert/controller/PostController.java @@ -3,11 +3,11 @@ import com.kert.model.Post; import com.kert.service.PostService; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; import org.springframework.http.ResponseEntity; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.*; -import java.util.List; - @RestController @RequestMapping("/posts") @RequiredArgsConstructor @@ -16,19 +16,18 @@ public class PostController { private final PostService postService; @PostMapping - public ResponseEntity createPost(@RequestBody Post post) { - Post createdPost = postService.createPost(post); + public ResponseEntity createPost(@RequestBody Post post, HttpServletRequest request) { + Post createdPost = postService.createPost(post, request); return ResponseEntity.ok(createdPost); } @GetMapping - public ResponseEntity> getPostsByTag(@RequestParam(required = false, name = "tag") String tag) { - List posts; - if (tag != null) { - posts = postService.getPostsByTag(tag); - } else { - posts = postService.getAllPosts(); - } + public ResponseEntity> getPosts( + @RequestParam(required = false) String search, + @RequestParam(defaultValue = "1") int page, + @RequestParam(defaultValue = "10") int size) { + + Page posts = postService.searchPosts(search, page, size); return ResponseEntity.ok(posts); } diff --git a/src/main/java/com/kert/model/Post.java b/src/main/java/com/kert/model/Post.java index f97ba63..c25da85 100644 --- a/src/main/java/com/kert/model/Post.java +++ b/src/main/java/com/kert/model/Post.java @@ -33,12 +33,15 @@ public class Post { @Column(length = 100) private String tag; + @Column(nullable = false) + private String description; + @Lob @Column(nullable = false) @NotBlank(message = "Content is mandatory") private String content; - @ManyToOne(fetch = FetchType.LAZY) + @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) @JoinColumn(name = "student_id", nullable = false) @NotNull(message = "User is mandatory") private User user; diff --git a/src/main/java/com/kert/repository/PostRepository.java b/src/main/java/com/kert/repository/PostRepository.java index 798881d..7fd188b 100644 --- a/src/main/java/com/kert/repository/PostRepository.java +++ b/src/main/java/com/kert/repository/PostRepository.java @@ -1,12 +1,10 @@ package com.kert.repository; import com.kert.model.Post; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import java.util.List; - -@Repository public interface PostRepository extends JpaRepository { - List findByTag(String Tag); -} + Page findByTitleContainingOrTagContaining(String title, String tag, Pageable pageable); +} \ No newline at end of file diff --git a/src/main/java/com/kert/service/PostService.java b/src/main/java/com/kert/service/PostService.java index 441fa3d..8430799 100644 --- a/src/main/java/com/kert/service/PostService.java +++ b/src/main/java/com/kert/service/PostService.java @@ -1,34 +1,56 @@ package com.kert.service; import com.kert.model.Post; +import com.kert.model.User; import com.kert.repository.PostRepository; +import com.kert.repository.UserRepository; +import com.kert.config.JwtTokenProvider; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; - import org.springframework.transaction.annotation.Transactional; -import java.util.List; +import jakarta.servlet.http.HttpServletRequest; @Service @RequiredArgsConstructor public class PostService { private final PostRepository postRepository; + private final UserRepository userRepository; + private final JwtTokenProvider jwtTokenProvider; + + public Post createPost(Post post, HttpServletRequest request) { + String token = extractTokenFromRequest(request); + Long userId = jwtTokenProvider.getUserIdFromJWT(token); + User user = userRepository.findById(userId).orElseThrow(); + + post.setUser(user); - public Post createPost(Post post) { return postRepository.save(post); } - public List getAllPosts() { - return postRepository.findAll(); + private String extractTokenFromRequest(HttpServletRequest request) { + String bearerToken = request.getHeader("Authorization"); + if (bearerToken != null && bearerToken.startsWith("Bearer ")) { + return bearerToken.substring(7); + } + return null; } - public Post getPostById(Long id) { - return postRepository.findById(id) - .orElseThrow(() -> new RuntimeException("Post not found with id: " + id)); + public Page searchPosts(String search, int page, int size) { + Pageable pageable = PageRequest.of(page, size); + if (search != null && !search.isEmpty()) { + return postRepository.findByTitleContainingOrTagContaining(search, search, pageable); + } else { + return postRepository.findAll(pageable); + } } - public List getPostsByTag(String tag) { - return postRepository.findByTag(tag); + public Post getPostById(Long id) { + return postRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Post not found with id: " + id)); } @Transactional @@ -36,10 +58,11 @@ public Post updatePost(Long id, Post postDetails) { Post post = getPostById(id); post.setTitle(postDetails.getTitle()); post.setTag(postDetails.getTag()); + post.setDescription(postDetails.getDescription()); post.setContent(postDetails.getContent()); return postRepository.save(post); } - + @Transactional public void deletePost(Long id) { Post post = getPostById(id);