This repository was archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
54 lines (38 loc) · 1.76 KB
/
Copy pathUserService.java
File metadata and controls
54 lines (38 loc) · 1.76 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 pt.ua.deti.codespell.codespellbackend.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import pt.ua.deti.codespell.codespellbackend.exception.implementations.ExistentUserException;
import pt.ua.deti.codespell.codespellbackend.exception.implementations.UserNotFoundException;
import pt.ua.deti.codespell.codespellbackend.model.User;
import pt.ua.deti.codespell.codespellbackend.repository.UserRepository;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@NonNull
public User findByUsername(String username) {
if (!userRepository.existsByUsername(username))
throw new UserNotFoundException(String.format("The user %s could not be found.", username));
return userRepository.findByUsername(username);
}
@NonNull
public User findByEmail(String email) {
if (!userRepository.existsByEmail(email))
throw new UserNotFoundException(String.format("The user %s could not be found.", email));
return userRepository.findByEmail(email);
}
public void registerUser(User user) {
if (userRepository.existsByUsername(user.getUsername()))
throw new ExistentUserException("The provided username is already taken.");
userRepository.save(user);
}
public void updateUser(User user) {
if (!userRepository.existsByUsername(user.getUsername()))
throw new UserNotFoundException(String.format("The user %s could not be found.", user.getUsername()));
userRepository.save(user);
}
}