Skip to content

Commit da22827

Browse files
committed
Add more web api methods on catalog.api. Implement pagination featue.
1 parent fbf6adb commit da22827

10 files changed

Lines changed: 277 additions & 125 deletions

File tree

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
33
{
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
6+
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
77
using System;
88
using System.Linq;
99
using System.Threading.Tasks;
10+
using ViewModel;
1011

1112
[Route("api/v1/[controller]")]
1213
public class CatalogController : ControllerBase
@@ -18,29 +19,71 @@ public CatalogController(CatalogContext context)
1819
_context = context;
1920
}
2021

21-
// GET api/v1/[controller]/all
22+
// GET api/v1/[controller]/items/[?pageSize=3&pageIndex=10]
2223

2324
[HttpGet]
2425
[Route("[action]")]
25-
public async Task<IActionResult> All()
26+
public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0)
2627
{
27-
var items = await _context.CatalogItems
28+
var totalItems = await _context.CatalogItems
29+
.LongCountAsync();
30+
31+
var itemsOnPage = await _context.CatalogItems
32+
.Skip(pageSize * pageIndex)
33+
.Take(pageSize)
34+
.ToListAsync();
35+
36+
var model = new PaginatedItemsViewModel<CatalogItem>(
37+
pageIndex, pageSize, totalItems, itemsOnPage);
38+
39+
return Ok(model);
40+
}
41+
42+
// GET api/v1/[controller]/FindCatalogItemByName/samplename
43+
44+
[HttpGet]
45+
[Route("[action]/{name:minlength(1)}")]
46+
public async Task<IActionResult> Items(string name, int pageSize = 10, int pageIndex = 0)
47+
{
48+
49+
var totalItems = await _context.CatalogItems
50+
.Where(c => c.Name.StartsWith(name))
51+
.LongCountAsync();
52+
53+
var itemsOnPage = await _context.CatalogItems
54+
.Where(c => c.Name.StartsWith(name))
55+
.Skip(pageSize * pageIndex)
56+
.Take(pageSize)
57+
.ToListAsync();
58+
59+
var model = new PaginatedItemsViewModel<CatalogItem>(
60+
pageIndex, pageSize, totalItems, itemsOnPage);
61+
62+
return Ok(model);
63+
}
64+
65+
// GET api/v1/[controller]/CatalogTypes
66+
67+
[HttpGet]
68+
[Route("[action]")]
69+
public async Task<IActionResult> CatalogTypes()
70+
{
71+
var items = await _context.CatalogTypes
2872
.ToListAsync();
2973

3074
return Ok(items);
3175
}
3276

33-
// GET api/v1/[controller]/FindByName/samplename
77+
// GET api/v1/[controller]/CatalogBrands
3478

