Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3756e2d

Browse files
Category list implemented
1 parent 7413f27 commit 3756e2d

File tree

12 files changed

+231
-43
lines changed

12 files changed

+231
-43
lines changed

‎src/AspnetRun.Api/Application/Validations/SearchProductsRequestValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace AspnetRun.Api.Application.Validations
55
{
6-
public class SearchProductsRequestValidator : AbstractValidator<SearchProductsRequest>
6+
public class SearchProductsRequestValidator : AbstractValidator<SearchPageRequest>
77
{
88
public SearchProductsRequestValidator()
99
{

‎src/AspnetRun.Api/Controllers/CategoryController.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AspnetRun.Api.Requests;
22
using AspnetRun.Application.Interfaces;
33
using AspnetRun.Application.Models;
4+
using AspnetRun.Core.Paging;
45
using MediatR;
56
using Microsoft.AspNetCore.Authentication.JwtBearer;
67
using Microsoft.AspNetCore.Authorization;
@@ -35,5 +36,15 @@ public async Task<ActionResult<IEnumerable<CategoryModel>>> GetCategories()
3536

3637
return Ok(categories);
3738
}
39+
40+
[Route("[action]")]
41+
[HttpPost]
42+
[ProducesResponseType(typeof(IPagedList<CategoryModel>), (int)HttpStatusCode.OK)]
43+
public async Task<ActionResult<IPagedList<CategoryModel>>> SearchCategories(SearchPageRequest request)
44+
{
45+
var categoryPagedList = await _categoryService.SearchCategories(request.Args);
46+
47+
return Ok(categoryPagedList);
48+
}
3849
}
3950
}

