title
stringlengths
3
46
content
stringlengths
0
1.6k
21:1828
We are finally ready to define our PackagesController:
21:1829
using Microsoft.AspNetCore.Mvc;
21:1830
using PackagesManagementBlazor.Server.Queries;
21:1831
using PackagesManagementBlazor.Shared;
21:1832
using System.Threading.Tasks;
21:1833
namespace PackagesManagementBlazor.Server.Controllers
21:1834
{
21:1835
[Route(`[controller]`)]
21:1836
[ApiController]
21:1837
public class PackagesController : ControllerBase
21:1838
{
21:1839
// GET api/<PackagesController>/Flor
21:1840
[HttpGet(`{location}`)]
21:1841
public async Task<PackagesListViewModel>
21:1842
GetAsync(string location,
21:1843
[FromServices] IPackagesListByLocationQuery query )
21:1844
{
21:1845
return new PackagesListViewModel
21:1846
{
21:1847
Items = await query.GetPackagesOf(location)
21:1848
};
21:1849
}
21:1850
}
21:1851
}
21:1852
21:1853
The server-side code is finished! Let’s move on to the definition of the Blazor service that communicates with the server.
21:1854
Implementing the business logic in a service
21:1855
Let’s add a ViewModels and a Services folder to the PackagesManagementBlazor.Client project. Most of the ViewModels we need were defined in the PackagesManagementBlazor.Shared project. We only need a ViewModel for the search form. Let’s add it to the ViewModels folder:
21:1856
using System.ComponentModel.DataAnnotations;
21:1857
namespace PackagesManagementBlazor.Client.ViewModels
21:1858
{
21:1859
public class SearchViewModel
21:1860
{
21:1861
[Required]
21:1862
public string? Location { get; set; }
21:1863
}
21:1864
}
21:1865
21:1866
Let’s call our service PackagesClient, and let’s add it to the Services folder:
21:1867
namespace PackagesManagementBlazor.Client.Services
21:1868
{
21:1869
public class PackagesClient
21:1870
{
21:1871
private HttpClient client;
21:1872
public PackagesClient(HttpClient client)
21:1873
{
21:1874
this.client = client;
21:1875
}
21:1876
public async Task<IEnumerable<PackageInfosViewModel>>
21:1877
GetByLocationAsync(string location)
21:1878
{
21:1879
var result =
21:1880
await client.GetFromJsonAsync<PackagesListViewModel>
21:1881
(`Packages/` + Uri.EscapeDataString(location));
21:1882
return result.Items;
21:1883
}
21:1884
}
21:1885
}
21:1886
21:1887
The code is straightforward! The Uri.EscapeDataString method URL-encodes the parameter so it can be safely appended to the URL.
21:1888
Finally, let’s register the service in the dependency injection:
21:1889
builder.Services.AddScoped<PackagesClient>();
21:1890
21:1891
It is worth pointing out that in a commercial application, we should have registered the service through an IPackagesClient interface in order to be able to mock it in the tests (.AddScoped<IPackagesClient, PackagesClient>()).
21:1892
With everything in place, we just need to build the UI.
21:1893
Implementing the user interface
21:1894
As the first step, let’s delete the application pages we don’t need – namely, Pages->Counter.razor and Pages->Weather.razor. Let’s also remove their links from the side menu in Shared -> NavMenu.razor.
21:1895
We will put our code in the Pages -> Home.razor page. Let’s replace the code of this page with the following:
21:1896
@using PackagesManagementBlazor.Client.ViewModels
21:1897
@using PackagesManagementBlazor.Shared
21:1898
@using PackagesManagementBlazor.Client.Services
21:1899
@inject PackagesClient client
21:1900
@page `/`
21:1901
<h1>Search packages by location</h1>
21:1902
<EditForm Model=`search`
21:1903
OnValidSubmit=`Search`>
21:1904
<DataAnnotationsValidator />
21:1905
<div class=`form-group`>
21:1906
<label for=`integerfixed`>Insert location starting chars</label>
21:1907
<InputText @bind-Value=`search.Location` />
21:1908
<ValidationMessage For=`@(() => search.Location)` />
21:1909
</div>
21:1910
<button type=`submit` class=`btn btn-primary`>
21:1911
Search
21:1912
</button>
21:1913
</EditForm>
21:1914
@code{
21:1915
SearchViewModel search { get; set; } = new SearchViewModel();
21:1916
async Task Search()
21:1917
{
21:1918
...
21:1919
}
21:1920
}
21:1921
21:1922
The preceding code adds the needed @using statements, injects our PackagesClient service into the page, and defines the search form. When the form is successfully submitted, it invokes the Search callback, where we will place the code that retrieves all the results.
21:1923
It is time to add the logic to display all the results and to complete the @code block. The following code must be placed immediately after the search form:
21:1924
@if (packages != null)
21:1925
{
21:1926
...
21:1927
}