Browse Source

Merge pull request #16041 from abpframework/Onur/IdentityUser-article-edit

Community Article - IdentityUser relationship
pull/16048/head
Halil İbrahim Kalkan 3 years ago
committed by GitHub
parent
commit
7abed90cb7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 123
      docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/Post.md
  2. BIN
      docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/images/run-dbmigrator.png
  3. BIN
      docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/images/user-department.png
  4. BIN
      docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/images/users-page-department.png

123
docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/Post.md

@ -139,6 +139,59 @@ When you look at your database, you can see that the `Department` table has been
![users-table](images/users-table.png)
> It's good to have some initial data in the database before running the application. This section introduces the [Data Seeding](https://docs.abp.io/en/abp/latest/Data-Seeding) system of the ABP framework. You can skip this section if you don't want to create the data seeding, but it is suggested to follow along and learn this useful ABP Framework feature.
Create a class deriving from the `IDataSeedContributor` in the `IdentityRelationship.Domain` project by copying the following code:
```csharp
using System;
using System.Threading.Tasks;
using IdentityRelationship.Departments;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
namespace IdentityRelationship;
public class IdentityRelationshipDataSeederContributor
: IDataSeedContributor, ITransientDependency
{
private readonly IRepository<Department, Guid> _departmentRepository;
public IdentityRelationshipDataSeederContributor(IRepository<Department, Guid> departmentRepository)
{
_departmentRepository = departmentRepository;
}
public async Task SeedAsync(DataSeedContext context)
{
if (await _departmentRepository.GetCountAsync() <= 0)
{
await _departmentRepository.InsertAsync(
new Department
{
Name = "Human Resources"
},
autoSave: true
);
await _departmentRepository.InsertAsync(
new Department
{
Name = "Production"
},
autoSave: true
);
}
}
}
```
- This code simply uses the `IRepository<Department, Guid>` (the default [repository](https://docs.abp.io/en/abp/latest/Repositories)) to insert two books to the database in case there weren't any books in it.
Run the `IdentityRelationship.DbMigrator` application to update the database:
![run-dbmigrator](images/run-dbmigrator.png)
Again, open the `IdentityRelationshipModuleExtensionConfigurator` class in the Domain.Shared project and add the following code:
```csharp
@ -147,7 +200,7 @@ user.AddOrUpdateProperty<Guid>(
"DepartmentId",
property =>
{
property.UI.Lookup.Url = "/api/app/departments";
property.UI.Lookup.Url = "/api/app/department";
property.UI.Lookup.DisplayPropertyName = "name";
}
);
@ -155,16 +208,22 @@ user.AddOrUpdateProperty<Guid>(
The `UI.Lookup.Url` option takes a URL to get the list of departments to select on the edit/create forms. This endpoint can be a typical controller, an auto API controller or any type of endpoint that returns a proper JSON response.
To localize, open the `IdentityRelationship.Domain.Shared` project and add it to your `/Localization/IdentityRelationship/en.json` file:
```json
"DepartmentId": "Department"
```
Create a `Departments` folder in the `IdentityRelationship.Application.Contracts` project of your solution and add the `DepartmentDto` class in it
```csharp
using System;
using Volo.Abp.Application.Dtos;
namespace IdentityRelationship.Departments;
public class DepartmentDto
{
public Guid Id { get; set; }
public class DepartmentDto : EntityDto<Guid>
{
public string Name { get; set; }
}
```
@ -179,7 +238,7 @@ namespace IdentityRelationship.Departments;
public interface IDepartmentAppService : IApplicationService
{
public Task<ListResultDto<DepartmentDto>> GetAsync();
public Task<PagedResultDto<DepartmentDto>> GetAsync();
}
```
@ -187,34 +246,52 @@ Time to implement the `IDepartmentAppService` interface. Create a `Departments`
```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using IdentityRelationship.Departments;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
namespace IdentityRelationship.Department;
public class DepartmentAppService : IdentityRelationshipAppService, IDepartmentAppService
{
public async Task<ListResultDto<DepartmentDto>> GetAsync()
private readonly IRepository<Departments.Department, Guid> _departmentRepository;
public DepartmentAppService(IRepository<Departments.Department, Guid> departmentRepository)
{
return new ListResultDto<DepartmentDto>(
new[]
{
new DepartmentDto
{
Id = Guid.Parse("6267f0df-870f-4173-be44-d74b4b56d2bd"),
Name = "Human Resources"
},
new DepartmentDto
{
Id = Guid.Parse("21c7b61f-330c-489e-8b8c-80e0a78a5cc5"),
Name = "Production"
}
}
);
_departmentRepository = departmentRepository;
}
public async Task<PagedResultDto<DepartmentDto>> GetAsync()
{
var departments = await _departmentRepository.GetListAsync();
var totalCount = await _departmentRepository.GetCountAsync();
return new PagedResultDto<DepartmentDto>(totalCount,
ObjectMapper.Map<List<Departments.Department>, List<DepartmentDto>>(departments));
}
}
```
`DepartmentAppService `is using the `ObjectMapper` to convert the `Department` objects to `DepartmentDto` objects. So, we need to define this mapping in the AutoMapper configuration.
Open the `IdentityRelationshipApplicationAutoMapperProfile` class inside the `IdentityRelationship.Application` project and add the following line to the constructor:
```csharp
using AutoMapper;
using IdentityRelationship.Departments;
namespace IdentityRelationship;
public class IdentityRelationshipApplicationAutoMapperProfile : Profile
{
public IdentityRelationshipApplicationAutoMapperProfile()
{
CreateMap<Departments.Department, DepartmentDto>();
}
}
```
Run your `IdentityRelationship.Web` project and add a department to one of your users.
![user-department-image](images/user-department.png)
@ -224,9 +301,9 @@ And it shows the department name on the data table:
![users-page-department-image](images/users-page-department.png)
## Conclusion
In this article I talked about the IdentityUser's relationships. Thank you for reading the article, I hope it was useful. See you soon!
In this article I talked about the use of the IdentityUser relationship and how to extend it. Thank you for reading the article, I hope it was useful. See you soon!
## References
- https://docs.abp.io/en/abp/latest/Module-Entity-Extensions
- https://learn.microsoft.com/en-us/ef/core/modeling/relationships
- https://community.abp.io/posts/how-to-add-custom-properties-to-the-user-entity-rixchoha
- https://community.abp.io/posts/how-to-add-custom-properties-to-the-user-entity-rixchoha

BIN
docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/images/run-dbmigrator.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

BIN
docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/images/user-department.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 51 KiB

BIN
docs/en/Community-Articles/2023-03-20- IdentityUser-Relationships/images/users-page-department.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 178 KiB

After

Width:  |  Height:  |  Size: 160 KiB

Loading…
Cancel
Save