Browse Source

Add new method in catalog controller to support filter by type and brand

pull/49/merge
Unai 8 years ago
parent
commit
725d658697
3 changed files with 46 additions and 3 deletions
  1. +34
    -2
      src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs
  2. +10
    -1
      src/Services/Catalog/Catalog.API/Infrastructure/CatalogContext.cs
  3. +2
    -0
      src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs

+ 34
- 2
src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs View File

@ -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]


+ 10
- 1
src/Services/Catalog/Catalog.API/Infrastructure/CatalogContext.cs View File

@ -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)


+ 2
- 0
src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs View File

@ -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…
Cancel
Save