-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserService.java
113 lines (92 loc) · 3.68 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.CodingDojo.LognadReg.Service;
import java.util.List;
import java.util.Optional;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;
import com.CodingDojo.LognadReg.Models.Occupation;
import com.CodingDojo.LognadReg.Models.RateReview;
import com.CodingDojo.LognadReg.Models.User;
import com.CodingDojo.LognadReg.Repositories.OccupationRepository;
import com.CodingDojo.LognadReg.Repositories.RateReviewRepository;
import com.CodingDojo.LognadReg.Repositories.UserOccupationRepository;
import com.CodingDojo.LognadReg.Repositories.UserRepository;
@Service
public class UserService {
private final UserRepository userRepository;
private final OccupationRepository occupationRepository;
private final UserOccupationRepository useroccupationRepository;
private final RateReviewRepository ratereviewRepository;
public UserService(UserRepository userRepository, OccupationRepository occupationRepository, UserOccupationRepository useroccupationRepository, RateReviewRepository ratereviewRepository) {
this.userRepository = userRepository;
this.occupationRepository = occupationRepository;
this.useroccupationRepository = useroccupationRepository;
this.ratereviewRepository = ratereviewRepository;
}
// register user and hash their password
public User registerUser(User user) {
String hashed = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()); //static line of code
user.setPassword(hashed);
user.setEmail(user.getEmail().toLowerCase());
return userRepository.save(user);
}
// find user by email
public User findByEmail(String email) {
return userRepository.findByEmail(email);
}
// find user by id
public User findUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
// authenticate user
public boolean authenticateUser(String email, String password) {
// first find the user by email
User user = userRepository.findByEmail(email);
// if we can't find it by email, return false
if(user == null) {
return false;
} else {
// if the passwords match, return true, else, return false
if(BCrypt.checkpw(password, user.getPassword())) {
return true;
} else {
return false;
}
}
}
public void updateUser(User user) {
userRepository.save(user);
}
public void deleteOccupation(Long id) {
occupationRepository.deleteById(id);
}
public List<Occupation> allOccupations() {
return (List<Occupation>) occupationRepository.findAll();
}
public Occupation addOccupation(Occupation occupation) {
return occupationRepository.save(occupation);
}
public RateReview addRateReview(RateReview ratereview) {
return ratereviewRepository.save(ratereview);
}
public Occupation findOccupationById(Long id) {
Optional<Occupation> o = occupationRepository.findById(id);
if(o.isPresent()) {
return o.get();
}
else {
return null;
}
}
public void updateOccupation(Occupation occupation) {
occupationRepository.save(occupation);
}
public void updateRateReview(RateReview ratereview) {
ratereviewRepository.save(ratereview);
}
public void newRateReview(RateReview ratereview) {
ratereviewRepository.save(ratereview);
}
public UserOccupationRepository getUseroccupationRepository() {
return useroccupationRepository;
}
}