‎src/AspnetRun.Api/Controllers/ProductController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task<ActionResult<IEnumerable<ProductModel>>> GetProducts()
4040
[Route("[action]")]
4141
[HttpPost]
4242
[ProducesResponseType(typeof(IPagedList<ProductModel>), (int)HttpStatusCode.OK)]
43-
public async Task<ActionResult<IPagedList<ProductModel>>> SearchProducts(SearchProductsRequest request)
43+
public async Task<ActionResult<IPagedList<ProductModel>>> SearchProducts(SearchPageRequest request)
4444
{
4545
var productPagedList = await _productService.SearchProducts(request.Args);
4646

‎src/AspnetRun.Api/Requests/SearchProductsRequest.cs renamed to ‎src/AspnetRun.Api/Requests/SearchPageRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace AspnetRun.Api.Requests
44
{
5-
public class SearchProductsRequest
5+
public class SearchPageRequest
66
{
77
public PageSearchArgs Args { get; set; }
88
}

‎src/AspnetRun.Application/Interfaces/ICategoryService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AspnetRun.Application.Models;
2+
using AspnetRun.Core.Paging;
23
using System.Collections.Generic;
34
using System.Threading.Tasks;
45

@@ -7,5 +8,7 @@ namespace AspnetRun.Application.Interfaces
78
public interface ICategoryService
89
{
910
Task<IEnumerable<CategoryModel>> GetCategoryList();
11+
12+
Task<IPagedList<CategoryModel>> SearchCategories(PageSearchArgs args);
1013
}
1114
}

‎src/AspnetRun.Application/Services/CategoryService.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
using AspnetRun.Application.Models;
77
using AspnetRun.Core.Entities;
88
using AspnetRun.Core.Interfaces;
9+
using AspnetRun.Core.Paging;
10+
using AspnetRun.Core.Repositories;
911
using AspnetRun.Core.Repositories.Base;
12+
using AspnetRun.Infrastructure.Paging;
1013

1114
namespace AspnetRun.Application.Services
1215
{
1316
public class CategoryService : ICategoryService
1417
{
15-
private readonly IRepository<Category> _categoryRepository;
18+
private readonly ICategoryRepository _categoryRepository;
1619
private readonly IAppLogger<CategoryService> _logger;
1720

18-
public CategoryService(IRepository<Category> categoryRepository, IAppLogger<CategoryService> logger)
21+
public CategoryService(ICategoryRepository categoryRepository, IAppLogger<CategoryService> logger)
1922
{
2023
_categoryRepository = categoryRepository ?? throw new ArgumentNullException(nameof(categoryRepository));
2124
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
@@ -29,5 +32,21 @@ public async Task<IEnumerable<CategoryModel>> GetCategoryList()
2932

3033
return categoryModels;
3134
}
35+
36+
public async Task<IPagedList<CategoryModel>> SearchCategories(PageSearchArgs args)
37+
{
38+
var categoryPagedList = await _categoryRepository.SearchCategoriesAsync(args);
39+
40+
var categoryModels = ObjectMapper.Mapper.Map<List<CategoryModel>>(categoryPagedList.Items);
41+
42+
var categoryModelPagedList = new PagedList<CategoryModel>(
43+
categoryPagedList.PageIndex,
44+
categoryPagedList.PageSize,
45+
categoryPagedList.TotalCount,
46+
categoryPagedList.TotalPages,
47+
categoryModels);
48+
49+
return categoryModelPagedList;
50+
}
3251
}
3352
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using AspnetRun.Core.Entities;
2+
using AspnetRun.Core.Paging;
23
using AspnetRun.Core.Repositories.Base;
4+
using System.Threading.Tasks;
35

46
namespace AspnetRun.Core.Repositories
57
{
68
public interface ICategoryRepository : IRepository<Category>
79
{
8-
//Task<Category> GetCategoryWithProductsAsync(int categoryId);
10+
Task<IPagedList<Category>>SearchCategoriesAsync(PageSearchArgsargs);
911
}
1012
}

‎src/AspnetRun.Infrastructure/IoC/DependencyRegistrar.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
1919
{
2020
// repositories
2121
builder.RegisterType<ProductRepository>().As<IProductRepository>().InstancePerDependency();
22+
builder.RegisterType<CategoryRepository>().As<ICategoryRepository>().InstancePerDependency();
2223
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
2324
builder.RegisterGeneric(typeof(EnumRepository<>)).As(typeof(IEnumRepository<>)).InstancePerDependency();
2425
builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerDependency();
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,77 @@
1-
using AspnetRun.Core.Entities;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Threading.Tasks;
5+
using AspnetRun.Core.Entities;
6+
using AspnetRun.Core.Paging;
27
using AspnetRun.Core.Repositories;
38
using AspnetRun.Infrastructure.Data;
9+
using AspnetRun.Infrastructure.Paging;
410
using AspnetRun.Infrastructure.Repository.Base;
511

612
namespace AspnetRun.Infrastructure.Repository
713
{
814
public class CategoryRepository : Repository<Category>, ICategoryRepository
915
{
10-
public CategoryRepository(AspnetRunContext dbContext) : base(dbContext)
16+
public CategoryRepository(AspnetRunContext dbContext)
17+
: base(dbContext)
1118
{
1219
}
20+
21+
public Task<IPagedList<Category>> SearchCategoriesAsync(PageSearchArgs args)
22+
{
23+
var query = Table;
24+
25+
var orderByList = new List<Tuple<SortingOption, Expression<Func<Category, object>>>>();
26+
27+
if (args.SortingOptions != null)
28+
{
29+
foreach (var sortingOption in args.SortingOptions)
30+
{
31+
switch (sortingOption.Field)
32+
{
33+
case "id":
34+
orderByList.Add(new Tuple<SortingOption, Expression<Func<Category, object>>>(sortingOption, c => c.Id));
35+
break;
36+
case "name":
37+
orderByList.Add(new Tuple<SortingOption, Expression<Func<Category, object>>>(sortingOption, c => c.Name));
38+
break;
39+
case "description":
40+
orderByList.Add(new Tuple<SortingOption, Expression<Func<Category, object>>>(sortingOption, c => c.Description));
41+
break;
42+
}
43+
}
44+
}
45+
46+
if (orderByList.Count == 0)
47+
{
48+
orderByList.Add(new Tuple<SortingOption, Expression<Func<Category, object>>>(new SortingOption { Direction = SortingOption.SortingDirection.ASC }, c => c.Id));
49+
}
50+
51+
var filterList = new List<Tuple<FilteringOption, Expression<Func<Category, bool>>>>();
52+
53+
if (args.FilteringOptions != null)
54+
{
55+
foreach (var filteringOption in args.FilteringOptions)
56+
{
57+
switch (filteringOption.Field)
58+
{
59+
case "id":
60+
filterList.Add(new Tuple<FilteringOption, Expression<Func<Category, bool>>>(filteringOption, c => c.Id == (int)filteringOption.Value));
61+
break;
62+
case "name":
63+
filterList.Add(new Tuple<FilteringOption, Expression<Func<Category, bool>>>(filteringOption, c => c.Name.Contains((string)filteringOption.Value)));
64+
break;
65+
case "description":
66+
filterList.Add(new Tuple<FilteringOption, Expression<Func<Category, bool>>>(filteringOption, c => c.Description.Contains((string)filteringOption.Value)));
67+
break;
68+
}
69+
}
70+
}
71+
72+
var categoryPagedList = new PagedList<Category>(query, new PagingArgs { PageIndex = args.PageIndex, PageSize = args.PageSize, PagingStrategy = args.PagingStrategy }, orderByList, filterList);
73+
74+
return Task.FromResult<IPagedList<Category>>(categoryPagedList);
75+
}
1376
}
1477
}

‎src/AspnetRun.Web/src/app/core/services/category-data.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { Injectable } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { environment } from 'src/environments/environment';
5-
import { ICategory } from 'src/app/shared/interfaces';
5+
import { ICategory,IPagedList } from 'src/app/shared/interfaces';
66

77
@Injectable()
88
export class CategoryDataService {
9-
constructor(private httpClient: HttpClient) { }
9+
constructor(private httpClient: HttpClient) { }
1010

11-
getCategories(): Observable<ICategory[]> {
12-
return this.httpClient.get<ICategory[]>(environment.apiUrl + '/Category/GetCategories');
13-
}
11+
getCategories(): Observable<ICategory[]> {
12+
return this.httpClient.get<ICategory[]>(environment.apiUrl + '/Category/GetCategories');
13+
}
14+
15+
searchCategories(args: any): Observable<IPagedList<ICategory>> {
16+
var request = { args: args };
17+
18+
return this.httpClient.post<IPagedList<ICategory>>(environment.apiUrl + '/Category/SearchCategories', request);
19+
}
1420
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /