Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Web/Interfaces/IPublishEventService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace Microsoft.eShopWeb.Web.Interfaces;

public enum EventType
{
PageOpenings,
AddToCart,
Checkout
}


public interface IPublishEventService
{
void PublishEvent(EventType eventType, IDictionary<string, string> properties);
}
25 changes: 18 additions & 7 deletions src/Web/Pages/Basket/Checkout.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@

namespace Microsoft.eShopWeb.Web.Pages.Basket;

public class CheckoutModel : PageModel
public class CheckoutModel : PageBase
{
private readonly IBasketService _basketService;
private readonly IOrderService _orderService;
private string _username = null;
private readonly IBasketViewModelService _basketViewModelService;
private readonly IAppLogger<CheckoutModel> _logger;

private readonly IAppLogger<CheckoutModel> _logger;
private readonly IPublishEventService _publishService;

public CheckoutModel(IBasketService basketService,
IBasketViewModelService basketViewModelService,
IOrderService orderService,
IAppLogger<CheckoutModel> logger)
IAppLogger<CheckoutModel> logger,
IPublishEventService publishEventService) : base(publishEventService)
{
_basketService = basketService;
_orderService = orderService;
_basketViewModelService = basketViewModelService;
_logger = logger;
_logger = logger;
_publishService = publishEventService;
}

public BasketViewModel BasketModel { get; set; } = new BasketViewModel();
Expand All @@ -52,7 +55,15 @@ public async Task<IActionResult> OnPost(IEnumerable<BasketItemViewModel> items)

var updateModel = items.ToDictionary(b => b.Id.ToString(), b => b.Quantity);
await _basketService.SetQuantities(BasketModel.Id, updateModel);
await _orderService.CreateOrderAsync(BasketModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));
await _orderService.CreateOrderAsync(BasketModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));

var dictionary = new Dictionary<string, string>
{
{ "Username", BasketModel.BuyerId},
{ "Total", BasketModel.Total().ToString()}
};

_publishService.PublishEvent(EventType.Checkout, dictionary);
await _basketService.DeleteBasketAsync(BasketModel.Id);
}
catch (EmptyBasketOnCheckoutException emptyBasketOnCheckoutException)
Expand All @@ -66,7 +77,7 @@ public async Task<IActionResult> OnPost(IEnumerable<BasketItemViewModel> items)
}

private async Task SetBasketModelAsync()
{
{
GetOrSetBasketCookieAndUserName();
BasketModel = await _basketViewModelService.GetOrCreateBasketForUser(_username);
}
Expand Down
19 changes: 15 additions & 4 deletions src/Web/Pages/Basket/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ namespace Microsoft.eShopWeb.Web.Pages.Basket;
public class IndexModel : PageModel
{
private readonly IBasketService _basketService;
private readonly IBasketViewModelService _basketViewModelService;

private readonly IBasketViewModelService _basketViewModelService;
private readonly IPublishEventService _publishService;

public IndexModel(IBasketService basketService,
IBasketViewModelService basketViewModelService)
IBasketViewModelService basketViewModelService,
IPublishEventService publishService) //: base(publishService)
{
_basketService = basketService;
_basketViewModelService = basketViewModelService;
_basketViewModelService = basketViewModelService;
_publishService = publishService;
}

public BasketViewModel BasketModel { get; set; } = new BasketViewModel();
Expand All @@ -41,6 +44,14 @@ public async Task<IActionResult> OnPost(CatalogItemViewModel productDetails)
var basket = await _basketService.AddItemToBasket(username,
productDetails.Id, productDetails.Price);

var dictionary = new Dictionary<string, string>
{
{ "Username", username},
{ "ProductId", productDetails.Id.ToString()},
{ "Price", productDetails.Price.ToString() }
};

_publishService.PublishEvent(EventType.AddToCart, dictionary);
BasketModel = await _basketViewModelService.Map(basket);

return RedirectToPage();
Expand Down
13 changes: 9 additions & 4 deletions src/Web/Pages/Basket/Success.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using Microsoft.AspNetCore.Mvc.RazorPages;

using Microsoft.eShopWeb.Web.Interfaces;

namespace Microsoft.eShopWeb.Web.Pages.Basket;

public class SuccessModel : PageModel
{
public class SuccessModel : PageBase
{

public SuccessModel(IPublishEventService publishEventService) : base(publishEventService)
{
}

public void OnGet()
{

}
}
13 changes: 13 additions & 0 deletions src/Web/Pages/PageBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.eShopWeb.Web.Interfaces;

namespace Microsoft.eShopWeb.Web.Pages;

public abstract class PageBase : PageModel
{
public PageBase(IPublishEventService publishEventService)
{
publishEventService.PublishEvent(EventType.PageOpenings, new Dictionary<string, string> {{ "PageName", this.GetType().Name }});
}
}
7 changes: 7 additions & 0 deletions src/Web/Properties/serviceDependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"appInsights1": {
"type": "appInsights"
}
}
}
7 changes: 7 additions & 0 deletions src/Web/Properties/serviceDependencies.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"appInsights1": {
"type": "appInsights.sdk"
}
}
}
27 changes: 27 additions & 0 deletions src/Web/Services/PublishEventService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using Microsoft.ApplicationInsights;
using Microsoft.eShopWeb.Web.Interfaces;
namespace Microsoft.eShopWeb.Web.Services;

public class PublishEventService : IPublishEventService
{
private readonly TelemetryClient _telemetryClient;

public PublishEventService(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient;
}


public void PublishEvent(EventType eventType, IDictionary<string, string> properties)
{
try
{
_telemetryClient.TrackEvent(eventType.ToString(), properties);
}
catch
{
//ignore;
}
}
}
7 changes: 6 additions & 1 deletion src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Web.Configuration;
using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.eShopWeb.Web.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down Expand Up @@ -143,8 +145,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddBlazorServices();

services.AddDatabaseDeveloperPageExceptionFilter();
services.AddApplicationInsightsTelemetry();
services.AddScoped<IPublishEventService, PublishEventService>();

_services = services; // used to debug registered services
_services = services; // used to debug registered services

}


Expand Down
1 change: 1 addition & 0 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="MediatR" Version="10.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1" />
Expand Down
3 changes: 3 additions & 0 deletions src/Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"System": "Warning"
},
"AllowedHosts": "*"
},
"ApplicationInsights": {
"InstrumentationKey": "00000000-0000-0000-0000-000000000000"
}
}