-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostService.java
More file actions
46 lines (36 loc) · 1.28 KB
/
PostService.java
File metadata and controls
46 lines (36 loc) · 1.28 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
package com.example.devSns.service;
import com.example.devSns.entity.PostEntity;
import com.example.devSns.repository.PostRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true)
public class PostService {
private final PostRepository postRepository;
// 생성자
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
public List<PostEntity> getAllPosts() {
return postRepository.findAll();
}
public PostEntity getPost(Long id) {
return postRepository.findById(id).orElseThrow();
}
@Transactional
public PostEntity createPost(PostEntity postEntity) {
return postRepository.save(postEntity);
}
@Transactional
public PostEntity updatePost(Long id, PostEntity updated) {
PostEntity postEntity = postRepository.findById(id).orElseThrow();
postEntity.setTitle(updated.getTitle());
postEntity.setContent(updated.getContent());
return postRepository.save(postEntity);
}
@Transactional
public void deletePost(Long id) {
postRepository.deleteById(id);
}
}