[2a5d6a3] | 1 | package com.wediscussmovies.project.service.impl;
|
---|
| 2 |
|
---|
[7bc8942] | 3 | import com.wediscussmovies.project.model.PasswordEncoder;
|
---|
[2a5d6a3] | 4 | import com.wediscussmovies.project.model.User;
|
---|
| 5 | import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
|
---|
| 6 | import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
|
---|
| 7 | import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
|
---|
| 8 | import com.wediscussmovies.project.repository.UserRepository;
|
---|
| 9 | import com.wediscussmovies.project.service.UserService;
|
---|
| 10 | import org.springframework.http.HttpRequest;
|
---|
| 11 | import org.springframework.stereotype.Service;
|
---|
| 12 |
|
---|
| 13 | import javax.servlet.http.HttpServletRequest;
|
---|
[7bc8942] | 14 | import java.security.NoSuchAlgorithmException;
|
---|
[2a5d6a3] | 15 | import java.util.Optional;
|
---|
| 16 |
|
---|
| 17 | @Service
|
---|
| 18 | public class UserServiceImpl implements UserService {
|
---|
| 19 | private final UserRepository userRepository;
|
---|
| 20 |
|
---|
| 21 | public UserServiceImpl(UserRepository userRepository) {
|
---|
| 22 | this.userRepository = userRepository;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | @Override
|
---|
| 26 | public Optional<User> login(String email, String password) {
|
---|
| 27 | Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
|
---|
| 28 | Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
|
---|
| 29 | if(userEmail.isPresent())
|
---|
| 30 | return userEmail;
|
---|
| 31 | else if(userUsername.isPresent())
|
---|
| 32 | return userUsername;
|
---|
| 33 | else
|
---|
| 34 | return Optional.empty();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @Override
|
---|
| 38 | public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
|
---|
| 39 | String username, String name, String surname) {
|
---|
| 40 | if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
|
---|
| 41 | confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
|
---|
| 42 | name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
|
---|
| 43 | request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
|
---|
| 44 | return Optional.empty();
|
---|
| 45 | }
|
---|
[7bc8942] | 46 | if(userRepository.findByUsername(username).isPresent()){
|
---|
[2a5d6a3] | 47 | request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
|
---|
| 48 | return Optional.empty();
|
---|
| 49 | }
|
---|
| 50 | if(userRepository.findByEmail(email).isPresent()){
|
---|
| 51 | request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
|
---|
| 52 | return Optional.empty();
|
---|
| 53 | }
|
---|
| 54 | if(!password.equals(confirmPassword)){
|
---|
| 55 | request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
|
---|
| 56 | return Optional.empty();
|
---|
| 57 | }
|
---|
| 58 | User user = new User(email, username, password, name, surname);
|
---|
| 59 | return Optional.of(userRepository.save(user));
|
---|
| 60 | }
|
---|
| 61 | }
|
---|