Skip to content

Commit 9201160

Browse files
author
Javier Suárez Ruiz
committed
2 parents af86b2b + a2e7e77 commit 9201160

13 files changed

Lines changed: 649 additions & 71 deletions

docker-compose.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ services:
1515
catalog.api:
1616
image: eshop/catalog.api:latest
1717
environment:
18-
- ConnectionString=Server=catalogdata;Port=5432;Database=postgres;username=postgres
18+
- ConnectionString=Server=catalogdata;Initial Catalog=CatalogDB;User Id=sa;Password=Pass@word
1919
expose:
2020
- "80"
2121
depends_on:
2222
- catalogdata
2323

2424
catalogdata:
25-
image: glennc/eshopdata
25+
image: eshop/mssql-server-private-preview
26+
environment:
27+
- ACCEPT_EULA=Y
28+
- SA_PASSWORD=Pass@word
29+
ports:
30+
- "1455:1433"
2631

2732
ordering.api:
2833
image: eshop/ordering.api:latest
@@ -43,7 +48,7 @@ services:
4348
image: eshop/ordering.data.sqlserver.linux
4449
ports:
4550
- "1433:1433"
46-
51+
4752
basket.api:
4853
image: eshop/basket.api:latest
4954
environment:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0)
2929
.LongCountAsync();
3030

3131
var itemsOnPage = await _context.CatalogItems
32+
.OrderBy(c=>c.Name)
3233
.Skip(pageSize * pageIndex)
3334
.Take(pageSize)
3435
.ToListAsync();

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using EntityFrameworkCore.Metadata.Builders;
44
using Microsoft.EntityFrameworkCore;
5-
using Npgsql.EntityFrameworkCore.PostgreSQL;
65

