Skip to content

Commit 37908c3

Browse files
committed
Add db connection and data model generation.
1 parent 400e44d commit 37908c3

12 files changed

Lines changed: 307 additions & 49 deletions

File tree

src/Web/WebMonolithic/docker-compose.override.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ services:
44
eshopweb:
55
environment:
66
- ASPNETCORE_ENVIRONMENT=Development
7+
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word
78
ports:
89
- "5106:5106"
910

src/Web/WebMonolithic/eShopWeb/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM microsoft/aspnetcore:1.0
1+
FROM microsoft/aspnetcore:1.1
22
ARG source
33
WORKDIR /app
44
EXPOSE 80
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace eShopWeb.Infrastructure
2+
{
3+
using eShopWeb.Models;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
6+
7+
public class CatalogContext : DbContext
8+
{
9+
public CatalogContext(DbContextOptions options) : base(options)
10+
{
11+
}
12+
public DbSet<CatalogItem> CatalogItems { get; set; }
13+
public DbSet<CatalogBrand> CatalogBrands { get; set; }
14+
public DbSet<CatalogType> CatalogTypes { get; set; }
15+
protected override void OnModelCreating(ModelBuilder builder)
16+
{
17+
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
18+
builder.Entity<CatalogType>(ConfigureCatalogType);
19+
builder.Entity<CatalogItem>(ConfigureCatalogItem);
20+
}
21+
22+
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
23+
{
24+
builder.ToTable("Catalog");
25+
26+
builder.Property(ci => ci.Id)
27+
.ForSqlServerUseSequenceHiLo("catalog_hilo")
28+
.IsRequired();
29+
30+
builder.Property(ci => ci.Name)
31+
.IsRequired(true)
32+
.HasMaxLength(50);
33+
34+
builder.Property(ci => ci.Price)
35+
.IsRequired(true);
36+
37+
builder.Property(ci => ci.PictureUri)
38+
.IsRequired(false);
39+
40+
builder.HasOne(ci => ci.CatalogBrand)
41+
.WithMany()
42+
.HasForeignKey(ci => ci.CatalogBrandId);
43+
44+
builder.HasOne(ci => ci.CatalogType)
45+
.WithMany()
46+
.HasForeignKey(ci => ci.CatalogTypeId);
47+
}
48+
49+
void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder)
50+
{
51+
builder.ToTable("CatalogBrand");
52+
53+
builder.HasKey(ci => ci.Id);
54+
55+
builder.Property(ci => ci.Id)
56+
.ForSqlServerUseSequenceHiLo("catalog_brand_hilo")
57+
.IsRequired();
58+
59+
builder.Property(cb => cb.Brand)
60+
.IsRequired()
61+
.HasMaxLength(100);
62+
}
63+
64+
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
65+
{
66+
builder.ToTable("CatalogType");
67+
68+
builder.HasKey(ci => ci.Id);
69+
70+
builder.Property(ci => ci.Id)
71+
.ForSqlServerUseSequenceHiLo("catalog_type_hilo")
72+
.IsRequired();
73+
74+
builder.Property(cb => cb.Type)
75+
.IsRequired()
76+
.HasMaxLength(100);
77+
}
78+
}
79+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
namespace eShopWeb.Infrastructure
2+
{
3+
using eShopWeb.Models;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.Extensions.Logging;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
public class CatalogContextSeed
13+
{
14+
public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0)
15+
{
16+
int retryForAvaiability = retry.Value;
17+
try
18+
{
19+
var context = (CatalogContext)applicationBuilder
20+
.ApplicationServices.GetService(typeof(CatalogContext));
21+
22+
//context.Database.Migrate();
23+
context.Database.EnsureCreated();
24+
25+
if (!context.CatalogBrands.Any())
26+
{
27+
context.CatalogBrands.AddRange(
28+
GetPreconfiguredCatalogBrands());
29+
30+
await context.SaveChangesAsync();
31+
}
32+
33+
if (!context.CatalogTypes.Any())
34+
{
35+
context.CatalogTypes.AddRange(
36+
GetPreconfiguredCatalogTypes());
37+
38+
await context.SaveChangesAsync();
39+
}
40+
41+
if (!context.CatalogItems.Any())
42+
{
43+
context.CatalogItems.AddRange(
44+
GetPreconfiguredItems());
45+
46+
await context.SaveChangesAsync();
47+
}
48+
}
49+
catch (Exception ex)
50+
{
51+
if (retryForAvaiability < 10)
52+
{
53+
retryForAvaiability++;
54+
var log = loggerFactory.CreateLogger("catalog seed");
55+
log.LogError(ex.Message);
56+
await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability);
57+
}
58+
}
59+
}
60+
61+
static IEnumerable<CatalogBrand> GetPreconfiguredCatalogBrands()
62+
{
63+
return new List<CatalogBrand>()
64+
{
65+
new CatalogBrand() { Brand = "Azure"},
66+
new CatalogBrand() { Brand = ".NET" },
67+
new CatalogBrand() { Brand = "Visual Studio" },
68+
new CatalogBrand() { Brand = "SQL Server" },
69+
new CatalogBrand() { Brand = "Other" }
70+
};
71+
}
72+
73+
static IEnumerable<CatalogType> GetPreconfiguredCatalogTypes()
74+
{
75+
return new List<CatalogType>()
76+
{
77+
new CatalogType() { Type = "Mug"},
78+
new CatalogType() { Type = "T-Shirt" },
79+
new CatalogType() { Type = "Sheet" },
80+
new CatalogType() { Type = "USB Memory Stick" }
81+
};
82+
}
83+
84+
static IEnumerable<CatalogItem> GetPreconfiguredItems()
85+
{
86+
return new List<CatalogItem>()
87+
{
88+
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" },
89+
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" },
90+
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" },
91+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
92+
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" },
93+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
94+
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" },
95+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
96+
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" },
97+
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" },
98+
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" },
99+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" }
100+
};
101+
}
102+
}
103+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace eShopWeb.Models
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+
}

