[57e58a3] | 1 | package com.example.skychasemk.services;
|
---|
| 2 |
|
---|
| 3 | import com.example.skychasemk.dto.ApplicationUserDTO;
|
---|
[9868304] | 4 | import com.example.skychasemk.dto.ApplicationUserLoginDTO;
|
---|
[57e58a3] | 5 | import com.example.skychasemk.model.ApplicationUser;
|
---|
| 6 | import com.example.skychasemk.repository.ApplicationUserRepository;
|
---|
| 7 | import jakarta.transaction.Transactional;
|
---|
[9868304] | 8 | import jakarta.validation.Valid;
|
---|
[57e58a3] | 9 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 10 | import org.springframework.stereotype.Service;
|
---|
| 11 |
|
---|
[9868304] | 12 | import java.util.Optional;
|
---|
[57e58a3] | 13 | //import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | @Service
|
---|
| 17 | public class ApplicationUserService {
|
---|
| 18 |
|
---|
| 19 | @Autowired
|
---|
| 20 | private ApplicationUserRepository userRepository;
|
---|
| 21 |
|
---|
| 22 | //private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
---|
| 23 |
|
---|
| 24 | @Transactional
|
---|
| 25 | public ApplicationUser registerUser(ApplicationUserDTO userDTO) {
|
---|
| 26 | if (userRepository.findByEmail(userDTO.getEmail()).isPresent()) {
|
---|
| 27 | throw new RuntimeException("Email already registered");
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | ApplicationUser user = new ApplicationUser();
|
---|
| 31 | user.setName(userDTO.getName());
|
---|
| 32 | user.setSurname(userDTO.getSurname());
|
---|
| 33 | user.setEmail(userDTO.getEmail());
|
---|
| 34 | user.setPassword(userDTO.getPassword());
|
---|
[9868304] | 35 | user.setPhoneNumber(userDTO.getPhone_number());
|
---|
[57e58a3] | 36 | ApplicationUser savedUser = userRepository.save(user);
|
---|
| 37 | userRepository.flush();
|
---|
| 38 | return savedUser;
|
---|
| 39 | }
|
---|
[9868304] | 40 |
|
---|
| 41 | public ApplicationUser findByEmail(@Valid ApplicationUserLoginDTO userDTO) {
|
---|
| 42 | if (userRepository.findByEmail(userDTO.getEmail()).isEmpty()) {
|
---|
| 43 | throw new RuntimeException("User not registered");
|
---|
| 44 | } else {
|
---|
| 45 | Optional<ApplicationUser> userId = userRepository.findByEmail(userDTO.getEmail());
|
---|
| 46 | return userId.get();
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
[57e58a3] | 49 | } |
---|