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