Knowledge Base

Snippets

26

1. Add Repository Method
Project: Server
File: Server\Repository\BusinessCompanyRepository.cs
Add this method:
public async Task<List<BusinessCompany>> GetBusinessCompaniesByIsNewItemAsync(int moduleId)
{
    using var db = _factory.CreateDbContext();
    return await db.BusinessCompany
        .Where(c => c.ModuleId == moduleId && c.IsNewItem)
        .OrderBy(c => c.SortOrder)
        .AsNoTracking()
        .ToListAsync();
}

 

2. Add to Repository Interface
Project: Server
File: Server\Repository\IBusinessCompanyRepository.cs
Add:

Task<List<BusinessCompany>> GetBusinessCompaniesByIsNewItemAsync(int moduleId);

 

3. Add to Shared Service Interface
Project: Shared
File: Shared\Interfaces\IBusinessCompanyService.cs
Add:
Task<List<Models.BusinessCompany>> GetBusinessCompaniesByIsNewItemAsync(int moduleId);

 

4. Add to Server Service Implementation
Project: Server
File: Server\Services\BusinessCompanyService.cs
Add:
public async Task<List<BusinessCompany>> GetBusinessCompaniesByIsNewItemAsync(int moduleId)
{
    return await _repository.GetBusinessCompaniesByIsNewItemAsync(moduleId);
}

 

5. Add API Endpoint
Project: Server
File: Server\Controllers\BusinessCompanyController.cs
Add:
[HttpGet("isnew/{moduleId}")]
public async Task<ActionResult<List<BusinessCompany>>> GetBusinessCompaniesByIsNewItem(int moduleId)
{
    var companies = await _businessCompanyRepository.GetBusinessCompaniesByIsNewItemAsync(moduleId);
    return Ok(companies);
}

 

6. Add to Client Service
Project: Client
File: Client\Services\BusinessCompanyService.cs
Add:
public async Task<List<Models.BusinessCompany>> GetBusinessCompaniesByIsNewItemAsync(int moduleId)
{
    return await GetJsonAsync<List<Models.BusinessCompany>>(
        CreateAuthorizationPolicyUrl($"{Apiurl}/isnew/{moduleId}", EntityNames.Module, moduleId),
        Enumerable.Empty<Models.BusinessCompany>().ToList());
}

 

7. Use in Index.razor
Project: Client
File: Client\Modules\GIBS.Module.BusinessDirectory\Index.razor
a. In your @code block, load the list if no type is selected:
 

private List<BusinessCompany> _newCompanies;

protected override async Task OnInitializedAsync()
{
    try
    {
        ParentItems = BuildTree(await BusinessDirectoryService.GetBusinessDirectorysAsync(ModuleState.ModuleId));
        _BusinessDirectorys = await BusinessDirectoryService.GetBusinessDirectorysAsync(ModuleState.ModuleId);

        if (PageState.QueryString.ContainsKey("typeId") && int.TryParse(PageState.QueryString["typeId"], out var typeId))
        {
            _selectedTypeId = typeId;
            _companies = await BusinessCompanyService.GetBusinessCompaniesByTypeAsync(typeId, ModuleState.ModuleId);
        }
        else
        {
            _selectedTypeId = null;
            _companies = null;
            _newCompanies = await BusinessCompanyService.GetBusinessCompaniesByIsNewItemAsync(ModuleState.ModuleId);
        }
    }
    catch (Exception ex)
    {
        await logger.LogError(ex, "Error Loading Business Directory Type {Error}", ex.Message);
        AddModuleMessage(Localizer["Message.LoadError"], MessageType.Error);
    }
}

b. In your markup, display the cards when no type is selected:

@if (!_selectedTypeId.HasValue)
{
    <div class="row">
        @if (_newCompanies == null)
        {
            <div class="col-12"><em>@Localizer["LoadingCompanies"]</em></div>
        }
        else if (_newCompanies.Count == 0)
        {
            <div class="col-12">@Localizer["NoCompaniesFound"]</div>
        }
        else
        {
            @foreach (var company in _newCompanies)
            {
                <div class="col-md-4 mb-3">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">@company.CompanyName</h5>
                            @if (!string.IsNullOrWhiteSpace(company.Address))
                            {
                                <div class="card-text">@company.Address, @company.City, @company.State @company.ZipCode</div>
                            }
                            @if (!string.IsNullOrWhiteSpace(company.Phone))
                            {
                                <div class="card-text">@company.Phone</div>
                            }
                            @if (!string.IsNullOrWhiteSpace(company.Website))
                            {
                                <div class="card-text"><a href="@company.Website" target="_blank">@company.Website</a></div>
                            }
                        </div>
                    </div>
                </div>
            }
        }
    </div>
}

Posted in: Oqtane

Post Rating

Comments

There are currently no comments, be the first to post one!

Post Comment

Only registered users may post comments.