-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
StockSubscriptionService.cs
70 lines (60 loc) · 2.72 KB
/
StockSubscriptionService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();
}
}
}