-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPostServiceImpl.java
More file actions
124 lines (96 loc) · 4.69 KB
/
PostServiceImpl.java
File metadata and controls
124 lines (96 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.example.feeda.domain.post.service;
import com.example.feeda.domain.follow.entity.Follows;
import com.example.feeda.domain.follow.repository.FollowsRepository;
import com.example.feeda.domain.post.dto.PostRequestDto;
import com.example.feeda.domain.post.dto.PostResponseDto;
import com.example.feeda.domain.post.entity.Post;
import com.example.feeda.domain.post.repository.PostRepository;
import com.example.feeda.domain.profile.entity.Profile;
import com.example.feeda.domain.profile.repository.ProfileRepository;
import com.example.feeda.security.jwt.JwtPayload;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
@Service
@RequiredArgsConstructor
public class PostServiceImpl implements PostService {
private final PostRepository postRepository;
private final ProfileRepository profileRepository;
private final FollowsRepository followsRepository;
@Override
public PostResponseDto createPost(PostRequestDto postRequestDto, JwtPayload jwtPayload) {
Profile profile = profileRepository.findById(jwtPayload.getProfileId())
.orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "존재하지 않는 프로필입니다."));
Post post = new Post(postRequestDto.getTitle(), postRequestDto.getContent(),
postRequestDto.getCategory(), profile);
Post savedPost = postRepository.save(post);
return new PostResponseDto(savedPost.getId(),
savedPost.getTitle(),
savedPost.getContent(),
savedPost.getCategory());
}
@Override
@Transactional(readOnly = true)
public PostResponseDto findPostById(Long id) {
Optional<Post> optionalPost = postRepository.findById(id);
if (optionalPost.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "게시글이 존재하지 않습니다");
}
Post findPost = optionalPost.get();
return new PostResponseDto(id, findPost.getTitle(), findPost.getContent(),
findPost.getCategory());
}
@Override
@Transactional(readOnly = true)
public Page<PostResponseDto> findAll(Pageable pageable, String keyword) {
return postRepository.findAllByTitleContaining(keyword, pageable)
.map(PostResponseDto::toDto);
}
@Override
@Transactional(readOnly = true)
public Page<PostResponseDto> findFollowingAllPost(Pageable pageable, JwtPayload jwtPayload) {
Page<Follows> followings = followsRepository.findAllByFollowers_Id(
jwtPayload.getProfileId(), pageable);
List<Long> followingProfileIds = followings.stream()
.map(following -> following.getFollowings().getId())
.toList();
Pageable newPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
Sort.Direction.DESC, "updatedAt");
return postRepository.findAllByProfile_IdIn(followingProfileIds, newPageable)
.map(PostResponseDto::toDto);
}
@Override
@Transactional
public PostResponseDto updatePost(Long id, PostRequestDto requestDto, JwtPayload jwtPayload) {
Post findPost = postRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "존재하지 않는 게시글"));
if (!findPost.getProfile().getId().equals(jwtPayload.getProfileId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "권한이 없습니다.");
}
findPost.update(requestDto.getTitle(), requestDto.getCategory(), requestDto.getCategory());
Post savedPost = postRepository.save(findPost);
return new PostResponseDto(savedPost.getId(),
savedPost.getTitle(),
savedPost.getContent(),
savedPost.getCategory());
}
@Override
@Transactional
public void deletePost(Long id, JwtPayload jwtPayload) {
Post findPost = postRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "존재하지 않는 게시글"));
if (!findPost.getProfile().getId().equals(jwtPayload.getProfileId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "권한이 없습니다.");
}
postRepository.delete(findPost);
}
}