src/Web/WebMonolithic/eShopWeb/Models/CatalogItem.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ namespace eShopWeb.Models
44
{
55
public class CatalogItem
66
{
7-
public string Id { get; set; }
7+
public int Id { get; set; }
8+
89
public string Name { get; set; }
10+
911
public string Description { get; set; }
12+
1013
public decimal Price { get; set; }
14+
1115
public string PictureUri { get; set; }
12-
public int CatalogBrandId { get; set; }
13-
public string CatalogBrand { get; set; }
16+
1417
public int CatalogTypeId { get; set; }
15-
public string CatalogType { get; set; }
1618

19+
public CatalogType CatalogType { get; set; }
20+
21+
public int CatalogBrandId { get; set; }
22+
23+
public CatalogBrand CatalogBrand { get; set; }
24+
25+
public CatalogItem() { }
1726
}
1827
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace eShopWeb.Models
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using eShopWeb.Models;
6+
using Microsoft.AspNetCore.Mvc.Rendering;
7+
8+
namespace eShopWeb.Services
9+
{
10+
public class CatalogService : ICatalogService
11+
{
12+
public IEnumerable<SelectListItem> GetBrands()
13+
{
14+
throw new NotImplementedException();
15+
}
16+
17+
public IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied)
18+
{
19+
throw new NotImplementedException();
20+
}
21+
22+
public IEnumerable<SelectListItem> GetTypes()
23+
{
24+
throw new NotImplementedException();
25+
}
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using eShopWeb.Models;
2+
using Microsoft.AspNetCore.Mvc.Rendering;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace eShopWeb.Services
9+
{
10+
public interface ICatalogService
11+
{
12+
IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied);
13+
IEnumerable<SelectListItem> GetBrands();
14+
IEnumerable<SelectListItem> GetTypes();
15+
}
16+
}

src/Web/WebMonolithic/eShopWeb/Startup.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using eShopWeb.Infrastructure;
52
using Microsoft.AspNetCore.Builder;
63
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Infrastructure;
76
using Microsoft.Extensions.Configuration;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Logging;
@@ -27,7 +26,24 @@ public Startup(IHostingEnvironment env)
2726
// This method gets called by the runtime. Use this method to add services to the container.
2827
public void ConfigureServices(IServiceCollection services)
2928
{
30-
// Add framework services.
29+
services.AddDbContext<CatalogContext>(c =>
30+
{
31+
try
32+
{
33+
var text = Configuration["ConnectionString"];
34+
c.UseSqlServer(Configuration["ConnectionString"]);
35+
c.ConfigureWarnings(wb =>
36+
{
37+
//By default, in this application, we don't want to have client evaluations
38+
wb.Log(RelationalEventId.QueryClientEvaluationWarning);
39+
});
40+
}
41+
catch (System.Exception ex )
42+
{
43+
var message = ex.Message;
44+
}
45+
});
46+
3147
services.AddMvc();
3248
}
3349

@@ -55,6 +71,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5571
name: "default",
5672
template: "{controller=Catalog}/{action=Index}/{id?}");
5773
});
74+
75+
//Seed Data
76+
CatalogContextSeed.SeedAsync(app, loggerFactory)
77+
.Wait();
5878
}
5979
}
6080
}

0 commit comments

Comments
 (0)