Added swagger and DataEnvelope
This commit is contained in:
Родитель
17d62447b6
Коммит
7169a4a4b3
|
@ -3,6 +3,8 @@
|
|||
@inject NavigationManager NavigationManager
|
||||
@layout DashboardCardLayout
|
||||
|
||||
<!-- Demo CRUD -->
|
||||
|
||||
<CardContainer Title="Manage Products">
|
||||
<TelerikGrid Data="Products" Groupable="true" EditMode="GridEditMode.Popup"
|
||||
Sortable="true" Pageable="true" PageSize="10"
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
@page "/"
|
||||
@inject HttpClient Http
|
||||
|
||||
<!-- Demo 1: Remote Sort, Filter, and Paging -->
|
||||
|
||||
<CardContainer Title="Sales">
|
||||
<TelerikGrid @ref="Grid" Height="500px" FilterMode="@GridFilterMode.FilterMenu"
|
||||
Sortable="true" Pageable="true" PageSize="10"
|
||||
OnStateInit="@((GridStateEventArgs<Sale> args) => OnStateInit(args))"
|
||||
Data=@Model.Data TotalCount=@Model.Total OnRead=@ReadItems>
|
||||
Data=@Model.CurrentPageData TotalCount=@Model.TotalItemCount OnRead=@ReadItems>
|
||||
<GridToolBar>
|
||||
<label>Report Range:</label>
|
||||
<TelerikDateRangePicker StartValue="@StartValue" EndValue="@EndValue"
|
||||
|
@ -41,20 +43,14 @@
|
|||
@code {
|
||||
|
||||
#region Grid Data Processing
|
||||
class SalesViewModel
|
||||
{
|
||||
public IEnumerable<Sale> Data { get; set; } = new List<Sale>();
|
||||
public int Total { get; set; } = 0;
|
||||
}
|
||||
|
||||
SalesViewModel Model { get; set; } = new SalesViewModel();
|
||||
DataEnvelope<Sale> Model { get; set; } = new DataEnvelope<Sale>();
|
||||
|
||||
async Task ReadItems(GridReadEventArgs args)
|
||||
{
|
||||
var response = await Http.PostAsJsonAsync("api/sales", args.Request);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
Model = await response.Content.ReadFromJsonAsync<SalesViewModel>();
|
||||
Model = await response.Content.ReadFromJsonAsync<DataEnvelope<Sale>>();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
@using Microsoft.JSInterop
|
||||
@using BlazingCoffee.Client
|
||||
@using BlazingCoffee.Client.Shared
|
||||
@using BlazingCoffee.Shared
|
||||
@using BlazingCoffee.Shared.Models
|
||||
@using Telerik.Blazor.Components
|
||||
@using Telerik.DataSource
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
|
||||
<PackageReference Include="Telerik.Documents.Core" Version="2020.1.109" />
|
||||
<PackageReference Include="Telerik.Documents.Fixed" Version="2020.1.109" />
|
||||
<PackageReference Include="Telerik.Documents.Flow" Version="2020.1.109" />
|
||||
|
|
|
@ -14,11 +14,11 @@ namespace BlazingCoffee.Server
|
|||
}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
// Not for production, show EF query data in the console
|
||||
optionsBuilder.UseLoggerFactory(ConsoleLogger);
|
||||
// Show EF Query parameter values in the console
|
||||
optionsBuilder.EnableSensitiveDataLogging();
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public DbSet<Employee> Employees { get; set; }
|
||||
public DbSet<Team> Teams { get; set; }
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using BlazingCoffee.Shared.Models;
|
||||
using BlazingCoffee.Shared;
|
||||
using BlazingCoffee.Shared.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Telerik.DataSource;
|
||||
|
@ -20,10 +22,16 @@ namespace BlazingCoffee.Server.Controllers
|
|||
|
||||
// GET: api/Sales
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<DataSourceResult>> GetSales([FromBody]DataSourceRequest request)
|
||||
public async Task<ActionResult<DataEnvelope<Sale>>> GetSales([FromBody]DataSourceRequest request)
|
||||
{
|
||||
var result = await _context.Sales.ToDataSourceResultAsync(request);
|
||||
return result;
|
||||
var data = new DataEnvelope<Sale>
|
||||
{
|
||||
CurrentPageData = result.Data.OfType<Sale>().ToList(),
|
||||
TotalItemCount = result.Total
|
||||
|
||||
};
|
||||
return data;
|
||||
}
|
||||
|
||||
// GET: api/Sales/5
|
||||
|
|
|
@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
namespace BlazingCoffee.Server
|
||||
{
|
||||
|
@ -25,6 +26,11 @@ namespace BlazingCoffee.Server
|
|||
services.AddRazorPages();
|
||||
services.AddDbContext<CoffeeContext>(options =>
|
||||
options.UseSqlite("Data Source=Coffee.db"));
|
||||
// Register the Swagger generator, defining 1 or more Swagger documents
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Blazing Coffee API", Version = "v1" });
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
@ -42,6 +48,16 @@ namespace BlazingCoffee.Server
|
|||
app.UseHsts();
|
||||
}
|
||||
|
||||
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
||||
app.UseSwagger();
|
||||
|
||||
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
||||
// specifying the Swagger JSON endpoint.
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Blazing Coffee API");
|
||||
});
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace BlazingCoffee.Shared
|
||||
{
|
||||
/// <summary>
|
||||
/// An evenlope for the DataSourceResult serialization back to the client.
|
||||
/// Needed because the framework cannot otherwise deserialize the IEnumerable collection
|
||||
/// that the Telerik DataSource package produces - it requires an explicit type -
|
||||
/// System.Text.Json cannot successfully deserialize interface properties.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The model type that is sent from the server</typeparam>
|
||||
public class DataEnvelope<T>
|
||||
{
|
||||
public List<T> CurrentPageData { get; set; }
|
||||
public int TotalItemCount { get; set; }
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче