Skip to content

Commit f502c23

Browse files
committed
Add persistence of published integration events for Catalog (the only microservice publishing integration events by the moment).
1 parent 4e5e32c commit f502c23

13 files changed

Lines changed: 300 additions & 29 deletions

src/Services/Catalog/Catalog.API/Catalog.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.0" />
4646
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
4747
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" />
48+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
4849
<PackageReference Include="Swashbuckle" Version="6.0.0-beta902" />
4950
</ItemGroup>
5051

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
66
using Microsoft.eShopOnContainers.Services.Common.Infrastructure;
77
using Microsoft.eShopOnContainers.Services.Common.Infrastructure.Catalog;
8+
using Microsoft.eShopOnContainers.Services.Common.Infrastructure.Data;
89
using Microsoft.Extensions.Options;
10+
using System;
911
using System.Collections.Generic;
1012
using System.Linq;
1113
using System.Threading.Tasks;
@@ -104,12 +106,6 @@ public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId,
104106
var model = new PaginatedItemsViewModel<CatalogItem>(
105107
pageIndex, pageSize, totalItems, itemsOnPage);
106108

107-
//hook to run integration tests until POST methods are created
108-
if (catalogTypeId.HasValue && catalogTypeId == 1)
109-
{
110-
_eventBus.Publish(new CatalogPriceChanged(2, 10.4M, 8.4M));
111-
}
112-
113109
return Ok(model);
114110
}
115111

@@ -153,11 +149,12 @@ public async Task<IActionResult> Post([FromBody]CatalogItem value)
153149
_context.CatalogItems.Update(item);
154150
await _context.SaveChangesAsync();
155151

156-
_eventBus.Publish(new CatalogPriceChanged(item.Id, item.Price, oldPrice));
152+
var @event = new CatalogPriceChanged(item.Id, item.Price, oldPrice);
153+
await ProcessEventAsync(@event);
157154
}
158155

159156
return Ok();
160-
}
157+
}
161158

162159
private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
163160
var baseUri = _settings.Value.ExternalCatalogBaseUrl;
@@ -168,5 +165,22 @@ private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
168165

169166
return items;
170167
}
168+
169+
private async Task ProcessEventAsync(IntegrationEventBase @event)
170+
{
171+
_eventBus.Publish(@event);
172+
var eventData = new IntegrationEvent(@event);
173+
eventData.TimesSent++;
174+
eventData.State = EventStateEnum.Sent;
175+
try
176+
{
177+
_context.IntegrationEvents.Add(eventData);
178+
await _context.SaveChangesAsync();
179+
}
180+
catch (Exception ex)
181+
{
182+
var t = ex.Message;
183+
}
184+
}
171185
}
172186
}

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using EntityFrameworkCore.Metadata.Builders;
44
using Microsoft.EntityFrameworkCore;
55
using Model;
6+
using Microsoft.eShopOnContainers.Services.Common.Infrastructure.Data;
67

78
public class CatalogContext : DbContext
89
{
@@ -12,12 +13,15 @@ public CatalogContext(DbContextOptions options) : base(options)
1213
public DbSet<CatalogItem> CatalogItems { get; set; }
1314
public DbSet<CatalogBrand> CatalogBrands { get; set; }
1415
public DbSet<CatalogType> CatalogTypes { get; set; }
16+
public DbSet<IntegrationEvent> IntegrationEvents { get; set; }
17+
1518
protected override void OnModelCreating(ModelBuilder builder)
1619
{
1720
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
1821
builder.Entity<CatalogType>(ConfigureCatalogType);
1922
builder.Entity<CatalogItem>(ConfigureCatalogItem);
20-
}
23+
builder.Entity<IntegrationEvent>(ConfigureIntegrationEvent);
24+
}
2125

2226
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
2327
{
@@ -75,5 +79,31 @@ void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
7579
.IsRequired()
7680
.HasMaxLength(100);
7781
}
82+
83+
void ConfigureIntegrationEvent(EntityTypeBuilder<IntegrationEvent> builder)
84+
{
85+
builder.ToTable("IntegrationEvent");
86+
87+
builder.HasKey(e => e.EventId);
88+
89+
builder.Property(e => e.EventId)
90+
.IsRequired();
91+
92+
builder.Property(e => e.Content)
93+
.IsRequired();
94+
95+
builder.Property(e => e.CreationTime)
96+
.IsRequired();
97+
98+
builder.Property(e => e.State)
99+
.IsRequired();
100+
101+
builder.Property(e => e.TimesSent)
102+
.IsRequired();
103+
104+
builder.Property(e => e.EventTypeName)
105+
.IsRequired();
106+
107+
}
78108
}
79109
}

src/Services/Catalog/Catalog.API/Infrastructure/Migrations/20170314083211_AddEventTable.Designer.cs

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 AddEventTable : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.CreateTable(
12+
name: "IntegrationEvent",
13+
columns: table => new
14+
{
15+
EventId = table.Column<Guid>(nullable: false),
16+
Content = table.Column<string>(nullable: false),
17+
CreationTime = table.Column<DateTime>(nullable: false),
18+
EventTypeName = table.Column<string>(maxLength: 200, nullable: false),
19+
State = table.Column<int>(nullable: false),
20+
TimesSent = table.Column<int>(nullable: false)
21+
},
22+
constraints: table =>
23+
{
24+
table.PrimaryKey("PK_IntegrationEvent", x => x.EventId);
25+
});
26+
}
27+
28+
protected override void Down(MigrationBuilder migrationBuilder)
29+
{
30+
migrationBuilder.DropTable(
31+
name: "IntegrationEvent");
32+
}
33+
}
34+
}

src/Services/Catalog/Catalog.API/Infrastructure/Migrations/CatalogContextModelSnapshot.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.EntityFrameworkCore.Metadata;
55
using Microsoft.EntityFrameworkCore.Migrations;
66
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
7+
using Microsoft.eShopOnContainers.Services.Common.Infrastructure;
78

89
namespace Catalog.API.Infrastructure.Migrations
910
{
@@ -13,13 +14,13 @@ partial class CatalogContextModelSnapshot : ModelSnapshot
1314
protected override void BuildModel(ModelBuilder modelBuilder)
1415
{
1516
modelBuilder
16-
.HasAnnotation("ProductVersion", "1.0.1")
17+
.HasAnnotation("ProductVersion", "1.1.0-rtm-22752")
1718
.HasAnnotation("SqlServer:Sequence:.catalog_brand_hilo", "'catalog_brand_hilo', '', '1', '10', '', '', 'Int64', 'False'")
1819
.HasAnnotation("SqlServer:Sequence:.catalog_hilo", "'catalog_hilo', '', '1', '10', '', '', 'Int64', 'False'")
1920
.HasAnnotation("SqlServer:Sequence:.catalog_type_hilo", "'catalog_type_hilo', '', '1', '10', '', '', 'Int64', 'False'")
2021
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
2122

22-
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.CatalogBrand", b =>
23+
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Model.CatalogBrand", b =>
2324
{
2425
b.Property<int>("Id")
2526
.ValueGeneratedOnAdd()
@@ -28,14 +29,14 @@ protected override void BuildModel(ModelBuilder modelBuilder)
2829

2930
b.Property<string>("Brand")
3031
.IsRequired()
31-
.HasAnnotation("MaxLength", 100);
32+
.HasMaxLength(100);
3233

3334
b.HasKey("Id");
3435

3536
b.ToTable("CatalogBrand");
3637
});
3738

38-
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.CatalogItem", b =>
39+
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Model.CatalogItem", b =>
3940
{
4041
b.Property<int>("Id")
4142
.ValueGeneratedOnAdd()
@@ -50,7 +51,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
5051

5152
b.Property<string>("Name")
5253
.IsRequired()
53-
.HasAnnotation("MaxLength", 50);
54+
.HasMaxLength(50);
5455

5556
b.Property<string>("PictureUri");
5657

@@ -65,7 +66,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
6566
b.ToTable("Catalog");
6667
});
6768

68-
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.CatalogType", b =>
69+
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Model.CatalogType", b =>
6970
{
7071
b.Property<int>("Id")
7172
.ValueGeneratedOnAdd()
@@ -74,21 +75,44 @@ protected override void BuildModel(ModelBuilder modelBuilder)
7475

7576
b.Property<string>("Type")
7677
.IsRequired()
77-
.HasAnnotation("MaxLength", 100);
78+
.HasMaxLength(100);
7879

7980
b.HasKey("Id");
8081

8182
b.ToTable("CatalogType");
8283
});
8384

84-
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.CatalogItem", b =>
85+
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Common.Infrastructure.Data.IntegrationEvent", b =>
8586
{
86-
b.HasOne("Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.CatalogBrand", "CatalogBrand")
87+
b.Property<Guid>("EventId")
88+
.ValueGeneratedOnAdd();
89+
90+
b.Property<string>("Content")
91+
.IsRequired();
92+
93+
b.Property<DateTime>("CreationTime");
94+
95+
b.Property<string>("EventTypeName")
96+
.IsRequired()
97+
.HasMaxLength(200);
98+
99+
b.Property<int>("State");
100+
101+
b.Property<int>("TimesSent");
102+
103+
b.HasKey("EventId");
104+
105+
b.ToTable("IntegrationEvent");
106+
});
107+
108+
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Catalog.API.Model.CatalogItem", b =>
109+
{
110+
b.HasOne("Microsoft.eShopOnContainers.Services.Catalog.API.Model.CatalogBrand", "CatalogBrand")
87111
.WithMany()
88112
.HasForeignKey("CatalogBrandId")
89113
.OnDelete(DeleteBehavior.Cascade);
90114

91-
b.HasOne("Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.CatalogType", "CatalogType")
115+
b.HasOne("Microsoft.eShopOnContainers.Services.Catalog.API.Model.CatalogType", "CatalogType")
92116
.WithMany()
93117
.HasForeignKey("CatalogTypeId")
94118
.OnDelete(DeleteBehavior.Cascade);

0 commit comments

Comments
 (0)