-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostService.java
More file actions
44 lines (33 loc) · 1.14 KB
/
PostService.java
File metadata and controls
44 lines (33 loc) · 1.14 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
package com.example.devSns.service;
import com.example.devSns.entity.Post;
import com.example.devSns.repository.PostRepository;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Service
public class PostService {
private final PostRepository postRepository;
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
public List<Post> findAll(){
return postRepository.findAll();
}
public Optional<Post> findById(Long id){
return postRepository.findById(id);
}
public Post save(Post post){
return postRepository.save(post);
}
public Post updatePost(Long id, Post updatedPost) {
Post existingPost = postRepository.findById(id).orElseThrow(() -> new RuntimeException("Post not found"));
updatedPost.setCreatedAt(existingPost.getCreatedAt());
updatedPost.setUpdatedAt(LocalDateTime.now());
updatedPost.setId(id);
return postRepository.save(updatedPost);
}
public void delete(Long id){
postRepository.deleteById(id);
}
}