source: src/main/java/com/example/skychasemk/services/ApplicationUserService.java@ de83113

Last change on this file since de83113 was 9868304, checked in by ste08 <sjovanoska@…>, 4 months ago

Frontend + some backend changes

  • Property mode set to 100644
File size: 1.8 KB
Line 
1package com.example.skychasemk.services;
2
3import com.example.skychasemk.dto.ApplicationUserDTO;
4import com.example.skychasemk.dto.ApplicationUserLoginDTO;
5import com.example.skychasemk.model.ApplicationUser;
6import com.example.skychasemk.repository.ApplicationUserRepository;
7import jakarta.transaction.Transactional;
8import jakarta.validation.Valid;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.stereotype.Service;
11
12import java.util.Optional;
13//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
14
15
16@Service
17public 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}
Note: See TracBrowser for help on using the repository browser.