March 11, 2021

Complete Application with Spring Boot – Part 6 (Reconcile User Profile and Employee Profile)

In this part, we would see how we can reconcile the User Profile and the Employee Profile.

If you are new to this series:

Start from Part 1 here

Get all Video Tutorials here

 

This is important because, if you are building application that holds employee data, then you remember this rule of thumb. It says:

“not all Users are Employees and not all Employees are Users”

Based on this, we have two separate entities: User and Employee. However, both entities relate in some way. This relationship is based on “Business Rules”. We’ll talk about details of this later. But for now, we’ll used the following rules:

  • an employee could later become a user
  • user profile of employees are created based on their firstname and lastname

Let’s now follows the steps below to reconcile the User profile and Employee profile. The idea is to be able to check if an employee have a user profile. If yes, then update the username field of the employee table.

Step 1: Extend the UserRepository to find a User by firstname and lastname. The method is given below:

User findByFirstnameAndLastname(String firstname, String lastname):

 

Step 2: Write the EmployeeService method to find User by firstname and lastname:

//Set the Username of the employee where firstname and lastname match
public void assignUsername(int id){
	Employee employee = employeeRepository.findById(id).orElse(null);
	User user = userRepository.findByFirstnameAndLastname(
			employee.getFirstname(),
			employee.getLastname());
	employee.setUsername(user.getUsername());
	employeeRepository.save(employee);
}

Step 3: Write the Controller method:

//Assign Employee Username
@RequestMapping(value = "/employees/assignUsername")
public  String assignUsername(int id){
	employeeService.assignUsername(id);
	return "redirect:/employees";
}

We now want to adjust the employee html table in this way: if the employee already have a user profile, then the username is displayed, else the word check is displayed. This would be a link such that if clicked would updated the employee’s username. See figure below:

Check if employee has a use profile

 

Step 4: In the html table, add one more column in the table header. Then add the column in the table body as well:

<td><a th:text="${employee.username} ?: 'Check'" th:href="@{/employees/assignUsername/(id=${employee.id})}"></a></td>

At this point, you can run the application to ensure everything works well.

 

Solving the Edit Issue

This issue arise from the fact that the username is not available in the edit form. Therefore, when the form is edited, null value is assigned to the username. Also note that the thumbnail disappears as well since the thumbnails image is named as the username.

One way to solve this issue is to add the username field in the edit form. Possibly as a hidden field since we don’t want users to edit it. Or maybe a readonly field. I’ll leave this as a homework for you to do.

Html markup:

 <input type="hidden" id="txtUsernameEdit" name="username">

JavaScripts code:

$('#txtUsernameEdit').val(employee.username);

Let me know if you have issues

5 1 vote
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rakesh
Rakesh
6 months ago

Hello Sir, I Liked this Tutoril Very much.
I Want this complete Course Code,
Could you please help me to pass the github link and make it public for me.

Thanks In Advance

Priyanka Patil
Priyanka Patil
2 months ago

Hello Sir, I want part 46 onwards videos..