1 | package finki.it.terapijamkbackend.spring.services;
|
---|
2 |
|
---|
3 | import finki.it.terapijamkbackend.spring.dto.CarriedOutInfo;
|
---|
4 | import finki.it.terapijamkbackend.spring.entities.User;
|
---|
5 | import finki.it.terapijamkbackend.spring.exception.UserNotFoundException;
|
---|
6 | import finki.it.terapijamkbackend.spring.repositories.UserRepository;
|
---|
7 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
8 |
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Optional;
|
---|
15 |
|
---|
16 | @Service
|
---|
17 | public class UserService {
|
---|
18 | @Autowired
|
---|
19 | private UserRepository userRepository;
|
---|
20 |
|
---|
21 | public void createUser(String name, String surname, String username, String password, String age, String phone) {
|
---|
22 | User user = new User(name, surname, username, password, age, phone);
|
---|
23 | userRepository.save(user);
|
---|
24 | }
|
---|
25 | public boolean userExists(String username) {
|
---|
26 | return userRepository.existsByUsername(username);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public boolean doesExist(String username,String password){
|
---|
30 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
31 | if(user!=null){
|
---|
32 | return user.getPassword().equals(password);
|
---|
33 | }
|
---|
34 | else return false;
|
---|
35 | }
|
---|
36 | public User getUserByUsername(String username) {
|
---|
37 | return userRepository.findByUsername(username).orElse(null);
|
---|
38 | }
|
---|
39 | public User getUserById(Long userId) {
|
---|
40 | return userRepository.findById(userId).orElse(null);
|
---|
41 | }
|
---|
42 | public User updateUser(String username, User updatedUser) throws UserNotFoundException {
|
---|
43 | Optional<User> existingUserOpt = userRepository.findByUsername(username);
|
---|
44 |
|
---|
45 | if (existingUserOpt.isPresent()) {
|
---|
46 | User existingUser = existingUserOpt.get();
|
---|
47 | existingUser.setName(updatedUser.getName());
|
---|
48 | existingUser.setSurname(updatedUser.getSurname());
|
---|
49 | existingUser.setPhone(updatedUser.getPhone());
|
---|
50 | existingUser.setDateBirth(updatedUser.getDateBirth());
|
---|
51 |
|
---|
52 |
|
---|
53 | if (!existingUser.getUsername().equals(updatedUser.getUsername())) {
|
---|
54 | if (userRepository.existsByUsername(updatedUser.getUsername())) {
|
---|
55 | throw new UserNotFoundException("Username already taken");
|
---|
56 | }
|
---|
57 | existingUser.setUsername(updatedUser.getUsername());
|
---|
58 | }
|
---|
59 |
|
---|
60 | return userRepository.save(existingUser);
|
---|
61 | } else {
|
---|
62 | throw new UserNotFoundException("User not found with username: " + username);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public boolean updateUserTermList(User user,String term,String additionalInfo,String status){
|
---|
67 | user.setAppointmentTerms(term,additionalInfo,status);
|
---|
68 | userRepository.save(user);
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 | public boolean updateUserCarriedOutList(User user,String term,String additionalInfo,String status,String note){
|
---|
72 | user.setCarriedOut(term,additionalInfo,note,status);
|
---|
73 | userRepository.save(user);
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | public List<User> getUsersByFilter(String parameter,String filter){
|
---|
77 | if(parameter.equals("status")){
|
---|
78 | switch(filter){
|
---|
79 | case "all":
|
---|
80 | return userRepository.findAll();
|
---|
81 | case "active":
|
---|
82 | return userRepository.findActive();
|
---|
83 | case "blocked":
|
---|
84 | return userRepository.findBlocked();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | else{
|
---|
88 | switch(parameter){
|
---|
89 | case "name":
|
---|
90 | return userRepository.findByName(filter);
|
---|
91 | case "surname":
|
---|
92 | return userRepository.findBySurname(filter);
|
---|
93 | case "username":
|
---|
94 | Optional opt=userRepository.findByUsername(filter);
|
---|
95 | if(opt.isPresent()){
|
---|
96 | User tempUser= (User) opt.get();
|
---|
97 | List<User>temp= new ArrayList<>();
|
---|
98 | temp.add(tempUser);
|
---|
99 | return temp;
|
---|
100 | }
|
---|
101 | break;
|
---|
102 | case "age":
|
---|
103 | int temp=Integer.parseInt(filter);
|
---|
104 | return userRepository.findByAge(temp);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public boolean isUserBlocked(String username) {
|
---|
111 | Optional<User> user = userRepository.findByUsername(username);
|
---|
112 | return user.map(User::isLocked).orElse(false);
|
---|
113 | }
|
---|
114 | public boolean toggleUserBlockStatus(String username) {
|
---|
115 | Optional<User> user = userRepository.findByUsername(username);
|
---|
116 | if (user.isPresent()) {
|
---|
117 | User u = user.get();
|
---|
118 | u.setLocked(!u.isLocked());
|
---|
119 | userRepository.save(u);
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | public List<CarriedOutInfo> findCarriedOutByUsername(String username) {
|
---|
125 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
126 | return userRepository.findCarriedOutByUserId(user.getId());
|
---|
127 | }
|
---|
128 | public List<Long> getAllUserIds() {
|
---|
129 | return userRepository.findAllUserIds();
|
---|
130 | }
|
---|
131 | public boolean checkDifferentUser(Map<String, String> userData){
|
---|
132 | if(!userRepository.existsByUsername(userData.get("username"))){
|
---|
133 | if(!userRepository.existsByPhone(userData.get("phone").replace("-",""))){
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 | }
|
---|