Skip to content

Commit

Permalink
feat(templates): simplify boilerplate's http client related codes #6811
Browse files Browse the repository at this point in the history
… (#6812)
  • Loading branch information
ysmoradi authored Feb 8, 2024
1 parent eacdfde commit cbda8ef
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public async Task<OverallAnalyticsStatsDataResponseDto> GetOverallAnalyticsStats
}

[HttpGet]
public async Task<IAsyncEnumerable<ProductsCountPerCategoryResponseDto>> GetProductsCountPerCategoryStats(CancellationToken cancellationToken)
public IQueryable<ProductsCountPerCategoryResponseDto> GetProductsCountPerCategoryStats()
{
return DbContext.Categories
.Select(c => new ProductsCountPerCategoryResponseDto()
{
CategoryName = c.Name,
CategoryColor = c.Color,
ProductCount = c.Products!.Count()
}).AsAsyncEnumerable();
});
}

[HttpGet]
public async Task<IAsyncEnumerable<ProductSaleStatResponseDto>> GetProductsSalesStats(CancellationToken cancellationToken)
public IQueryable<ProductSaleStatResponseDto> GetProductsSalesStats()
{
Random rand = new Random();
return DbContext.Products.Include(p => p.Category)
Expand All @@ -43,12 +43,12 @@ public async Task<IAsyncEnumerable<ProductSaleStatResponseDto>> GetProductsSales
ProductName = p.Name,
CategoryColor = p.Category!.Color,
SaleAmount = rand.Next(1, 10) * p.Price
}).AsAsyncEnumerable();
});
}


[HttpGet]
public async Task<ProductPercentagePerCategoryResponseDto[]> GetProductsPercentagePerCategoryStats(CancellationToken cancellationToken)
public async Task<List<ProductPercentagePerCategoryResponseDto>> GetProductsPercentagePerCategoryStats(CancellationToken cancellationToken)
{
var productsTotalCount = await DbContext.Products.CountAsync(cancellationToken);

Expand All @@ -63,6 +63,6 @@ public async Task<ProductPercentagePerCategoryResponseDto[]> GetProductsPercenta
CategoryName = c!.Name,
CategoryColor = c.Color,
ProductPercentage = (float)decimal.Divide(c.Products!.Count(), productsTotalCount) * 100
}).ToArrayAsync(cancellationToken);
}).ToListAsync(cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ namespace Boilerplate.Shared.Dtos;
[JsonSerializable(typeof(TodoItemDto))]
[JsonSerializable(typeof(PagedResult<TodoItemDto>))]
//#elif (sample == "Admin")
[JsonSerializable(typeof(ProductsCountPerCategoryResponseDto))]
[JsonSerializable(typeof(List<ProductsCountPerCategoryResponseDto>))]
[JsonSerializable(typeof(OverallAnalyticsStatsDataResponseDto))]
[JsonSerializable(typeof(ProductSaleStatResponseDto))]
[JsonSerializable(typeof(ProductPercentagePerCategoryResponseDto))]
[JsonSerializable(typeof(List<ProductSaleStatResponseDto>))]
[JsonSerializable(typeof(List<ProductPercentagePerCategoryResponseDto>))]
[JsonSerializable(typeof(ProductDto))]
[JsonSerializable(typeof(PagedResult<ProductDto>))]
[JsonSerializable(typeof(List<ProductDto>))]
[JsonSerializable(typeof(CategoryDto))]
[JsonSerializable(typeof(PagedResult<CategoryDto>))]
[JsonSerializable(typeof(List<CategoryDto>))]
//#endif
[JsonSerializable(typeof(SignInRequestDto))]
[JsonSerializable(typeof(TokenResponseDto))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public static JsonTypeInfo<T> GetTypeInfo<T>(this JsonSerializerOptions options)
return (JsonTypeInfo<T>)result;
}

return JsonTypeInfo.CreateJsonTypeInfo<T>(options);
throw new InvalidOperationException($"Add [JsonSerializable(typeof({GetTypeDisplayName(typeof(T))}))] to the {nameof(AppJsonContext)}");
}

private static string GetTypeDisplayName(Type type)
{
if (type.IsGenericType)
{
return $"{type.Name.Split('`')[0]}<{string.Join(", ", type.GetGenericArguments().Select(GetTypeDisplayName))}>";
}

return type.Name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private async Task GetData()
{
isLoading = true;

var data = await (await dashboardController.GetProductsCountPerCategoryStats(CurrentCancellationToken)).ToListAsync(CurrentCancellationToken);
var data = await dashboardController.GetProductsCountPerCategoryStats(CurrentCancellationToken);

BitChartBarDataset<int> chartDataSet = [.. data.Select(d => d.ProductCount)];
chartDataSet.BackgroundColor = data.Select(d => d.CategoryColor ?? string.Empty).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private async Task GetData()
{
isLoading = true;

var data = await (await dashboardController.GetProductsSalesStats(CurrentCancellationToken)).ToListAsync(CurrentCancellationToken);
var data = await dashboardController.GetProductsSalesStats(CurrentCancellationToken);

BitChartBarDataset<decimal> chartDataSet = [.. data.Select(d => d.SaleAmount)];
chartDataSet.BackgroundColor = data.Select(d => d.CategoryColor ?? string.Empty).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private async Task LoadAllCategoriesAsync()

try
{
var categoryList = await (await categoryController.Get(CurrentCancellationToken)).ToListAsync(CurrentCancellationToken);
var categoryList = await categoryController.Get(CurrentCancellationToken);

allCategoryList = categoryList.Select(c => new BitDropdownItem<string>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public interface ICategoryController : IAppController
Task<PagedResult<CategoryDto>> GetCategories(CancellationToken cancellationToken = default) => default!;

[HttpGet]
Task<IAsyncEnumerable<CategoryDto>> Get(CancellationToken cancellationToken) => default!;
Task<List<CategoryDto>> Get(CancellationToken cancellationToken) => default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public interface IDashboardController : IAppController
Task<OverallAnalyticsStatsDataResponseDto> GetOverallAnalyticsStatsData(CancellationToken cancellationToken = default);

[HttpGet]
Task<IAsyncEnumerable<ProductsCountPerCategoryResponseDto>> GetProductsCountPerCategoryStats(CancellationToken cancellationToken = default);
Task<List<ProductsCountPerCategoryResponseDto>> GetProductsCountPerCategoryStats(CancellationToken cancellationToken = default) => default!;

[HttpGet]
Task<IAsyncEnumerable<ProductSaleStatResponseDto>> GetProductsSalesStats(CancellationToken cancellationToken = default);
Task<List<ProductSaleStatResponseDto>> GetProductsSalesStats(CancellationToken cancellationToken = default) => default!;

[HttpGet]
Task<ProductPercentagePerCategoryResponseDto[]> GetProductsPercentagePerCategoryStats(CancellationToken cancellationToken = default);
Task<List<ProductPercentagePerCategoryResponseDto>> GetProductsPercentagePerCategoryStats(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public interface IProductController : IAppController

[HttpGet]
Task<PagedResult<ProductDto>> GetProducts(CancellationToken cancellationToken = default) => default!;

[HttpGet]
Task<List<ProductDto>> Get(CancellationToken cancellationToken) => default!;
}

0 comments on commit cbda8ef

Please sign in to comment.