Skip to content

Commit

Permalink
Code refactoring for #1090 (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
s04v committed Mar 11, 2024
1 parent 4565b57 commit c8a29a9
Show file tree
Hide file tree
Showing 15 changed files with 4,821 additions and 62 deletions.
Expand Up @@ -203,7 +203,7 @@
<div class="back-in-stock-subscribe">
<form action="api/stocks/back-in-stock">
<div>
@if (SignInManager.IsSignedIn(User))
@if (User.Identity.IsAuthenticated)
{
<p>Subscribe and we'll notify you when the product is back in stock.</p>
<div class="d-flex">
Expand Down
Expand Up @@ -33,10 +33,7 @@ public class WorkContext : IWorkContext
_configuration = configuration;
}

public string GetCurrentHostName()
{
return _httpContext.Request.Host.Value;
}
public string GetCurrentHostName() => _httpContext.Request.Host.Value;

public async Task<User> GetCurrentUser()
{
Expand Down
Expand Up @@ -24,9 +24,9 @@ public class StockApiController : Controller
private readonly IWorkContext _workContext;
private readonly IRepository<Warehouse> _warehouseRepository;
private readonly IRepository<StockHistory> _stockHistoryRepository;
private readonly IRepository<BackInStockSubscription> _backInStockSubscriptionRepository;
private readonly IRepository<ProductBackInStockSubscription> _backInStockSubscriptionRepository;

public StockApiController(IRepository<Stock> stockRepository, IStockService stockService, IWorkContext workContext, IRepository<Warehouse> warehouseRepository, IRepository<StockHistory> stockHistoryRepository, IRepository<BackInStockSubscription> backInStockSubscriptionRepository, IStockSubscriptionService stockSubscriptionService)
public StockApiController(IRepository<Stock> stockRepository, IStockService stockService, IWorkContext workContext, IRepository<Warehouse> warehouseRepository, IRepository<StockHistory> stockHistoryRepository, IRepository<ProductBackInStockSubscription> backInStockSubscriptionRepository, IStockSubscriptionService stockSubscriptionService)
{
_stockRepository = stockRepository;
_stockService = stockService;
Expand Down Expand Up @@ -145,13 +145,12 @@ public async Task<IActionResult> GetStockHistory(int warehouseId, int productId)
public async Task<IActionResult> BackInStockSubscribe(long productId, string customerEmail)
{
if (await _backInStockSubscriptionRepository.Query()
.Where(o => o.ProductId == productId && o.CustomerEmail == customerEmail)
.AnyAsync())
.AnyAsync(o => o.ProductId == productId && o.CustomerEmail == customerEmail))
{
return Conflict();
}

await _stockSubscriptionService.BackInStockSubscribeAsync(productId, customerEmail);
await _stockSubscriptionService.ProductBackInStockSubscribeAsync(productId, customerEmail);

return Ok();
}
Expand Down

This file was deleted.

Expand Up @@ -7,7 +7,7 @@

namespace SimplCommerce.Module.Inventory.Event
{
public class BackInStock : INotification
public class ProductBackInStock : INotification
{
public long ProductId { get; set; }
}
Expand Down
@@ -0,0 +1,22 @@
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 ProductBackInStockSendEmailHandler : INotificationHandler<ProductBackInStock>
{
public readonly IStockSubscriptionService _stockSubscriptionService;

public ProductBackInStockSendEmailHandler(IStockSubscriptionService stockSubscriptionService)
=> _stockSubscriptionService = stockSubscriptionService;

public async Task Handle(ProductBackInStock notification, CancellationToken cancellationToken)
=> await _stockSubscriptionService.ProductBackInStockSendNotificationsAsync(notification.ProductId);
}
}
Expand Up @@ -7,9 +7,10 @@

namespace SimplCommerce.Module.Inventory.Models
{
public class BackInStockSubscription : EntityBase
public class ProductBackInStockSubscription : EntityBase
{
public long ProductId { get; set; }

public string CustomerEmail { get; set; }
}
}
Expand Up @@ -17,8 +17,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<IStockService, StockService>();
serviceCollection.AddTransient<IStockSubscriptionService, StockSubscriptionService>();
serviceCollection.AddTransient<INotificationHandler<BackInStock>, BackInStockSendEmailHandler>();

serviceCollection.AddTransient<INotificationHandler<ProductBackInStock>, ProductBackInStockSendEmailHandler>();

GlobalConfiguration.RegisterAngularModule("simplAdmin.inventory");
}
Expand Down
Expand Up @@ -8,8 +8,8 @@ namespace SimplCommerce.Module.Inventory.Services
{
public interface IStockSubscriptionService
{
Task BackInStockSubscribeAsync(long productId, string customerEmail);
Task ProductBackInStockSubscribeAsync(long productId, string customerEmail);

Task BackInStockSendNotificationsAsync(long productId);
Task ProductBackInStockSendNotificationsAsync(long productId);
}
}
Expand Up @@ -49,7 +49,6 @@ public async Task UpdateStock(StockUpdateRequest stockUpdateRequest)
var stock = await _stockRepository.Query().FirstOrDefaultAsync(x => x.ProductId == stockUpdateRequest.ProductId && x.WarehouseId == stockUpdateRequest.WarehouseId);

var prevStockQuantity = product.StockQuantity;

stock.Quantity = stock.Quantity + stockUpdateRequest.AdjustedQuantity;
product.StockQuantity = product.StockQuantity + stockUpdateRequest.AdjustedQuantity;
var stockHistory = new StockHistory
Expand All @@ -67,7 +66,7 @@ public async Task UpdateStock(StockUpdateRequest stockUpdateRequest)

if (prevStockQuantity <= 0 && product.StockQuantity > 0)
{
await _mediator.Publish(new BackInStock { ProductId = product.Id });
await _mediator.Publish(new ProductBackInStock { ProductId = product.Id });
}
}
}
Expand Down
Expand Up @@ -16,22 +16,22 @@ namespace SimplCommerce.Module.Inventory.Services
{
public class StockSubscriptionService : IStockSubscriptionService
{
private readonly IRepository<BackInStockSubscription> _backInStockSubscriptionRepository;
private readonly IRepository<ProductBackInStockSubscription> _productBackInStockSubscriptionRepository;
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)
public StockSubscriptionService(IRepository<ProductBackInStockSubscription> backInStockSubscriptionRepository, IEmailSender emailSender, IRazorViewRenderer viewRender, IRepository<Product> productRepository)
{
_backInStockSubscriptionRepository = backInStockSubscriptionRepository;
_productBackInStockSubscriptionRepository = backInStockSubscriptionRepository;
_emailSender = emailSender;
_viewRender = viewRender;
_productRepository = productRepository;
}

public async Task BackInStockSendNotificationsAsync(long productId)
public async Task ProductBackInStockSendNotificationsAsync(long productId)
{
var subscriptions = await _backInStockSubscriptionRepository
var subscriptions = await _productBackInStockSubscriptionRepository
.Query()
.Where(o => o.ProductId == productId)
.ToListAsync();
Expand All @@ -42,29 +42,30 @@ public async Task BackInStockSendNotificationsAsync(long productId)
.Include(o => o.ThumbnailImage)
.FirstOrDefaultAsync();

var emailBody = await _viewRender.RenderViewToStringAsync("/Areas/Inventory/Views/EmailTemplates/BackInStockEmail.cshtml", product);
var emailBody = await _viewRender.RenderViewToStringAsync("/Areas/Inventory/Views/EmailTemplates/ProductBackInStockEmail.cshtml", product);
var emailSubject = $"Back in stock";

foreach (var subscription in subscriptions)
{
await _emailSender.SendEmailAsync(subscription.CustomerEmail, emailSubject, emailBody, true);

_backInStockSubscriptionRepository.Remove(subscription);
_productBackInStockSubscriptionRepository.Remove(subscription);
}

await _backInStockSubscriptionRepository.SaveChangesAsync();
await _productBackInStockSubscriptionRepository.SaveChangesAsync();
}

public async Task BackInStockSubscribeAsync(long productId, string customerEmail)
public async Task ProductBackInStockSubscribeAsync(long productId, string customerEmail)
{
var subscription = new BackInStockSubscription
var subscription = new ProductBackInStockSubscription
{
ProductId = productId,
CustomerEmail = customerEmail
};

_backInStockSubscriptionRepository.Add(subscription);
await _backInStockSubscriptionRepository.SaveChangesAsync();
_productBackInStockSubscriptionRepository.Add(subscription);

await _productBackInStockSubscriptionRepository.SaveChangesAsync();
}
}
}

0 comments on commit c8a29a9

Please sign in to comment.