April 11, 2024

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

9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rakesh
Rakesh
3 years 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
3 years ago

Hello Sir, I want part 46 onwards videos..

BJ Maxwell
BJ Maxwell
3 years ago
Reply to  Priyanka Patil

Hi, Kidson the Genius.
I am still waiting for the image upload to the database as you promised at the end of Part 46.

Many thanks for all you do.

BJ Maxwell
BJ Maxwell
3 years ago

Hi, Kidson the Genius.
I am still waiting for the image upload to the database as you promised at the end of Part 46.
Many thanks for all you do.

Ertholin
Ertholin
2 years ago

Hi sir. I congratulate you for this tutorial. It’s very interesting and helpful. But, I don’t know if it’s a mistake, I don’t see the 44th part.
And the part where we can upload a photo for the profile.
Thanks for considering my message.

Ertholin
Ertholin
2 years ago

Yes, I have received the email and ask me to subscribe, I already do that. Thanks a lot for your good works.
By the way, I am an Haitian (I suppose you know Haiti). I am a student in Computer Science. Programming is my passion. I want to have your Whatsapp number if you use it. Please, let me know if it will be possible.

naliman
naliman
4 days ago
Awaiting for approval

hello mr kindsonthegenius can you please upload the database here because i didn’t find it into your project in github https://github.com/KindsonTheGenius/fleetapp