0% found this document useful (0 votes)
21 views1 page

Zuru 1

This document defines a UserService class that handles user registration. The class uses a UserRepository and PasswordEncoder to save new user objects to the database after encoding the password.

Uploaded by

jonathanmaithya9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Zuru 1

This document defines a UserService class that handles user registration. The class uses a UserRepository and PasswordEncoder to save new user objects to the database after encoding the password.

Uploaded by

jonathanmaithya9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package com.Jocode.zururestaurants.

service;

import com.Jocode.zururestaurants.DTO.RegistrationRequest;
import com.Jocode.zururestaurants.model.User;
import com.Jocode.zururestaurants.repository.UserRepository;
import com.mysql.cj.protocol.a.PacketSplitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService {

private final UserRepository userRepository;


private final PasswordEncoder passwordEncoder; // Assume you have a
PasswordEncoder bean configured

@Autowired
public UserService(UserRepository userRepository, PasswordEncoder
passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@Bean
public UserService userService(){
return new UserService(UserRepository userRepository, PasswordEncoder
passwordEncoder);
}

public boolean registerUser(RegistrationRequest registrationRequest) {


String email = registrationRequest.getEmail();
if (userRepository.existsByEmail(email)) {
// User already exists
return false;
}

User newUser = new User();


newUser.setUsername(registrationRequest.getUsername());
newUser.setEmail(email);

newUser.setPassword(passwordEncoder.encode(registrationRequest.getPassword()));
// Map other fields if necessary

// Persist the user entity


User savedUser = userRepository.save(newUser);

// Assuming save is successful if savedUser is not null


return savedUser != null;
}
}

You might also like