ProfileService.java
1.46 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
package com.srh.api.service;
import com.srh.api.model.Profile;
import com.srh.api.repository.ProfileRepository;
import org.hibernate.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Service
public class ProfileService {
@Autowired
private ProfileRepository profileRepository;
public Profile find(Integer id) {
Optional<Profile> profile = profileRepository.findById(id);
if (profile.isPresent())
return profile.get();
throw new ObjectNotFoundException(id, Profile.class.getName());
}
public Page<Profile> findAll(Pageable pageInfo) {
return profileRepository.findAll(pageInfo);
}
public Profile save(Profile profile) {
return profileRepository.save(profile);
}
public Profile update(Profile profile) {
find(profile.getId());
return profileRepository.save(profile);
}
public void delete(Integer id) {
find(id);
profileRepository.deleteById(id);
}
public List<Profile> getProfilesByAuthority(boolean isAdmin) {
if (isAdmin) {
return (List<Profile>) profileRepository.findAll();
}
return Collections.singletonList(find(2));
}
}