-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostService.java
84 lines (70 loc) · 3.06 KB
/
PostService.java
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
package com.example.api.service;
import com.example.api.dto.PostRequestDto;
import com.example.api.dto.PostResponseDto;
import com.example.api.entity.PostEntity;
import com.example.api.entity.UserEntity;
import com.example.api.exception.AttributeIsNotPresentedException;
import com.example.api.exception.ItemNotFoundException;
import com.example.api.mapper.PostMapper;
import com.example.api.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import java.time.Instant;
import java.util.List;
@Service
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
private final PostMapper mapper;
private final UserService userService;
private final Sort idAscSort = Sort.by(Sort.Direction.ASC, "id");
public List<PostResponseDto> findAll(int page, int count) {
return mapper.fromEntities(
postRepository.findAll(PageRequest.of(page, count, idAscSort)).getContent()
);
}
public PostResponseDto findById(Long id) {
return mapper.fromEntity(
postRepository.findById(id).orElseThrow(ItemNotFoundException::new)
);
}
public PostResponseDto save(PostRequestDto postRequestDto, UserDetails principal) {
UserEntity userEntity = userService.findByUsername(principal.getUsername());
PostEntity postEntity = mapper.fromDto(postRequestDto);
postEntity.setAuthor(userEntity);
return mapper.fromEntity(postRepository.save(postEntity));
}
public PostResponseDto update(PostRequestDto postRequestDto) {
if (postRequestDto.getId() == null) {
throw new AttributeIsNotPresentedException("id");
}
PostEntity post = mapper.fromDto(postRequestDto);
post.setUpdated(Instant.now());
return mapper.fromEntity(postRepository.save(post));
}
public PostResponseDto delete(PostRequestDto postRequestDto) {
if (postRequestDto.getId() == null) {
throw new AttributeIsNotPresentedException("id");
}
PostEntity post = postRepository.findById(mapper.fromDto(postRequestDto).getId())
.orElseThrow(ItemNotFoundException::new);
post.setDeleted(Instant.now());
post.setDeletedBy(userService.getCurrentUserEntity());
return mapper.fromEntity(postRepository.save(post));
}
public PostResponseDto restore(Long id) {
if (id == null) {
throw new AttributeIsNotPresentedException("id");
}
PostEntity post = postRepository.findById(id).orElseThrow(ItemNotFoundException::new);
post.setDeleted(null);
return mapper.fromEntity(postRepository.save(post));
}
public boolean existByAuthorAndId(UserEntity author, Long id) {
return postRepository.existsByAuthorAndId(author, id);
}
}