3579
[HttpGet]
36-
[Route("FindByName/{name:minlength(1)}")]
37-
public async Task<IActionResult> Find(string name)
80+
[Route("[action]")]
81+
public async Task<IActionResult> CatalogBrands()
3882
{
39-
var items = await _context.CatalogItems
40-
.Where(c => c.Name.StartsWith(name, StringComparison.CurrentCultureIgnoreCase))
83+
var items = await _context.CatalogBrands
4184
.ToListAsync();
4285

43-
return Ok();
86+
return Ok(items);
4487
}
4588
}
4689
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
9+
public class CatalogBrand
10+
{
11+
public int Id { get; set; }
12+
13+
public string Brand { get; set; }
14+
}
15+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using EntityFrameworkCore.Metadata.Builders;
4+
using Microsoft.EntityFrameworkCore;
5+
using Npgsql.EntityFrameworkCore.PostgreSQL;
6+
7+
public class CatalogContext : DbContext
8+
{
9+
public CatalogContext(DbContextOptions options) : base(options)
10+
{
11+
}
12+
13+
public DbSet<CatalogItem> CatalogItems { get; set; }
14+
15+
public DbSet<CatalogBrand> CatalogBrands { get; set; }
16+
17+
public DbSet<CatalogType> CatalogTypes { get; set; }
18+
19+
protected override void OnModelCreating(ModelBuilder builder)
20+
{
21+
builder.HasSequence("idseqcatalog")
22+
.StartsAt(1)
23+
.IncrementsBy(1);
24+
25+
builder.HasSequence("idseqcatalogbrand")
26+
.StartsAt(1)
27+
.IncrementsBy(1);
28+
29+
builder.HasSequence("idseqcatalogtype")
30+
.StartsAt(1)
31+
.IncrementsBy(1);
32+
33+
builder.Entity<CatalogItem>(ConfigureCatalogItem);
34+
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
35+
builder.Entity<CatalogType>(ConfigureCatalogType);
36+
37+
builder.HasPostgresExtension("uuid-ossp");
38+
}
39+
40+
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
41+
{
42+
builder.ForNpgsqlToTable("catalog");
43+
44+
builder.Property(ci => ci.Id)
45+
.HasDefaultValueSql("nextval('idseqcatalog')")
46+
.IsRequired();
47+
48+
builder.Property(ci => ci.Name)
49+
.IsRequired(true)
50+
.HasMaxLength(50);
51+
52+
builder.Property(ci => ci.Price)
53+
.IsRequired(true);
54+
55+
builder.Property(ci => ci.PictureUri)
56+
.IsRequired(false);
57+
58+
}
59+
60+
void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder)
61+
{
62+
builder.ForNpgsqlToTable("catalogbrand");
63+
64+
builder.Property(cb => cb.Id)
65+
.HasDefaultValueSql("nextval('idseqcatalogbrand')")
66+
.IsRequired();
67+
68+
builder.Property(cb => cb.Brand)
69+
.IsRequired()
70+
.HasMaxLength(100);
71+
}
72+
73+
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
74+
{
75+
builder.ForNpgsqlToTable("catalogtype");
76+
77+
builder.Property(cb => cb.Id)
78+
.HasDefaultValueSql("nextval('idseqcatalogtype')")
79+
.IsRequired();
80+
81+
builder.Property(cb => cb.Type)
82+
.IsRequired()
83+
.HasMaxLength(100);
84+
}
85+
}
86+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using Microsoft.AspNetCore.Builder;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
public class CatalogContextSeed
10+
{
11+
public static async Task SeedAsync(IApplicationBuilder applicationBuilder)
12+
{
13+
var context = (CatalogContext)applicationBuilder
14+
.ApplicationServices.GetService(typeof(CatalogContext));
15+
16+
using (context)
17+
{
18+
context.Database.EnsureCreated();
19+
20+
if (!context.CatalogBrands.Any())
21+
{
22+
context.CatalogBrands.AddRange(
23+
GetPreconfiguredCatalogBrands());
24+
25+
await context.SaveChangesAsync();
26+
}
27+
28+
if (!context.CatalogTypes.Any())
29+
{
30+
context.CatalogTypes.AddRange(
31+
GetPreconfiguredCatalogTypes());
32+
33+
await context.SaveChangesAsync();
34+
}
35+
36+
if (!context.CatalogItems.Any())
37+
{
38+
context.CatalogItems.AddRange(
39+
GetPreconfiguredItems());
40+
41+
await context.SaveChangesAsync();
42+
}
43+
}
44+
}
45+
46+
static IEnumerable<CatalogBrand> GetPreconfiguredCatalogBrands()
47+
{
48+
return new List<CatalogBrand>()
49+
{
50+
new CatalogBrand() { Brand="Azure"},
51+
new CatalogBrand() { Brand = "Visual Studio" }
52+
};
53+
}
54+
55+
static IEnumerable<CatalogType> GetPreconfiguredCatalogTypes()
56+
{
57+
return new List<CatalogType>()
58+
{
59+
new CatalogType() { Type="Mug"},
60+
new CatalogType() { Type = "T-Shirt" }
61+
};
62+
}
63+
64+
static IEnumerable<CatalogItem> GetPreconfiguredItems()
65+
{
66+
return new List<CatalogItem>()
67+
{
68+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
69+
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
70+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
71+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=1, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
72+
};
73+
}
74+
}
75+
}

src/Services/Catalog/Catalog.API/Model/CatalogItem.cs renamed to src/Services/Catalog/Catalog.API/Infrastructure/CatalogItem.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
22
{
33
using System;
44

@@ -14,6 +14,14 @@ public class CatalogItem
1414

1515
public string PictureUri { get; set; }
1616

17+
public int CatalogTypeId { get; set; }
18+
19+
public CatalogType CatalogType { get; set; }
20+
21+
public int CatalogBrandId { get; set; }
22+
23+
public CatalogBrand CatalogBrand { get; set; }
24+
1725
public CatalogItem() { }
1826
}
1927
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
public class CatalogType
9+
{
10+
public int Id { get; set; }
11+
12+
public string Type { get; set; }
13+
}
14+
}

src/Services/Catalog/Catalog.API/Model/CatalogContext.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)