Add new method in catalog controller to support filter by type and brand
This commit is contained in:
		
							parent
							
								
									728cb0fbfa
								
							
						
					
					
						commit
						725d658697
					
				| @ -39,10 +39,10 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers | |||||||
|             return Ok(model); |             return Ok(model); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // GET api/v1/[controller]/FindCatalogItemByName/samplename |         // GET api/v1/[controller]/items/withname/samplename | ||||||
| 
 | 
 | ||||||
|         [HttpGet] |         [HttpGet] | ||||||
|         [Route("[action]/{name:minlength(1)}")]
 |         [Route("[action]/withname/{name:minlength(1)}")]
 | ||||||
|         public async Task<IActionResult> Items(string name, int pageSize = 10, int pageIndex = 0) |         public async Task<IActionResult> Items(string name, int pageSize = 10, int pageIndex = 0) | ||||||
|         { |         { | ||||||
| 
 | 
 | ||||||
| @ -62,6 +62,38 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers | |||||||
|             return Ok(model); |             return Ok(model); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         // GET api/v1/[controller]/items/type/1/brand/null | ||||||
|  | 
 | ||||||
|  |         [HttpGet] | ||||||
|  |         [Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")]
 | ||||||
|  |         public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId, int pageSize = 10, int pageIndex = 0) | ||||||
|  |         { | ||||||
|  |             var root = (IQueryable<CatalogItem>)_context.CatalogItems; | ||||||
|  | 
 | ||||||
|  |             if (catalogTypeId.HasValue) | ||||||
|  |             { | ||||||
|  |                 root = root.Where(ci => ci.CatalogTypeId == catalogTypeId); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             if (catalogBrandId.HasValue) | ||||||
|  |             { | ||||||
|  |                 root = root.Where(ci => ci.CatalogBrandId == catalogBrandId); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             var totalItems = await root | ||||||
|  |                 .LongCountAsync(); | ||||||
|  | 
 | ||||||
|  |             var itemsOnPage = await root | ||||||
|  |                 .Skip(pageSize * pageIndex) | ||||||
|  |                 .Take(pageSize) | ||||||
|  |                 .ToListAsync(); | ||||||
|  | 
 | ||||||
|  |             var model = new PaginatedItemsViewModel<CatalogItem>( | ||||||
|  |                 pageIndex, pageSize, totalItems, itemsOnPage); | ||||||
|  | 
 | ||||||
|  |             return Ok(model); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         // GET api/v1/[controller]/CatalogTypes |         // GET api/v1/[controller]/CatalogTypes | ||||||
| 
 | 
 | ||||||
|         [HttpGet] |         [HttpGet] | ||||||
|  | |||||||
| @ -30,9 +30,10 @@ | |||||||
|                 .StartsAt(1) |                 .StartsAt(1) | ||||||
|                 .IncrementsBy(1); |                 .IncrementsBy(1); | ||||||
| 
 | 
 | ||||||
|             builder.Entity<CatalogItem>(ConfigureCatalogItem); |  | ||||||
|             builder.Entity<CatalogBrand>(ConfigureCatalogBrand); |             builder.Entity<CatalogBrand>(ConfigureCatalogBrand); | ||||||
|             builder.Entity<CatalogType>(ConfigureCatalogType); |             builder.Entity<CatalogType>(ConfigureCatalogType); | ||||||
|  |             builder.Entity<CatalogItem>(ConfigureCatalogItem); | ||||||
|  |             | ||||||
| 
 | 
 | ||||||
|             builder.HasPostgresExtension("uuid-ossp"); |             builder.HasPostgresExtension("uuid-ossp"); | ||||||
|         } |         } | ||||||
| @ -55,6 +56,14 @@ | |||||||
|             builder.Property(ci => ci.PictureUri) |             builder.Property(ci => ci.PictureUri) | ||||||
|                 .IsRequired(false); |                 .IsRequired(false); | ||||||
| 
 | 
 | ||||||
|  |             builder.HasOne(ci => ci.CatalogBrand) | ||||||
|  |                 .WithMany() | ||||||
|  |                 .HasForeignKey(ci => ci.CatalogBrandId); | ||||||
|  | 
 | ||||||
|  |             builder.HasOne(ci => ci.CatalogType) | ||||||
|  |                 .WithMany() | ||||||
|  |                 .HasForeignKey(ci => ci.CatalogTypeId); | ||||||
|  | 
 | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder) |         void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder) | ||||||
|  | |||||||
| @ -15,6 +15,8 @@ | |||||||
| 
 | 
 | ||||||
|             using (context) |             using (context) | ||||||
|             { |             { | ||||||
|  |                 context.Database.EnsureDeleted(); | ||||||
|  | 
 | ||||||
|                 context.Database.EnsureCreated(); |                 context.Database.EnsureCreated(); | ||||||
| 
 | 
 | ||||||
|                 if (!context.CatalogBrands.Any()) |                 if (!context.CatalogBrands.Any()) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user