Skip to content

Commit

Permalink
feat: post service logic with controller fix
Browse files Browse the repository at this point in the history
LAPLACE4A authored and Village-GG-Water committed Nov 3, 2024
1 parent db69b58 commit c0124ce
Showing 4 changed files with 52 additions and 29 deletions.
21 changes: 10 additions & 11 deletions src/main/java/com/kert/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -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<Post> createPost(@RequestBody Post post) {
Post createdPost = postService.createPost(post);
public ResponseEntity<Post> createPost(@RequestBody Post post, HttpServletRequest request) {
Post createdPost = postService.createPost(post, request);
return ResponseEntity.ok(createdPost);
}

@GetMapping
public ResponseEntity<List<Post>> getPostsByTag(@RequestParam(required = false, name = "tag") String tag) {
List<Post> posts;
if (tag != null) {
posts = postService.getPostsByTag(tag);
} else {
posts = postService.getAllPosts();
}
public ResponseEntity<Page<Post>> getPosts(
@RequestParam(required = false) String search,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size) {

Page<Post> posts = postService.searchPosts(search, page, size);
return ResponseEntity.ok(posts);
}

5 changes: 4 additions & 1 deletion src/main/java/com/kert/model/Post.java
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 4 additions & 6 deletions src/main/java/com/kert/repository/PostRepository.java
Original file line number Diff line number Diff line change
@@ -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<Post, Long> {
List<Post> findByTag(String Tag);
}
Page<Post> findByTitleContainingOrTagContaining(String title, String tag, Pageable pageable);
}
45 changes: 34 additions & 11 deletions src/main/java/com/kert/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,68 @@
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<Post> 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<Post> 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<Post> 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
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);

0 comments on commit c0124ce

Please sign in to comment.