@ -0,0 +1,15 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure | |||||
{ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
public class CatalogBrand | |||||
{ | |||||
public int Id { get; set; } | |||||
public string Brand { get; set; } | |||||
} | |||||
} |
@ -0,0 +1,86 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure | |||||
{ | |||||
using EntityFrameworkCore.Metadata.Builders; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Npgsql.EntityFrameworkCore.PostgreSQL; | |||||
public class CatalogContext : DbContext | |||||
{ | |||||
public CatalogContext(DbContextOptions options) : base(options) | |||||
{ | |||||
} | |||||
public DbSet<CatalogItem> CatalogItems { get; set; } | |||||
public DbSet<CatalogBrand> CatalogBrands { get; set; } | |||||
public DbSet<CatalogType> CatalogTypes { get; set; } | |||||
protected override void OnModelCreating(ModelBuilder builder) | |||||
{ | |||||
builder.HasSequence("idseqcatalog") | |||||
.StartsAt(1) | |||||
.IncrementsBy(1); | |||||
builder.HasSequence("idseqcatalogbrand") | |||||
.StartsAt(1) | |||||
.IncrementsBy(1); | |||||
builder.HasSequence("idseqcatalogtype") | |||||
.StartsAt(1) | |||||
.IncrementsBy(1); | |||||
builder.Entity<CatalogItem>(ConfigureCatalogItem); | |||||
builder.Entity<CatalogBrand>(ConfigureCatalogBrand); | |||||
builder.Entity<CatalogType>(ConfigureCatalogType); | |||||
builder.HasPostgresExtension("uuid-ossp"); | |||||
} | |||||
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder) | |||||
{ | |||||
builder.ForNpgsqlToTable("catalog"); | |||||
builder.Property(ci => ci.Id) | |||||
.HasDefaultValueSql("nextval('idseqcatalog')") | |||||
.IsRequired(); | |||||
builder.Property(ci => ci.Name) | |||||
.IsRequired(true) | |||||
.HasMaxLength(50); | |||||
builder.Property(ci => ci.Price) | |||||
.IsRequired(true); | |||||
builder.Property(ci => ci.PictureUri) | |||||
.IsRequired(false); | |||||
} | |||||
void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder) | |||||
{ | |||||
builder.ForNpgsqlToTable("catalogbrand"); | |||||
builder.Property(cb => cb.Id) | |||||
.HasDefaultValueSql("nextval('idseqcatalogbrand')") | |||||
.IsRequired(); | |||||
builder.Property(cb => cb.Brand) | |||||
.IsRequired() | |||||
.HasMaxLength(100); | |||||
} | |||||
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder) | |||||
{ | |||||
builder.ForNpgsqlToTable("catalogtype"); | |||||
builder.Property(cb => cb.Id) | |||||
.HasDefaultValueSql("nextval('idseqcatalogtype')") | |||||
.IsRequired(); | |||||
builder.Property(cb => cb.Type) | |||||
.IsRequired() | |||||
.HasMaxLength(100); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,75 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure | |||||
{ | |||||
using Microsoft.AspNetCore.Builder; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
public class CatalogContextSeed | |||||
{ | |||||
public static async Task SeedAsync(IApplicationBuilder applicationBuilder) | |||||
{ | |||||
var context = (CatalogContext)applicationBuilder | |||||
.ApplicationServices.GetService(typeof(CatalogContext)); | |||||
using (context) | |||||
{ | |||||
context.Database.EnsureCreated(); | |||||
if (!context.CatalogBrands.Any()) | |||||
{ | |||||
context.CatalogBrands.AddRange( | |||||
GetPreconfiguredCatalogBrands()); | |||||
await context.SaveChangesAsync(); | |||||
} | |||||
if (!context.CatalogTypes.Any()) | |||||
{ | |||||
context.CatalogTypes.AddRange( | |||||
GetPreconfiguredCatalogTypes()); | |||||
await context.SaveChangesAsync(); | |||||
} | |||||
if (!context.CatalogItems.Any()) | |||||
{ | |||||
context.CatalogItems.AddRange( | |||||
GetPreconfiguredItems()); | |||||
await context.SaveChangesAsync(); | |||||
} | |||||
} | |||||
} | |||||
static IEnumerable<CatalogBrand> GetPreconfiguredCatalogBrands() | |||||
{ | |||||
return new List<CatalogBrand>() | |||||
{ | |||||
new CatalogBrand() { Brand="Azure"}, | |||||
new CatalogBrand() { Brand = "Visual Studio" } | |||||
}; | |||||
} | |||||
static IEnumerable<CatalogType> GetPreconfiguredCatalogTypes() | |||||
{ | |||||
return new List<CatalogType>() | |||||
{ | |||||
new CatalogType() { Type="Mug"}, | |||||
new CatalogType() { Type = "T-Shirt" } | |||||
}; | |||||
} | |||||
static IEnumerable<CatalogItem> GetPreconfiguredItems() | |||||
{ | |||||
return new List<CatalogItem>() | |||||
{ | |||||
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" }, | |||||
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" }, | |||||
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" }, | |||||
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" }, | |||||
}; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,14 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure | |||||
{ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
public class CatalogType | |||||
{ | |||||
public int Id { get; set; } | |||||
public string Type { get; set; } | |||||
} | |||||
} |
@ -1,46 +0,0 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model | |||||
{ | |||||
using EntityFrameworkCore.Metadata.Builders; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Npgsql.EntityFrameworkCore.PostgreSQL; | |||||
public class CatalogContext : DbContext | |||||
{ | |||||
public CatalogContext(DbContextOptions options) : base(options) | |||||
{ | |||||
} | |||||
public DbSet<CatalogItem> CatalogItems { get; set; } | |||||
protected override void OnModelCreating(ModelBuilder builder) | |||||
{ | |||||
builder.HasSequence("idseq") | |||||
.StartsAt(1) | |||||
.IncrementsBy(1); | |||||
builder.Entity<CatalogItem>(ConfigureCatalogItem); | |||||
builder.HasPostgresExtension("uuid-ossp"); | |||||
} | |||||
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder) | |||||
{ | |||||
builder.ForNpgsqlToTable("catalog"); | |||||
builder.Property(ci => ci.Id) | |||||
.HasDefaultValueSql("nextval('idseq')") | |||||
.IsRequired(); | |||||
builder.Property(ci => ci.Name) | |||||
.IsRequired(true) | |||||
.HasMaxLength(50); | |||||
builder.Property(ci => ci.Price) | |||||
.IsRequired(true); | |||||
builder.Property(ci => ci.PictureUri) | |||||
.IsRequired(false); | |||||
} | |||||
} | |||||
} |
@ -1,67 +0,0 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model | |||||
{ | |||||
using Microsoft.AspNetCore.Builder; | |||||
using Model; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
public class CatalogContextSeed | |||||
{ | |||||
public static async Task SeedAsync(IApplicationBuilder applicationBuilder) | |||||
{ | |||||
var context = (CatalogContext)applicationBuilder | |||||
.ApplicationServices.GetService(typeof(CatalogContext)); | |||||
using (context) | |||||
{ | |||||
context.Database.EnsureCreated(); | |||||
if (!context.CatalogItems.Any()) | |||||
{ | |||||
context.CatalogItems.AddRange( | |||||
GetPreconfiguredItems()); | |||||
await context.SaveChangesAsync(); | |||||
} | |||||
} | |||||
} | |||||
static IEnumerable<CatalogItem> GetPreconfiguredItems() | |||||
{ | |||||
return new List<CatalogItem>() | |||||
{ | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" } | |||||
}; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,24 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel | |||||
{ | |||||
using System.Collections.Generic; | |||||
public class PaginatedItemsViewModel<TEntity> where TEntity : class | |||||
{ | |||||
public int PageIndex { get; private set; } | |||||
public int PageSize { get; private set; } | |||||
public long Count { get; private set; } | |||||
public IEnumerable<TEntity> Data { get; private set; } | |||||
public PaginatedItemsViewModel(int pageIndex, int pageSize, long count, IEnumerable<TEntity> data) | |||||
{ | |||||
this.PageIndex = pageIndex; | |||||
this.PageSize = pageSize; | |||||
this.Count = count; | |||||
this.Data = data; | |||||
} | |||||
} | |||||
} |