@Service
@Transactional
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final BCryptPasswordEncoder passwordEncoder;
private final JwtUtil jwtUtil;
private final ModelMapper mapper;
@Autowired
public UserServiceImpl(UserRepository userRepository,
BCryptPasswordEncoder passwordEncoder,
JwtUtil jwtUtil,
ModelMapper mapper) {
[Link] = userRepository;
[Link] = passwordEncoder;
[Link] = jwtUtil;
[Link] = mapper;
}
@Override
public String signUp(SignUpDto sdtorequest) {
if ([Link]([Link]()))
return "Email already registered";
User user = [Link](sdtorequest, [Link]);
[Link]([Link]([Link]()));
[Link](user);
return "Signup successful";
}
@Override
public LoginResposneDTO login(LoginRequestDTO loginRequest) {
User user = [Link]([Link]())
.orElseThrow(() -> new RuntimeException("Invalid
credentials"));
if (, [Link]()))
throw new RuntimeException("Invalid credentials");
String token = [Link]([Link](),
[Link]().name(), [Link]());
return new LoginResposneDTO([Link](), token, [Link]());
}
@Override
public String Forgetpassword(ForgetpasswordDto forgetpasswordDto) {
User user = [Link]([Link]())
.orElseThrow(() -> new RuntimeException("Email not
registered"));
[Link]([Link]([Link]()));
[Link]("YES");
[Link](user);
return "Password updated successfully";
}
}
---------------------------------------------------------------------------
Controller → DTO → Service Interface → ImplService → Entity → Repository → Database
Controller → Receives HTTP requests
DTO → Maps request/response data
Service → Defines “what to do”
ImplService → Implements “how to do” (logic + DB + JWT)
Entity → Maps table in DB
Repository → Handles DB operations
--------------------------------------------------------------------------
🚀 1. Client → Controller Layer
What happens:
User sends API request from Postman, Web, or Mobile App.
Request hits the Controller class first.
Controller validates input using @Valid.
Example:
POST /auth/signup
Data → { name, email, phno, pwd, role }
Controller job:
Accept request body into DTO
Call the required service method
🚀 2. Controller → DTO Layer
What happens:
Request data is mapped into a DTO object
SignUpDto
LoginRequestDTO
ForgetpasswordDto
Purpose of DTO:
Carries data from UI → Backend
Prevents exposing Entity (database structure)
Performs input validation (@NotBlank, @Email, etc.)
🚀 3. DTO → Service Layer
What happens:
Controller calls methods from UserService interface:
signUp(SignUpDto dto)
login(LoginRequestDTO dto)
Forgetpassword(ForgetpasswordDto dto)
Purpose of Service:
Only defines what needs to be done
No business logic here
Works as a contract between controller and service implementation
🚀 4. Service → ImplService (Business Logic Layer)
What happens:
The actual business logic executes here.
SignUp flow in ImplService:
Check if email already exists
Encrypt password
Convert DTO → Entity
Save user to DB
Return success message
Login flow:
Fetch user by email
Compare password (BCrypt)
Generate JWT token
Return response DTO
Forget Password flow:
Find user by email
Encrypt new password
Update entity
Save to DB
This layer is the brain of your application.
🚀 5. ImplService → Entity Layer
What happens:
User entity is created/updated based on DTO data
Entity maps to the database table
Example:
[Link] → users_tbl
Entity includes:
Columns
Constraints
Timestamps
Role enum
It represents the actual database row structure.
🚀 6. Entity → Repository Layer
What happens:
ImplService interacts with Repository to perform DB operations.
Repository functions used:
save(entity)
findByEmail(email)
existsByEmail(email)
Repository sends queries to DB automatically using JPA.
You do not write SQL because Spring Data handles it.
🚀 7. Repository → Database
What happens:
Database actions occur:
✔ Insert new user
✔ Update password
✔ Search user by email
✔ Validate credentials
Database stores:
userId
name, email, phno
encrypted pwd
role
created/updated date
🚀 8. Database → Back to Controller
Repository returns data to ImplService → Service → Controller
Controller sends a clean response to the client
Examples:
"Signup Successful"
{ userId, role, token }
"Password Updated"
-------------------------------------------------------------------------------
⚡ INTERVIEW-FRIENDLY EXPLANATION
“Our project follows a layered architecture.
When a user sends a request, it first hits the Controller, which validates and
converts data into DTO.
The DTO is then passed to the Service layer, which defines the contract.
The actual logic resides in the ImplService where DTO is mapped to Entity and
repository methods are used to interact with the database.
The Repository layer handles all CRUD operations using JPA.
Finally, the response returns back to the controller and then to the client.”