1 | package com.example.skychasemk.services;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.dto.ApplicationUserDTO;
|
---|
4 | import com.example.skychasemk.dto.ApplicationUserLoginDTO;
|
---|
5 | import com.example.skychasemk.model.ApplicationUser;
|
---|
6 | import com.example.skychasemk.repository.ApplicationUserRepository;
|
---|
7 | import jakarta.transaction.Transactional;
|
---|
8 | import jakarta.validation.Valid;
|
---|
9 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.util.Optional;
|
---|
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());
|
---|
35 | user.setPhoneNumber(userDTO.getPhone_number());
|
---|
36 | ApplicationUser savedUser = userRepository.save(user);
|
---|
37 | userRepository.flush();
|
---|
38 | return savedUser;
|
---|
39 | }
|
---|
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 | }
|
---|
49 | } |
---|