-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...mplCommerce.Module.Inventory/Areas/Inventory/Views/EmailTemplates/BackInStockEmail.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@using SimplCommerce.Module.Catalog.Models | ||
@using SimplCommerce.Module.Core.Extensions | ||
@using SimplCommerce.Module.Core.Services | ||
|
||
@inject IWorkContext _workContext | ||
@inject IMediaService _mediaService | ||
|
||
@{ | ||
Layout = null; | ||
|
||
var hostName = _workContext.GetCurrentHostName(); | ||
var thumbnailUrl = _mediaService.GetThumbnailUrl(Model.ThumbnailImage); | ||
} | ||
|
||
@model SimplCommerce.Module.Catalog.Models.Product | ||
|
||
<div style="border: 1px solid #ebebeb; width: 50%; padding: 16px 24px; margin: 0 auto;"> | ||
Hi, <b>@Model.Name</b> in now available. | ||
Get in now before it out of stock again. | ||
<br /> | ||
<br /> | ||
<div> | ||
<img src="https://@hostName@thumbnailUrl" /> | ||
<h2>@Model.Name</h2> | ||
<h4 style="color:red;">$1000</h4> | ||
</div> | ||
<a href="https://@hostName/@Model.Slug"> | ||
View product | ||
</a> | ||
</div> |
14 changes: 14 additions & 0 deletions
14
src/Modules/SimplCommerce.Module.Inventory/Event/BackInStock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
|
||
namespace SimplCommerce.Module.Inventory.Event | ||
{ | ||
public class BackInStock : INotification | ||
{ | ||
public long ProductId { get; set; } | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Modules/SimplCommerce.Module.Inventory/Event/BackInStockSendEmailHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using SimplCommerce.Module.Inventory.Services; | ||
|
||
namespace SimplCommerce.Module.Inventory.Event | ||
{ | ||
public class BackInStockSendEmailHandler : INotificationHandler<BackInStock> | ||
{ | ||
public readonly IStockSubscriptionService _stockSubscriptionService; | ||
|
||
public BackInStockSendEmailHandler(IStockSubscriptionService stockSubscriptionService) | ||
{ | ||
_stockSubscriptionService = stockSubscriptionService; | ||
} | ||
|
||
public async Task Handle(BackInStock notification, CancellationToken cancellationToken) | ||
{ | ||
await _stockSubscriptionService.BackInStockSendNotificationsAsync(notification.ProductId); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Modules/SimplCommerce.Module.Inventory/Models/BackInStockSubscription.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using SimplCommerce.Infrastructure.Models; | ||
|
||
namespace SimplCommerce.Module.Inventory.Models | ||
{ | ||
public class BackInStockSubscription : EntityBase | ||
{ | ||
public long ProductId { get; set; } | ||
public string CustomerEmail { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Modules/SimplCommerce.Module.Inventory/Services/IStockSubscriptionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SimplCommerce.Module.Inventory.Services | ||
{ | ||
public interface IStockSubscriptionService | ||
{ | ||
Task BackInStockSubscribeAsync(long productId, string customerEmail); | ||
|
||
Task BackInStockSendNotificationsAsync(long productId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Modules/SimplCommerce.Module.Inventory/Services/StockSubscriptionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Routing; | ||
using Microsoft.EntityFrameworkCore; | ||
using SimplCommerce.Infrastructure.Data; | ||
using SimplCommerce.Infrastructure.Web; | ||
using SimplCommerce.Module.Catalog.Models; | ||
using SimplCommerce.Module.Core.Models; | ||
using SimplCommerce.Module.Core.Services; | ||
using SimplCommerce.Module.Inventory.Models; | ||
|
||
namespace SimplCommerce.Module.Inventory.Services | ||
{ | ||
public class StockSubscriptionService : IStockSubscriptionService | ||
{ | ||
private readonly IRepository<BackInStockSubscription> _backInStockSubscriptionRepository; | ||
private readonly IRepository<Product> _productRepository; | ||
private readonly IEmailSender _emailSender; | ||
private readonly IRazorViewRenderer _viewRender; | ||
|
||
public StockSubscriptionService(IRepository<BackInStockSubscription> backInStockSubscriptionRepository, IEmailSender emailSender, IRazorViewRenderer viewRender, IRepository<Product> productRepository) | ||
{ | ||
_backInStockSubscriptionRepository = backInStockSubscriptionRepository; | ||
_emailSender = emailSender; | ||
_viewRender = viewRender; | ||
_productRepository = productRepository; | ||
} | ||
|
||
public async Task BackInStockSendNotificationsAsync(long productId) | ||
{ | ||
var subscriptions = await _backInStockSubscriptionRepository | ||
.Query() | ||
.Where(o => o.ProductId == productId) | ||
.ToListAsync(); | ||
|
||
var product = await _productRepository | ||
.Query() | ||
.Where(o => o.Id == productId) | ||
.Include(o => o.ThumbnailImage) | ||
.FirstOrDefaultAsync(); | ||
|
||
var emailBody = await _viewRender.RenderViewToStringAsync("/Areas/Inventory/Views/EmailTemplates/BackInStockEmail.cshtml", product); | ||
var emailSubject = $"Back in stock"; | ||
|
||
foreach (var subscription in subscriptions) | ||
{ | ||
await _emailSender.SendEmailAsync(subscription.CustomerEmail, emailSubject, emailBody, true); | ||
|
||
_backInStockSubscriptionRepository.Remove(subscription); | ||
} | ||
|
||
await _backInStockSubscriptionRepository.SaveChangesAsync(); | ||
} | ||
|
||
public async Task BackInStockSubscribeAsync(long productId, string customerEmail) | ||
{ | ||
var subscription = new BackInStockSubscription | ||
{ | ||
ProductId = productId, | ||
CustomerEmail = customerEmail | ||
}; | ||
|
||
_backInStockSubscriptionRepository.Add(subscription); | ||
await _backInStockSubscriptionRepository.SaveChangesAsync(); | ||
} | ||
} | ||
} |
Oops, something went wrong.