-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPostService.java
More file actions
47 lines (38 loc) · 1.62 KB
/
PostService.java
File metadata and controls
47 lines (38 loc) · 1.62 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
package com.makefire.anonymous.service.post;
import com.makefire.anonymous.domain.post.entity.Post;
import com.makefire.anonymous.domain.post.repository.PostRepository;
import com.makefire.anonymous.rest.dto.request.post.PostRequest;
import com.makefire.anonymous.rest.dto.response.post.PostResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@AllArgsConstructor
public class PostService {
private final PostRepository postRepository;
public PostResponse selectPost(Long id) {
Post post = postRepository.findById(id).orElseThrow(() -> new IllegalArgumentException());
return PostResponse.from(post);
}
public List<PostResponse> selectPosts() {
List<Post> postList = postRepository.findAll();
return PostResponse.fromList(postList);
}
@Transactional
public PostResponse createPost(PostRequest postRequest) {
Post post = PostRequest.toEntity(postRequest);
return PostResponse.from(postRepository.save(post));
}
@Transactional(rollbackFor = IllegalArgumentException.class)
public PostResponse updatePost(PostRequest postRequest) {
Post post = postRepository.findById(postRequest.getId()).orElseThrow(() -> new IllegalArgumentException());
post.update(postRequest);
return PostResponse.from(post);
}
public Boolean deletePost(Long id) {
Post post = postRepository.findById(id).orElseThrow(() -> new IllegalArgumentException());
postRepository.delete(post);
return true;
}
}