Skip to content

Commit

Permalink
Merge pull request #169 from bounswe/issue#166-return-posts-of-specif…
Browse files Browse the repository at this point in the history
…ic-user

Issue#166 return posts of specific user
  • Loading branch information
oguzhekim authored Oct 21, 2024
2 parents 40974e8 + 02cea55 commit 3b62fed
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public ResponseEntity<List<PostResponse>> fetchPosts(
return ResponseEntity.ok(posts);
}

@GetMapping("/user/{username}")
public ResponseEntity<List<PostResponse>> getPostsByUser(@PathVariable String username) {
try {
List<PostResponse> posts = postService.getPostsByUser(username);
return ResponseEntity.ok(posts);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

@DeleteMapping("/{postId}")
public ResponseEntity<String> deletePost(@PathVariable Long postId, HttpServletRequest request) throws IllegalAccessException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Builder;
import lombok.Data;

import java.util.List;
import java.util.Set;

@Data
Expand All @@ -16,4 +17,5 @@ public class UserProfileResponse {
private Role role;
private Set<String> followers;
private Set<String> following;
private List<PostResponse> posts;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.group7.demo.repository;

import com.group7.demo.models.Post;
import com.group7.demo.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -9,6 +10,7 @@
import java.util.Set;

public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByUser(User user);

@Query("SELECT DISTINCT p FROM Post p JOIN p.tags t WHERE t.name IN :tagNames")
List<Post> findPostsByTags(@Param("tagNames") Set<String> tagNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public List<PostResponse> getAllPosts() {
.collect(Collectors.toList());
}

public List<PostResponse> getPostsByUser(String username) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new EntityNotFoundException("User not found with username: " + username));

List<Post> posts = postRepository.findByUser(user);

return posts.stream()
.map(this::mapToPostResponse)
.collect(Collectors.toList());
}

@Transactional
public void deletePost(Long postId, HttpServletRequest request) throws IllegalAccessException {
User authenticatedUser = authenticationService.getAuthenticatedUserInternal(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.group7.demo.services;

import com.group7.demo.dtos.PostResponse;
import com.group7.demo.dtos.UserProfileResponse;
import com.group7.demo.models.User;
import com.group7.demo.repository.UserRepository;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -15,6 +17,7 @@
public class UserService {
private final UserRepository userRepository;
private final AuthenticationService authenticationService;
private final PostService postService;

public UserProfileResponse getUserProfile(String username) throws Exception {
User user = userRepository.findByUsername(username)
Expand All @@ -30,12 +33,15 @@ public UserProfileResponse getUserProfile(String username) throws Exception {
.map(User::getUsername)
.collect(Collectors.toSet());

List<PostResponse> posts = postService.getPostsByUser(user.getUsername());

return UserProfileResponse.builder()
.username(user.getUsername())
.fullName(user.getFullName())
.role(user.getRole())
.followers(followers)
.following(following)
.posts(posts)
.build();
}

Expand Down

0 comments on commit 3b62fed

Please sign in to comment.