76
public class CatalogContext : DbContext
87
{
@@ -18,32 +17,19 @@ public CatalogContext(DbContextOptions options) : base(options)
1817

1918
protected override void OnModelCreating(ModelBuilder builder)
2019
{
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-
3320
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
3421
builder.Entity<CatalogType>(ConfigureCatalogType);
3522
builder.Entity<CatalogItem>(ConfigureCatalogItem);
3623

3724

38-
builder.HasPostgresExtension("uuid-ossp");
3925
}
4026

4127
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
4228
{
43-
builder.ForNpgsqlToTable("catalog");
29+
builder.ToTable("Catalog");
4430

4531
builder.Property(ci => ci.Id)
46-
.HasDefaultValueSql("nextval('idseqcatalog')")
32+
.ForSqlServerUseSequenceHiLo("catalog_hilo")
4733
.IsRequired();
4834

4935
builder.Property(ci => ci.Name)
@@ -68,11 +54,13 @@ void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
6854

6955
void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder)
7056
{
71-
builder.ForNpgsqlToTable("catalogbrand");
57+
builder.ToTable("CatalogBrand");
7258

73-
builder.Property(cb => cb.Id)
74-
.HasDefaultValueSql("nextval('idseqcatalogbrand')")
75-
.IsRequired();
59+
builder.HasKey(ci => ci.Id);
60+
61+
builder.Property(ci => ci.Id)
62+
.ForSqlServerUseSequenceHiLo("catalog_brand_hilo")
63+
.IsRequired();
7664

7765
builder.Property(cb => cb.Brand)
7866
.IsRequired()
@@ -81,11 +69,14 @@ void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder)
8169

8270
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
8371
{
84-
builder.ForNpgsqlToTable("catalogtype");
8572

86-
builder.Property(cb => cb.Id)
87-
.HasDefaultValueSql("nextval('idseqcatalogtype')")
88-
.IsRequired();
73+
builder.ToTable("CatalogType");
74+
75+
builder.HasKey(ci => ci.Id);
76+
77+
builder.Property(ci => ci.Id)
78+
.ForSqlServerUseSequenceHiLo("catalog_type_hilo")
79+
.IsRequired();
8980

9081
builder.Property(cb => cb.Type)
9182
.IsRequired()

src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
22
{
3+
using EntityFrameworkCore;
34
using Microsoft.AspNetCore.Builder;
4-
using System;
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Threading.Tasks;
@@ -15,7 +15,7 @@ public static async Task SeedAsync(IApplicationBuilder applicationBuilder)
1515

1616
using (context)
1717
{
18-
context.Database.EnsureCreated();
18+
context.Database.Migrate();
1919

2020
if (!context.CatalogBrands.Any())
2121
{

src/Services/Catalog/Catalog.API/Infrastructure/Migrations/20161103152832_Initial.Designer.cs

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace Catalog.API.Infrastructure.Migrations
6+
{
7+
public partial class Initial : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.CreateSequence(
12+
name: "catalog_brand_hilo",
13+
incrementBy: 10);
14+
15+
migrationBuilder.CreateSequence(
16+
name: "catalog_hilo",
17+
incrementBy: 10);
18+
19+
migrationBuilder.CreateSequence(
20+
name: "catalog_type_hilo",
21+
incrementBy: 10);
22+
23+
migrationBuilder.CreateTable(
24+
name: "catalogbrand",
25+
columns: table => new
26+
{
27+
Id = table.Column<int>(nullable: false),
28+
Brand = table.Column<string>(maxLength: 100, nullable: false)
29+
},
30+
constraints: table =>
31+
{
32+
table.PrimaryKey("PK_catalogbrand", x => x.Id);
33+
});
34+
35+
migrationBuilder.CreateTable(
36+
name: "CatalogTypes",
37+
columns: table => new
38+
{
39+
Id = table.Column<int>(nullable: false),
40+
Type = table.Column<string>(maxLength: 100, nullable: false)
41+
},
42+
constraints: table =>
43+
{
44+
table.PrimaryKey("PK_CatalogTypes", x => x.Id);
45+
});
46+
47+
migrationBuilder.CreateTable(
48+
name: "catalog",
49+
columns: table => new
50+
{
51+
Id = table.Column<int>(nullable: false),
52+
CatalogBrandId = table.Column<int>(nullable: false),
53+
CatalogTypeId = table.Column<int>(nullable: false),
54+
Description = table.Column<string>(nullable: true),
55+
Name = table.Column<string>(maxLength: 50, nullable: false),
56+
PictureUri = table.Column<string>(nullable: true),
57+
Price = table.Column<decimal>(nullable: false)
58+
},
59+
constraints: table =>
60+
{
61+
table.PrimaryKey("PK_catalog", x => x.Id);
62+
table.ForeignKey(
63+
name: "FK_catalog_catalogbrand_CatalogBrandId",
64+
column: x => x.CatalogBrandId,
65+
principalTable: "catalogbrand",
66+
principalColumn: "Id",
67+
onDelete: ReferentialAction.Cascade);
68+
table.ForeignKey(
69+
name: "FK_catalog_CatalogTypes_CatalogTypeId",
70+
column: x => x.CatalogTypeId,
71+
principalTable: "CatalogTypes",
72+
principalColumn: "Id",
73+
onDelete: ReferentialAction.Cascade);
74+
});
75+
76+
migrationBuilder.CreateIndex(
77+
name: "IX_catalog_CatalogBrandId",
78+
table: "catalog",
79+
column: "CatalogBrandId");
80+
81+
migrationBuilder.CreateIndex(
82+
name: "IX_catalog_CatalogTypeId",
83+
table: "catalog",
84+
column: "CatalogTypeId");
85+
}
86+
87+
protected override void Down(MigrationBuilder migrationBuilder)
88+
{
89+
migrationBuilder.DropSequence(
90+
name: "catalog_brand_hilo");
91+
92+
migrationBuilder.DropSequence(
93+
name: "catalog_hilo");
94+
95+
migrationBuilder.DropSequence(
96+
name: "catalog_type_hilo");
97+
98+
migrationBuilder.DropTable(
99+
name: "catalog");
100+
101+
migrationBuilder.DropTable(
102+
name: "catalogbrand");
103+
104+
migrationBuilder.DropTable(
105+
name: "CatalogTypes");
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)