forked from alexeevna/Medical-Web-App
-
Notifications
You must be signed in to change notification settings - Fork 8
/
UserService.java
72 lines (56 loc) · 2.22 KB
/
UserService.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
package com.app.medicalwebapp.services;
import com.app.medicalwebapp.model.User;
import com.app.medicalwebapp.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> getAll() {
List<User> us = null;
try {
us = userRepository.findByRoleNotLikeOrderByInitialsAsc("Модератор");
} catch (Exception e) {
e.printStackTrace();
}
return us;
}
public Optional<User> getOneByUsername(String username) {
return userRepository.findByUsernameAndRoleNotLike(username, "Модератор");
}
public Optional<User> getOneByUsernameAndRole(String username, String role) {
return userRepository.findByUsernameAndRole(username, role);
}
public void uploadUserAvatar(byte[] bytes, long id) {
Optional<User> userOptional = userRepository.findById(id);
if (userOptional.isPresent()) {
User user = userOptional.get();
user.setAvatar(bytes);
userRepository.save(user);
}
}
public Optional<User> getByUsername(String username) {
return userRepository.findByUsername(username);
}
public List<User> getByInitials(String initials) {
return userRepository.findByInitialsContainingAndRoleNotLikeOrderByInitialsAscAllIgnoreCase(initials, "Модератор");
}
public List<User> getAllByRole(String role) {
return userRepository.findByRoleOrderByInitialsAsc(role);
}
public List<User> getByInitialsAndRole(String initials, String role) {
return userRepository.findByInitialsContainingAndRoleOrderByInitialsAscAllIgnoreCase(initials, role);
}
public User getById(Long id) {
return userRepository.getById(id);
}
public User save(User user) {
return userRepository.save(user);
}
}