Add db connection and data model generation.
This commit is contained in:
parent
400e44dc34
commit
37908c3f83
@ -4,6 +4,7 @@ services:
|
||||
eshopweb:
|
||||
environment:
|
||||
- ASPNETCORE_ENVIRONMENT=Development
|
||||
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word
|
||||
ports:
|
||||
- "5106:5106"
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM microsoft/aspnetcore:1.0
|
||||
FROM microsoft/aspnetcore:1.1
|
||||
ARG source
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
|
@ -0,0 +1,79 @@
|
||||
namespace eShopWeb.Infrastructure
|
||||
{
|
||||
using eShopWeb.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
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.Entity<CatalogBrand>(ConfigureCatalogBrand);
|
||||
builder.Entity<CatalogType>(ConfigureCatalogType);
|
||||
builder.Entity<CatalogItem>(ConfigureCatalogItem);
|
||||
}
|
||||
|
||||
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
|
||||
{
|
||||
builder.ToTable("Catalog");
|
||||
|
||||
builder.Property(ci => ci.Id)
|
||||
.ForSqlServerUseSequenceHiLo("catalog_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(ci => ci.Name)
|
||||
.IsRequired(true)
|
||||
.HasMaxLength(50);
|
||||
|
||||
builder.Property(ci => ci.Price)
|
||||
.IsRequired(true);
|
||||
|
||||
builder.Property(ci => ci.PictureUri)
|
||||
.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)
|
||||
{
|
||||
builder.ToTable("CatalogBrand");
|
||||
|
||||
builder.HasKey(ci => ci.Id);
|
||||
|
||||
builder.Property(ci => ci.Id)
|
||||
.ForSqlServerUseSequenceHiLo("catalog_brand_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(cb => cb.Brand)
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
}
|
||||
|
||||
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
|
||||
{
|
||||
builder.ToTable("CatalogType");
|
||||
|
||||
builder.HasKey(ci => ci.Id);
|
||||
|
||||
builder.Property(ci => ci.Id)
|
||||
.ForSqlServerUseSequenceHiLo("catalog_type_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(cb => cb.Type)
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
namespace eShopWeb.Infrastructure
|
||||
{
|
||||
using eShopWeb.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class CatalogContextSeed
|
||||
{
|
||||
public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0)
|
||||
{
|
||||
int retryForAvaiability = retry.Value;
|
||||
try
|
||||
{
|
||||
var context = (CatalogContext)applicationBuilder
|
||||
.ApplicationServices.GetService(typeof(CatalogContext));
|
||||
|
||||
//context.Database.Migrate();
|
||||
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();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (retryForAvaiability < 10)
|
||||
{
|
||||
retryForAvaiability++;
|
||||
var log = loggerFactory.CreateLogger("catalog seed");
|
||||
log.LogError(ex.Message);
|
||||
await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static IEnumerable<CatalogBrand> GetPreconfiguredCatalogBrands()
|
||||
{
|
||||
return new List<CatalogBrand>()
|
||||
{
|
||||
new CatalogBrand() { Brand = "Azure"},
|
||||
new CatalogBrand() { Brand = ".NET" },
|
||||
new CatalogBrand() { Brand = "Visual Studio" },
|
||||
new CatalogBrand() { Brand = "SQL Server" },
|
||||
new CatalogBrand() { Brand = "Other" }
|
||||
};
|
||||
}
|
||||
|
||||
static IEnumerable<CatalogType> GetPreconfiguredCatalogTypes()
|
||||
{
|
||||
return new List<CatalogType>()
|
||||
{
|
||||
new CatalogType() { Type = "Mug"},
|
||||
new CatalogType() { Type = "T-Shirt" },
|
||||
new CatalogType() { Type = "Sheet" },
|
||||
new CatalogType() { Type = "USB Memory Stick" }
|
||||
};
|
||||
}
|
||||
|
||||
static IEnumerable<CatalogItem> GetPreconfiguredItems()
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
15
src/Web/WebMonolithic/eShopWeb/Models/CatalogBrand.cs
Normal file
15
src/Web/WebMonolithic/eShopWeb/Models/CatalogBrand.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace eShopWeb.Models
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
@ -4,15 +4,24 @@ namespace eShopWeb.Models
|
||||
{
|
||||
public class CatalogItem
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public string PictureUri { get; set; }
|
||||
public int CatalogBrandId { get; set; }
|
||||
public string CatalogBrand { get; set; }
|
||||
public int CatalogTypeId { get; set; }
|
||||
public string CatalogType { get; set; }
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
|
||||
public decimal Price { get; set; }
|
||||
|
||||
public string PictureUri { get; set; }
|
||||
|
||||
public int CatalogTypeId { get; set; }
|
||||
|
||||
public CatalogType CatalogType { get; set; }
|
||||
|
||||
public int CatalogBrandId { get; set; }
|
||||
|
||||
public CatalogBrand CatalogBrand { get; set; }
|
||||
|
||||
public CatalogItem() { }
|
||||
}
|
||||
}
|
14
src/Web/WebMonolithic/eShopWeb/Models/CatalogType.cs
Normal file
14
src/Web/WebMonolithic/eShopWeb/Models/CatalogType.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace eShopWeb.Models
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
27
src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs
Normal file
27
src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using eShopWeb.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
namespace eShopWeb.Services
|
||||
{
|
||||
public class CatalogService : ICatalogService
|
||||
{
|
||||
public IEnumerable<SelectListItem> GetBrands()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<SelectListItem> GetTypes()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
16
src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs
Normal file
16
src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using eShopWeb.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eShopWeb.Services
|
||||
{
|
||||
public interface ICatalogService
|
||||
{
|
||||
IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied);
|
||||
IEnumerable<SelectListItem> GetBrands();
|
||||
IEnumerable<SelectListItem> GetTypes();
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using eShopWeb.Infrastructure;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@ -27,7 +26,24 @@ namespace eShopWeb
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// Add framework services.
|
||||
services.AddDbContext<CatalogContext>(c =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var text = Configuration["ConnectionString"];
|
||||
c.UseSqlServer(Configuration["ConnectionString"]);
|
||||
c.ConfigureWarnings(wb =>
|
||||
{
|
||||
//By default, in this application, we don't want to have client evaluations
|
||||
wb.Log(RelationalEventId.QueryClientEvaluationWarning);
|
||||
});
|
||||
}
|
||||
catch (System.Exception ex )
|
||||
{
|
||||
var message = ex.Message;
|
||||
}
|
||||
});
|
||||
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
@ -55,6 +71,10 @@ namespace eShopWeb
|
||||
name: "default",
|
||||
template: "{controller=Catalog}/{action=Index}/{id?}");
|
||||
});
|
||||
|
||||
//Seed Data
|
||||
CatalogContextSeed.SeedAsync(app, loggerFactory)
|
||||
.Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
{
|
||||
"ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word",
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp1.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp1.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
@ -9,45 +9,17 @@
|
||||
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Pics\1.png" />
|
||||
<None Remove="Pics\10.png" />
|
||||
<None Remove="Pics\11.png" />
|
||||
<None Remove="Pics\12.png" />
|
||||
<None Remove="Pics\2.png" />
|
||||
<None Remove="Pics\3.png" />
|
||||
<None Remove="Pics\4.png" />
|
||||
<None Remove="Pics\5.png" />
|
||||
<None Remove="Pics\6.png" />
|
||||
<None Remove="Pics\7.png" />
|
||||
<None Remove="Pics\8.png" />
|
||||
<None Remove="Pics\9.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Pics\1.png" />
|
||||
<Content Include="Pics\10.png" />
|
||||
<Content Include="Pics\11.png" />
|
||||
<Content Include="Pics\12.png" />
|
||||
<Content Include="Pics\2.png" />
|
||||
<Content Include="Pics\3.png" />
|
||||
<Content Include="Pics\4.png" />
|
||||
<Content Include="Pics\5.png" />
|
||||
<Content Include="Pics\6.png" />
|
||||
<Content Include="Pics\7.png" />
|
||||
<Content Include="Pics\8.png" />
|
||||
<Content Include="Pics\9.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.0.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.0.0-msbuild3-final" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@ -55,7 +27,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Services\" />
|
||||
<Folder Include="Pics\" />
|
||||
<Folder Include="Infrastructure\" />
|
||||
<Folder Include="Views\Catalog\" />
|
||||
<Folder Include="wwwroot\css\catalog\" />
|
||||
<Folder Include="wwwroot\fonts\" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user